【问题标题】:Building a photo array from database tables从数据库表构建照片数组
【发布时间】:2013-12-12 17:55:35
【问题描述】:

所以我几个月来一直在努力解决这个问题,并希望在这里找到一劳永逸的解决方案。我使用phppwinty 作为pwinty.com 的php 库。

在作者提供的默认示例中,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");

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

现在 $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;

我希望我的描述性足够好,最终可以一劳永逸地解决这个该死的问题。提前感谢您的帮助,希望我们能共同制定解决方案。编码愉快。

如果我可以提供更多您认为对您有帮助的信息,请在下方留言索取信息。

【问题讨论】:

    标签: php mysql sql arrays foreach


    【解决方案1】:

    $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;
    }
    

    这应该会生成一个多维数组,其中您的 DB 行位于 $order_variables

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

    编辑

    仔细查看您的查询,我相信部分问题在于您的变量值没有用单引号括起来。这可能对您更有效。 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'");
    

    【讨论】:

    • 谢谢 :-),我碰巧看到了这个问题。抽奖的好运气。
    • 查询是否像我的$order_id 一样尝试使用$product_id?产品 ID 不像订单 ID 那样预先填充。
    • 我问的原因是因为我收到了这个错误:Fatal error: Call to a member function fetch_array() on a non-object in /home/www/alphahq.org/libraries/phppwinty/print.php on line 39
    • 根据您的查询和预期的分配方法,看起来您的 $product_id 变量应该来自 order_products 表中的 product_id 字段
    • 在这种情况下,您的查询可能有问题。您可以在 sql 客户端或 phpMyAdmin 中运行查询吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2010-11-05
    • 2015-07-04
    • 1970-01-01
    • 2022-10-12
    • 2020-07-09
    相关资源
    最近更新 更多