递增&当数据匹配列

递增&当数据匹配列,第1张

我有一个表格,其列数据要排序。列中列出了不同的数字。我想将数字排序到范围内并计算数字在范围内出现的次数:

0

1 - 50,000

50,001 - 75,000

75,001 - 100,000

100,000

到目前为止,我已经创建了一个循环遍历列数据的while循环,并显示了一个包含所有数字的表。现在,我试图循环遍历行并对数字进行排序 - 如果它在某个范围内,它应该向该范围的变量/数组添加(递增1)。变量计算一个数字在该范围内出现的次数。

这些是我的if语句,应检查列中的数据是否在一个范围内,并在数组中添加一个数字(当我将这段代码添加到我的脚本中时,它会变为空白浏览器):

    if($credit_limit = 0) {
        $cred0  ;
    }

    if($credit_limit >= 1 && $credit_limit <= 50,000) {
        $cred1_50000  ;
    }

    if($credit_limit >= 50,001 && $credit_limit <= 75,000) {
        $cred50001_75000  ;
    }

    if($credit_limit >= 75,001 && $credit_limit <= 100,000) {
        $cred75001_100000  ;
    }

    if($credit_limit > 100,000) {
        $cred_100000  ;
    }

    $credit_data = array(
        array('0', $cred0),
        array('1 to 50,000', $cred1_50000),
        array('50,001 to 75,000', $cred50001_75000),
        array('75,001 to 100,000', $cred75001_100000),
        array('100,000', $cred_100000)  
    );

我很感激有关为什么我的代码在脚本上显示为空白以及为什么它不会增加数组中的变量或在回显时显示计数的任何见解。

以下是所有代码:

<?php
require_once("./includes/database_connection.php");

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    // VARIABLES FOR CREDIT LIMIT RANGES

    $cred0 = 0;
    $cred1_50000 = 0;
    $cred50001_75000 = 0;
    $cred75001_100000 = 0;
    $cred_100000 = 0;

    // QUERY TO GET DATA FROM CREDIT LIMIT COLUMN

    $credit_limit = 'SELECT creditLimit FROM customers ORDER BY customerNumber ASC';
    $result = mysqli_query($dbc, $credit_limit) 
        or die ('Error querying database');

?>

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>Home</title>
    <link type="text/css" rel="stylesheet" href="classic_cars.css" />
    <style>
        #table11 {
            height: 100px;
        }   
    </style>
</head>

<body>
    <?php
        require_once("./includes/navigation.php");
    ?>
        <h1>Credit Limit Table</h1>
        <div id="table11">
        <table border= "1"> 
            <tr>
                <td>Credit Limit</td>
            </tr>
        <?php

            while($row = mysqli_fetch_array($result)) {
                $creditLimit = $row['creditLimit'];

                // SHOW COLUMN WITH DATA IN A TABLE

                echo "<tr>
                        <td>$creditLimit</td>
                    </tr>";

                //    INCREMENT INTO ARRAY IF VALUE IN COLUMN IS WITHIN CERTAIN RANGE

                    if($credit_limit = 0) {
                        $cred0  ;
                    }

                    if($credit_limit >= 1 && $credit_limit <= 50,000) {
                        $cred1_50000  ;
                    }

                    if($credit_limit >= 50,001 && $credit_limit <= 75,000) {
                        $cred50001_75000  ;
                    }

                    if($credit_limit >= 75,001 && $credit_limit <= 100,000) {
                        $cred75001_100000  ;
                    }

                    if($credit_limit > 100,000) {
                        $cred_100000  ;
                    }

                    // ARRAY

                    $credit_data = array(
                        array('0', $cred0),
                        array('1 to 50,000', $cred1_50000),
                        array('50,001 to 75,000', $cred50001_75000),
                        array('75,001 to 100,000', $cred75001_100000),
                        array('100,000', $cred_100000)  
                    );

                    // DISPLAY HOW MANY TIMES A NUMBER IN SPECIFIED RANGE SHOWS UP
                    echo $cred0;
                    echo $cred1_50000;
                    echo $cred50001_75000;
                    echo $cred75001_100000;
                    echo $cred_100000;

            } // end while loop
        ?>

    </table>
    <div>



    <?php
        require_once("./includes/footer.php");
        mysqli_close($dbc);
    ?>  
</body>
</html>

最佳答案:

2 个答案:

答案 0 :(得分:1)

您的号码的所有逗号都需要删除。

50,000至50000等。

你可能可以使用number_format(),但这样做会更有效。

您还要分配=,而不是在此处比较==

if($credit_limit = 0)

因此$credit_limit将始终评估为0。

参考文献:

  • http://php.net/manual/en/language.operators.assignment.php
  • http://php.net/manual/en/language.operators.comparison.php

答案 1 :(得分:0)

不是用php循环完成所有工作,为什么不尝试mysql做的努力呢? 如果您将字段“creditLimit”编入索引,则mysql将为您提供答案。

 $credit_limit = 'SELECT COUNT(creditLimit) as total FROM customers 
WHERE creditLimit=0';
$result = mysqli_query($dbc, $credit_limit) ;
$data=mysql_fetch_assoc($result);
echo "<br>Number of costumers with credit 0 ".$data['total'];
//---------------------------------------------------------
 $credit_limit = 'SELECT COUNT(creditLimit) as total FROM customers 
WHERE creditLimit>0 and creditLimit<=50000';
$result = mysqli_query($dbc, $credit_limit) ;
$data=mysql_fetch_assoc($result);
echo "<br>Number of costumers >1 and <=50000 ".$data['total'];

//---------------------------------------------------------
 $credit_limit = 'SELECT COUNT(creditLimit) as total FROM customers 
WHERE creditLimit>50000 and creditLimit<=75000';
$result = mysqli_query($dbc, $credit_limit) ;
$data=mysql_fetch_assoc($result);
echo "<br>Number of costumers >50000 and <=75000 ".$data['total'];

//---------------------------------------------------------
 $credit_limit = 'SELECT COUNT(creditLimit) as total FROM customers 
WHERE creditLimit>75000 and creditLimit<=100000';
$result = mysqli_query($dbc, $credit_limit) ;
$data=mysql_fetch_assoc($result);
echo "<br>Number of costumers >75000 and <=100000 ".$data['total'];
//---------------------------------------------------------
 $credit_limit = 'SELECT COUNT(creditLimit) as total FROM customers WHERE creditLimit>100000';
$result = mysqli_query($dbc, $credit_limit) ;
$data=mysql_fetch_assoc($result);
echo "<br>Number of costumers >100000 ".$data['total'];
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复