Group Query Results in PHP

后端 未结 2 1267
独厮守ぢ
独厮守ぢ 2021-01-23 10:20

I am using the following query to populate a table:

$result = mysql_query(\"SELECT vendor, part_number, machine, Sum(q_received) AS received, Sum(q_rejected) AS          


        
相关标签:
2条回答
  • 2021-01-23 10:52

    During each loop you have to decide if it is time to draw the heading--you're going to list a new vendor. If it isn't a new vendor, you're still listing parts for the one you drew the last heading for, so you don't want the heading or to end the table.

    $current_vendor = false;
    while($row = mysql_fetch_array($result)) {
        // listing a new vendor? Output the heading, start the table
        if ($row['vendor'] != $current_vendor) {
                if ($current_vendor !== false)
                    echo '</table>[/fancy_table]'; // if we're changing vendors, close the table
            echo '
                <h4>'.$row['vendor'].'</h4>[fancy_table]
                <table>
                    <tr>
                    <th>Part Number</th>
                    <th>Quantity Received</th>
                    <th>Quantity Rejected</th>
                    <th>Machine</th>
                    </tr>
            ';
            $current_vendor = $row['vendor'];
        }
        // output the row of data
        echo '<tr>
            <td>'.$row['part_number'].'</td>
            <td>'.$row['received'].'</td>
            <td>'.$row['rejected'].'</td>
            <td>'.$row['machine'].'</td>
            </tr>
        ';
    }
    echo '</table>[/fancy_table]'; // close the final table
    

    On another note, you should start updating this code to use PDO. Obviously, you're in the middle of updating something that used to exist... make PDO a part of this update. The mysql_* functions are on the way to deprecation, and soon they'll be gone.

    PDO is not hard to transition into, and it is MUCH easier now while you're already working on things than it will be later, when you're faced with the "it isn't broke, don't fix it" problem.

    Here's a quick look at your code using the superior PDO DBA method:

       $pdo = new PDO("mysql:host=localhost;dbname=database", '-username-', '-password-');
       $sql = '
            SELECT
                vendor, 
                part_number, 
                machine, 
                SUM(q_received) AS "received",
                SUM(q_rejected) AS "rejected" 
            FROM 
                qa_parts 
            GROUP BY 
                vendor, 
                part_number, 
                machine 
            ORDER BY 
                vendor ASC
        ';
        $statement = $pdo->prepare($sql);
        $statement->execute();
        while ($order = $statement->fetch()) {
            /* Put your table code here! */
        }
    

    Documentation

    • foreach - http://php.net/manual/en/control-structures.foreach.php
    • mysql_query (DEPRECATED) - http://php.net/manual/en/function.mysql-query.php
    • PDO - http://www.php.net/manual/en/book.pdo.php
    • PDO::prepare - http://www.php.net/manual/en/pdo.prepare.php
    • PDOStatement::fetch - http://www.php.net/manual/en/pdostatement.fetch.php
    • PDOStatement::execute - http://www.php.net/manual/en/pdostatement.execute.php
    0 讨论(0)
  • 2021-01-23 10:52

    You need to split your loop into two parts. First part checks if you've got a new/different vendor, and sets up the "outer" setup. Then the inner part which dumps out that vendor's parts.

    e.g.

    $prev_vendor = null;
    while($row = ...) {
        if ($row['vendor'] != $prev_vendor) {
           ... new vendor stuff ...
           $prev_vendor = $row['vendor']; // update vendor for next iteration
        }
        ... dump this row's machine data ...
    }
    
    0 讨论(0)
提交回复
热议问题