Php 从数据库表构建照片数组
所以我已经为此奋斗了几个月,我希望在这里找到一个一劳永逸的解决办法。我将其用作的php库 在作者提供的默认示例中,图像数组的创建与下面的示例类似,所有变量都以硬编码为例Php 从数据库表构建照片数组,php,mysql,sql,arrays,foreach,Php,Mysql,Sql,Arrays,Foreach,所以我已经为此奋斗了几个月,我希望在这里找到一个一劳永逸的解决办法。我将其用作的php库 在作者提供的默认示例中,图像数组的创建与下面的示例类似,所有变量都以硬编码为例 $photo1 = $pwinty->addPhoto($order, "4x6", "http://www.picisto.com/photos/picisto-20120608100135-925870.jpg", "1", "ShrinkToFit"); $photo2 = $pwinty->addPhoto(
$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\u id放入脚本中:$order\u id=$\u get['id']代码>
我尝试了将所有表与关系连接在一起,但只得到了错误,尽管我不认为问题出在连接中,但我设置了foreach
的方式
我的JOIN
和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;
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\u 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'");
谢谢:-),我碰巧看到问题出现了。运气好。查询是否正试图使用$product\u id
与我的$order\u id
一样?产品id没有像订单id那样预先填充。我询问的原因是因为我收到了以下错误:致命错误:根据您的查询和指定方法,在第39行的/home/www/alphahq.org/libraries/phppwinty/print.php中调用非对象上的成员函数fetch_array(),看起来您的$product\u id
变量应该来自order\u products
表中的product\u id
字段。在这种情况下,您的查询可能有问题。您可以在sql客户端或phpMyAdmin中运行查询吗?