PHP多维数组返回空子数组

时间:2015-05-13 17:29:59

标签: php mysql arrays multidimensional-array opencart

我正在使用php使用值数组从mysql数据库中提取数据。

我的初始数组$ models是一个零件编号列表,如下所示:

 Array ( 
         [0] => AB-1 
         [1] => AB-2 
         [2] => BCA-2 
         [3] => DE3 
 ) 

以下是设置数据的foreach,以便我可以在模板页面上显示产品:

$products = array();

 foreach ($models as $key => $prod_num) {
        $product_info = $this->model_extension_news->getModelInfo($prod_num);
        if ($product_info['image']) {
                            $image = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_wishlist_width'), $this->config->get('config_image_wishlist_height'));
                        } else {
                            $image = false;
                        }

                        if ($product_info['quantity'] <= 0) {
                            $stock = $product_info['stock_status'];
                        } elseif ($this->config->get('config_stock_display')) {
                            $stock = $product_info['quantity'];
                        } else {
                            $stock = $this->language->get('text_instock');
                        }

                        if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
                            $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                        } else {
                            $price = false;
                        }
            $products[] = array(
                            'product_id' => $product_info['product_id'],
                            'thumb'      => $image,
                            'name'       => $product_info['name'],
                            'model'      => $product_info['model'],
                            'stock'      => $stock,
                            'price'      => $price,     
                            'href'       => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),

                        );

                }   

我访问数据库以从名为getModelInfo()的函数中找到$ product_id和其他信息(我正在使用opencart框架):

public function getModelInfo($prod_num){
    $query = $this->db->query("SELECT p.product_id, pd.name AS name, p.model AS model, p.image, p.price, p.quantity FROM " . DB_PREFIX ."product p LEFT JOIN ".DB_PREFIX."product_description pd ON p.product_id = pd.product_id WHERE p.model = '".$prod_num."'");
         return array(
                        'product_id'       => $query->row['product_id'],
                        'name'             => $query->row['name'],
                        'model'            => $prod_num,
                        'image'            => $query->row['image'],
                        'price'            => $query->row['price'],
                        'quantity'         => $query->row['quantity'],
                         );
    }

问题是我的返回数组不完整。第一个结果已填写,但附加结果将从数据库返回空白信息。我知道数据库中的每个产品都有数据......我无法理解为什么我的$ products数组中的最后3个产品没有显示数据:

Array ( 
        [0] => Array ( 
              [product_id] => 18713 
              [thumb] => http://www.website.com/image/AB-1.jpg 
              [name] => AB-1 FULL 
              [model] => AB-1 
              [stock] => 79 
              [price] => $168.00 
              [href] => http://www.website.com/index.php?route=product/product&product_id=18713 
        ) 
         [1] => Array ( 
               [product_id] => 
               [thumb] => 
               [name] => 
               [model] => AB-2 
               [stock] => 
               [price] => 0 
               [href] => http://www.website.com/index.php?route=product/product&product_id= 
        ) 
         [2] => Array ( 
               [product_id] => 
               [thumb] => 
               [name] => 
               [model] => BCA-2
               [stock] => 
               [price] => 0 
               [href] => http://www.website.com/index.php?route=product/product&product_id= 
        ) 
         [3] => Array ( 
               [product_id] => 
               [thumb] => 
               [name] => 
               [model] => DE-3 
               [stock] => 
               [price] => 0 
               [href] => http://www.website.com/index.php?route=product/product&product_id= 
        ) 
)

我在这里缺少什么?我在foreach做错了吗?有没有其他方法来设置$产品?我似乎无法看到这段代码出了什么问题。任何帮助(或重定向到此问题的解决版本)表示赞赏!

1 个答案:

答案 0 :(得分:0)

您是否有可能缺少某些产品的产品说明?在这种情况下,您的“LEFT JOIN”不会返回该产品的结果。您需要使用:“LEFT OUTER JOIN”(请点击此链接了解它的作用:SQL LEFT OUTER JOIN)。

iv'e在你的查询中添加了它:

$query = $this->db->query("
SELECT 
    p.product_id, pd.name AS name, p.model AS model, p.image, p.price, p.quantity 

FROM 
    " . DB_PREFIX ."product p 

LEFT JOIN
    ".DB_PREFIX."product_description pd ON p.product_id = pd.product_id WHERE p.model = '".$prod_num."'");