我从这页PHP/mySQL - how to fetch nested rows into multidimensinal array开始回答,因为我正在使用表格之间的相同关系。我的代码现在看起来像这样:
$query=sprintf("SELECT * FROM product INNER JOIN production ON product.ID = production.product_ID");
$data = mysql_query($query);
$products = array();
while ($row = mysql_fetch_assoc($data))
{
if (!isset($products[$row['product_ID']]))
{
$row['production'] = array();
$products[$row['product_ID']] = $row;
}
$products[$row['product_ID']]['production'][] = $row;
}
foreach ($products as $product)
{
foreach ($product['production'] as $item)
//... do stuff
}
查询是可靠的 - 我已经在数据库上直接尝试了这个。 我的问题是,我无法解决它所说的最后一点......做的事情 无论我在哪里等变量组合,我都会得到ArrayArrayArray(等)。我想要的是得到一个我可以使用的输出表。 我希望这是足够的信息,这是我在StackOverflow上的第一个问题。谢谢。
当我执行var_dump($ item)时,我得到一个长显示,如下所示:
array(9) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(1) "4" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(3) "130" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "21" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "705" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "23" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(2) "23" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "26" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(2) "23" } array(8) { ["ID"]=> string(1) "2" ["product_name"]=> string(10) "Drums 12kg" ["order"]=> string(2) "20" ["hidden"]=> string(1) "0" ["id"]=> string(2) "28" ["product_ID"]=> string(1) "2" ["date"]=> string(10) "2012-07-19" ["quantity"]=> string(2) "12" } array(9) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(1) "5" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "10" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "10" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "10" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "14" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-12" ["quantity"]=> string(3) "140" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "16" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-12" ["quantity"]=> string(3) "220" } array(8) { ["ID"]=> string(1) "3" ["product_name"]=> string(12) "Nibbles 12kg" ["order"]=> string(2) "65" ["hidden"]=> string(1) "0" ["id"]=> string(2) "17" ["product_ID"]=> string(1) "3" ["date"]=> string(10) "2012-07-04" ["quantity"]=> string(3) "120" } array(9) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(1) "6" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "20" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(2) "11" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "20" } array(8) { ["ID"]=> string(1) "4" ["product_name"]=> string(16) "Breast meat 12kg" ["order"]=> string(2) "40" ["hidden"]=> string(1) "0" ["id"]=> string(2) "13" ["product_ID"]=> string(1) "4" ["date"]=> string(10) "2012-10-10" ["quantity"]=> string(2) "75" } array(9) { ["ID"]=> string(1) "5" ["product_name"]=> string(11) "Value Birds" ["order"]=> string(2) "50" ["hidden"]=> string(1) "0" ["id"]=> string(1) "7" ["product_ID"]=> string(1) "5" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "40" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "5" ["product_name"]=> string(11) "Value Birds" ["order"]=> string(2) "50" ["hidden"]=> string(1) "0" ["id"]=> string(2) "12" ["product_ID"]=> string(1) "5" ["date"]=> string(10) "2012-09-07" ["quantity"]=> string(2) "40" } array(9) { ["ID"]=> string(1) "1" ["product_name"]=> string(11) "Thighs 12kg" ["order"]=> string(3) "105" ["hidden"]=> string(1) "1" ["id"]=> string(1) "8" ["product_ID"]=> string(1) "1" ["date"]=> string(10) "2012-10-01" ["quantity"]=> string(3) "500" ["production"]=> array(0) { } } array(9) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "29" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" ["production"]=> array(0) { } } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "30" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "31" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-19" ["quantity"]=> string(3) "500" } array(8) { ["ID"]=> string(1) "7" ["product_name"]=> string(19) "Whole birds size 13" ["order"]=> string(2) "70" ["hidden"]=> string(1) "1" ["id"]=> string(2) "32" ["product_ID"]=> string(1) "7" ["date"]=> string(10) "2012-07-13" ["quantity"]=> string(3) "500" }
答案 0 :(得分:0)
您将在最后两个foreach循环中构建表的html。我建议更好地命名列,因为标识项目中数组键的文本令人困惑(您有三个ID字段)。
print "<table><thead><tr><th>ID</th><th>Product</th><th>Order</th><th>Hidden</th><th>ID</th><th>Product ID</th><th>date</th><th>Quantity</th></tr></thead><tbody>";
foreach ($products as $product)
{
foreach ($product['production'] as $item)
{
print "<tr>";
print "<td>$item['ID']</td>";
print "<td>$item['product_name']</td>";
print "<td>$item['order']</td>";
print "<td>$item['hidden']</td>";
print "<td>$item['id']</td>";
print "<td>$item['product_id']</td>";
print "<td>$item['date']</td>";
print "<td>$item['quantity']</td>";
print "</tr>";
}
}
print "</tbody></table>";