从数据库表构建照片阵列


Building a photo array from database tables

所以我已经为此斗争了几个月,希望能一劳永逸地找到解决方案。我使用phppwinty作为pwinty.com.的php库

在作者提供的默认示例中,创建的图像数组与下面的示例类似,所有变量都是硬编码的。

$photo1 = $pwinty->addPhoto($order, "4x6", "http://www.picisto.com/photos/picisto-20120608100135-925870.jpg", "1", "ShrinkToFit");
$photo2 = $pwinty->addPhoto($order, "6x6", "http://www.picisto.com/photos/picisto-20120601010631-895061.jpg", "4", "ShrinkToFit");
$photo3 = $pwinty->addPhoto($order, "5x7", "http://www.picisto.com/photos/picisto-20120626210525-763773.jpg", "3", "ShrinkToFit");

(注意:在训练中,我发现我们不一定需要在照片后面添加整数。它可以是photo,而不是:photo1。)

现在$order变量可以保留了。其次是尺寸、图像来源和数量。这是我目前唯一担心的3个变量。不幸的是,我的购物车把这些东西分成了3张不同的桌子。

-- order_options -- The table for the size variable
   --option_value -- The column for the size variable 
   --order_id -- Relationship column 
-- order_products -- The table for the product id's and qty in the order 
   -- product_id -- The column for the product id variable 
   -- product_quantity -- The column for the quantity variable
   -- order_id -- Relationship column 
-- products -- The table for the image source variable 
   -- product_image -- The column for the image source variable 
   -- product_id -- Relationship column 

我使用get变量将order_id获取到脚本中:$order_id = $_GET['id'];

我试图将所有的表连同关系一起JOIN,但除了错误什么都没有得到。虽然我不认为问题出在JOIN中,但我设置foreach的方式。

我的JOINforeach现在看起来是这样的:

foreach ($db->query("SELECT orderproducts.product_id, orderoptions.option_value, products.product_image FROM order_products as orderproducts INNER JOIN order_options as    orderoptions ON orderproducts.order_id = orderoptions.order_id INNER JOIN products as products ON orderproducts.product_id = products.product_id WHERE              orderproducts.order_id = $order_id AND orderproducts.product_id = $product_id") as $row)
    $order_variables[] = $row;
if (count($order_variables) > 0): 
    foreach ($order_variables as $row):
        $product_id = $row['product_id'];
        $product_quantity = $row['product_quantity'];
        $size = $row['option_value'];
        $product_source = "https://www.alphahq.org/uploads/images/".$row['product_image']."";
        $photo = $pwinty->addPhoto($order, $size, $product_source, $product_quantity, "ShrinkToFit");
    endforeach;
endif;

我之所以说我认为问题出在如何设置foreach上,是因为当我运行脚本时,我在浏览器中遇到的1个错误如下:

Warning: Invalid argument supplied for foreach() in /home/www/alphahq.org/libraries/phppwinty/print.php on line 35

根据我的代码编辑器,第35行是上面的foreach语句。

    foreach ($db->query("SELECT orderproducts.product_id, orderoptions.option_value, products.product_image FROM order_products as orderproducts INNER JOIN order_options as    orderoptions ON orderproducts.order_id = orderoptions.order_id INNER JOIN products as products ON orderproducts.product_id = products.product_id WHERE              orderproducts.order_id = $order_id AND orderproducts.product_id = $product_id") as $row)
    $order_variables[] = $row;

我希望我有足够的描述性,最终能一劳永逸地解决这个该死的问题。感谢您提前提供的帮助,希望我们能一起解决问题。快乐的编码。

如果我能提供更多你认为有帮助的信息,请在下面友好地评论,请求提供信息

$db->query()将返回一个结果集。除非您在query()方法中执行某些操作,否则您使用的是资源,而不是对象或数组。foreach需要一些可以迭代的东西。

您是否考虑过使用:

$result = $db->query("SELECT orderproducts.product_id, orderoptions.option_value, products.product_image FROM order_products as orderproducts INNER JOIN order_options as orderoptions ON orderproducts.order_id = orderoptions.order_id INNER JOIN products as products ON orderproducts.product_id = products.product_id WHERE orderproducts.order_id = $order_id AND orderproducts.product_id = $product_id");
$order_variables = array();
while($row = $result->fetch_array()) {
    $order_variables[] = $row;
}

这应该会在$order_variables中生成一个包含DB行的多维数组。

(请注意,我在示例中使用了MySQLi的OOP版本,我不确定这是否是您正在使用的。如果不是,请根据需要进行修改。)

编辑

仔细查看您的查询,我认为问题的一部分是您的变量值没有用单引号括起来。这可能对你更有效。MySQL喜欢单引号中的值(尤其是非数字值):

$result = $db->query("SELECT orderproducts.product_id, orderoptions.option_value, products.product_image FROM order_products as orderproducts INNER JOIN order_options as orderoptions ON orderproducts.order_id = orderoptions.order_id INNER JOIN products as products ON orderproducts.product_id = products.product_id WHERE orderproducts.order_id = '$order_id' AND orderproducts.product_id = '$product_id'");