繁体   English   中英

从数据库表构建照片阵列

[英]Building a photo array from database tables

因此,我已经为此奋斗了几个月,希望能在这里找到一个解决方案。 我使用phppwinty作为一个PHP库pwinty.com

在作者提供的默认示例中,像下面的示例一样创建了images数组,所有变量都进行了硬编码作为示例。

$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可以只是photo 。)

现在,$ order变量可以保留了。 接下来是尺寸,图像来源和数量。 这些是我目前担心的仅有的三个变量。 有了购物车,我很遗憾地将这些细分成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

(请注意,我在示例中使用的是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'");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM