关联列等于____时循环显示多个记录

时间:2015-10-12 12:43:06

标签: php mysql arrays

我仍然是PHP和MySQL的新手,但愿意学习,现在我需要经过三天的修补后需要帮助。

首先,为了帮助您理解我的数据库结构,我有一个名为" o70vm_invoices_items"的MySQL数据库表。我试图遍历每个invoice_ID的结果。 items表中的每个项目都有一个PK(o70vm_invoices_items.id),invoice_ID列是另一个表(发票)的链接。

例如,在项目表中,某些发票将包含一个项目,其他发票可能包含两个或三个项目。我对如何循环显示与每张发票相关联的所有项目(o70vm_invoices_items.name)感到非常挑战。

这是我的MySQL查询语句:

$queryItems = "SELECT       
    o70vm_invoices_items.id AS 'Item_ID',
    o70vm_invoices_items.invoice_id AS 'Invoice_ID_on_Items',
    o70vm_invoices_items.name AS 'Program',
    o70vm_invoices_items.value AS 'Fee',
    o70vm_invoices_items.amount AS 'Qty',
    o70vm_invoices_items.desc AS 'forWeek',

    GROUP_CONCAT(o70vm_invoices_items.desc SEPARATOR ' & ') As 'forWeekGroup',          
    ROUND(SUM((o70vm_invoices_items.value)*(o70vm_invoices_items.amount)),2) AS 'INV-TOTAL'
    FROM o70vm_invoices_items
    WHERE o70vm_invoices_items.invoice_id = $invoiceID
    GROUP BY o70vm_invoices_items.invoice_id";

如您所见,我正在从INVOICE_ID搜索结果。这部分效果很好。我得到这些结果很容易显示。问题是我希望在" o70vm_invoices_items.name"中显示每个值。所选发票ID的列,我只能显示一个项目。

以下是我尝试循环显示结果并显示信息:

// storing the result of this MySQL query
$resultItems = mysql_query($queryItems) or die(mysql_error());

    echo "<table><tr>";

while($rowItems = mysql_fetch_array($resultItems))
    {

    echo "<td>";                
    echo "<input type='text' title='' name='name' id='name' value='". $rowItems['Program']. "' /><br>";
    echo            "</td>";
    }
    echo "</tr></table>";

同样,我只能获得一个项目的结果,而不是所有项目。任何帮助,非常感谢。我想我可能需要一种不同的方式来编写一个数组。另外,正如您所看到的,我将其显示在输入标记中,我希望稍后我能够编辑内容。

请注意:一旦我弄明白,我也需要对费用和数量列也做同样的事情。

2 个答案:

答案 0 :(得分:2)

您的查询包含这两行。它们一起保证您在结果集中只能获得一行。

invoice_id

您的WHERE行过滤您的表,因此结果集仅包含特定 GROUP BY o70vm_invoices_items.id 值的行。然后,GROUP BY在一个结果集行中汇总该特定值的信息。

从您的问题来看,每张发票上有多个项目,并且您希望显示项目的详细信息行。在这种情况下,您需要按项目分组,而不是GROUP BY

试试这个GROUP BY。

{{1}}

而且,您正在使用{{1}}功能的非标准的非标准MySQL扩展。如果你开始使用多个表格,这会让你很难受。阅读它。 https://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html

答案 1 :(得分:2)

首先,我会重写sql,使其看起来更容易阅读,为表格分配alias并在引用字段时使用它。当您加入多个表时,这尤其有用 - 每个alias必须是唯一的。对于原始sql,因为你只是从一个表中绘制数据,所以不需要真正的别名,也不需要使用完整的表名作为各个字段的前缀(即:table.field) - 只需要字段名就可以了已经足够了。

<?php
    $queryitems = "select 
            o.`id` as 'item_id',
            o.`invoice_id` as 'invoice_id_on_items',
            o.`name` as 'program',
            o.`value` as 'fee',
            o.`amount` as 'qty',
            o.`desc` as 'forweek',
            group_concat( o.`desc` separator ' & ' ) as 'forweekgroup',          
            round( sum( ( o.`value` ) * ( o.`amount` ) ),2 ) as 'inv-total'
            from `o70vm_invoices_items` o
            where o.`invoice_id` = '$invoiceid';";


    // storing the result of this MySQL query
    $resultItems = mysql_query( $queryItems );
    if( $resultItems ){
        echo '
            <table>
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">Program</th>
                    <th scope="col">fee</th>
                </tr>';

        $id=0; /* Each field / element that has an id must have a unique id ~ use a counter to achieve this */
        while( $row = mysql_fetch_object( $resultItems ) ){

            $id++;/* Increment the id counter */

            echo '
                <tr>
                    <td>'.$row->item_id.'</td>
                    <td>
                        <input type="text" title="'.$row->program.'" name="name" id="name'.$id.'" value="' . $row->program. '" />
                    </td>
                    <td>'.$row->fee.'</td>
                </tr>';
        }

        echo '
            </table>';
    } else {/* Do not give away too much information and degrade gracefully */
        echo 'Sorry, there was an error retrieving relevant information from the database';
    }

?>