从foreach中的两个表中收集信息


Collect information from two tables in foreaches

我在一个数据库表中有图像产品ID,在另一个表中有image Source。这些都被整理好了。一个订单中可以有多个图像。我正试着把每张照片发给Pwinty。

因此,对于每个图像,我都需要制作:

$photo = $pwinty->addPhoto($order, "$size", "$source", "$qty", "ShrinkToFit");
// I have not got to the size and qty variable yet
// Because of that I have this instead
$photo = $pwinty->addPhoto($order, "4x6", "$source", "1", "ShrinkToFit");

现在,因为它们在多个表中,我有以下代码:(这根本不会返回上面的照片数组。

// Get the customers pictures for this order 
foreach ($db->query("SELECT * FROM order_products WHERE order_id=$order_id") as $row)
$picture_info[] = $row;
if (count($picture_info) > 0):
    foreach ($picture_info as $row):
        $product_id = $row['product_id'];
        echo $product_id;
        foreach ($db->query("SELECT * FROM products WHERE product_id=$product_id") as $row)
        $picture_source[] = $row;
            foreach ($picture_source as $row):
                $source = $row['product_image'];
                echo $source;
                // add some photos
                $photo = $pwinty->addPhoto($order, "4x6", "$source", "1", "ShrinkToFit");

            endforeach; 
    endforeach;
endif;

它返回的是:

14138646283376c471632817da60f95964cb2d57dc46.png
Array
(
[id] => 7776
[address1] => 2002 E Blain
[address2] => 
[postalOrZipCode] => 68460
[country] => United States
[addressTownOrCity] => Belvidear
[recipientName] => Joe Dohn
[textOnReverse] => Photos by AlphaHQ
[stateOrCounty] => Kansas
[status] => NotYetSubmitted
[payment] => 
[paymentUrl] => 
[photos] => Array
    (
    )
[documents] => Array
    (
    )
[stickers] => Array
    (
    )
    )

看到图像数组是空的吗?我需要修复什么?

优化注意:您最好从select语句中列出您想要的内容,而不是说select*。SQL将更快地执行

你能给我们看看你从所有回声中得到的输出吗?

此外,当您将东西作为测试进行回显时,最好放入一些文本来描述您正在回显的内容,这样即使它设置为NULL,您也会看到文本,现在该变量为空。

现在看来

foreach ($db->query("SELECT * FROM products WHERE product_id=$product_id") as $row)

实际上没有得到任何行。

你可以做的一个简单的测试是

echo "My row is " + $row + "'n";

因此,您可以看到实际得到的行数。