php - 当关联列等于 ____ 时循环显示多条记录

标签 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 的结果。这部分效果很好。我得到这些结果很容易显示。问题是我希望在所选发票 ID 的“o70vm_invoices_items.name”列中显示每个值,并且我只能显示一个项目。

这是我尝试遍历结果并显示信息的尝试:

// 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>";

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

请注意:一旦我弄清楚了这一点,我还需要对 Fee 和 Qty 列执行相同的操作。

最佳答案

首先,我将重写 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';
    }

?>

关于php - 当关联列等于 ____ 时循环显示多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33081469/

相关文章:

php - Silverstripe:按作者过滤博客文章

java - 在 Java 中每月组合日期数组和总和值

java - 打印数组

PHP MySQL 添加到数据库中的现有值

php - 与 codeigniter 加入时出错 : variable query

php - 启动一个有循环的 Linux 作业

mysql - SQL优化: count all rows through subquery or own query/other improvements

javascript - 使用 PHP 将带有 < 和 > 运算符的字符串插入数据库

mysql - 大量基于文本的文档需要通过网络或桌面界面进行索引和搜索,我应该考虑什么?

c - 如何从文件中获取所有数字并将它们输入到数组中?