0

我在 codeigniter 中有一个函数,它从数据库中提取数据,检查它是否应该是销售项目,如果是,则添加销售属性。我有 2 个相同的函数来提取数据,一个用于前 20 个结果并通过 ajax 加载接下来的 20 个结果。

提取前 20 个结果的函数通过销售转换器运行良好,但提取接下来的 20 个结果的函数返回以下错误消息:

遇到 PHP 错误

严重性:警告

消息:为 foreach() 提供的参数无效

文件名:models/products_model.php

行号:15

这是销售转换器功能(错误导致第 15 行):

private function checkSalesProducts($query)
{
    
    $i=0;
    //print_r($query);
    if (sizeof($query) > 0) {
        echo "<script type=\"text/javascript\">alert('".print_r($query)."');</script>";
        foreach($query as $row)  //LINE 15
        {
        
            $row->sale = 0;
            ++$i;
        
        }
    
    }
    
    $this->db->select("sale_product, sale_discount");
    $salesItems = $this->db->get("sale_items");
    
    foreach($salesItems->result() as $salesItem)
    {
        
        for($i=0; $i<sizeof($query); ++$i)
        {
            
            if($salesItem->sale_product == $query[$i]->prod_id)
            {
            
                $query[$i]->prod_price = $query[$i]->prod_price * (1 - $salesItem->sale_discount);
                $query[$i]->sale = true;
                $query[$i]->sale_discount = $salesItem->sale_discount;
                break;
            
            }               
        
        }
    
    }
    
    return $query;
    
}

这是提取初始 20 的函数:

public function searchProducts($searchText)
{

    $this->db->select("prod_id");
    $this->db->where("MATCH (`prod_title`, `prod_desc`) AGAINST ('{$searchText}' IN BOOLEAN MODE)", "", false);
    $this->db->or_where("prod_id LIKE '%{$searchText}%'");
    $this->db->or_where("prod_sku LIKE '%{$searchText}%'");
    $this->db->or_where("prod_title LIKE '%{$searchText}%'");
    $this->db->or_where("prod_desc LIKE '%{$searchText}%'");
    
    $query = $this->db->get("product_search", 20);
    
    if($query->num_rows() == 0) return null;
    
    foreach($query->result() as $row)
    {
    
        $this->db->or_where("prod_id", $row->prod_id);
    
    }
    
    $query = $this->db->get("products");
    
    return $this->checkSalesProducts($query->result());
}

这是返回下一个 20 的函数:

public function loadMoreSearchProducts($searchText, $id)
{

    $this->db->select("prod_id");
    $this->db->where("(MATCH (`prod_title`, `prod_desc`) AGAINST ('{$searchText}' IN BOOLEAN MODE) OR prod_id LIKE '%{$searchText}%' OR prod_sku LIKE '%{$searchText}%' OR prod_title LIKE '%{$searchText}%' OR prod_desc LIKE '%{$searchText}%') AND `prod_id` < {$id}", "", false);
    $this->db->order_by("prod_id", "desc");
    
    $query = $this->db->get("product_search", 20);
    
    if($query->num_rows() == 0) return null;
    
    foreach($query->result() as $row)
    {
    
        $this->db->or_where("prod_id", $row->prod_id);
    
    }
    
    $query = $this->db->get("products");
    
    return $this->checkSalesProducts($query->result());
    
    /*$this->db->select("prod_id, prod_sku, prod_title, prod_desc, prod_price, prod_main_image");
    $this->db->where("(prod_sku LIKE '%{$searchText}%' OR prod_title LIKE '%{$searchText}%' OR prod_desc LIKE '%{$searchText}%') AND `prod_id` < {$id}");
    $this->db->order_by("prod_id", "desc");
    $query = $this->db->get("products", 20);
    
    return $this->checkSalesProducts($query->result());*/

}

这是导致错误的数组中前 3 个结果的示例:

Array
(
[0] => stdClass Object
    (
        [prod_id] => 5
        [prod_sku] => HY-FLY
        [prod_cat] => 68
        [prod_title] => Hy Guardian Fly Rug
        [prod_desc] => <p><font color="#ff0000" size="4">Our best selling Combo neck fly rug,</font><font color="#6e5d3f" size="2"> made from polyester silver close weave mesh giving maximum protection against all flying insects.</font></p>
        [prod_price] => 47.00
        [prod_weight] => 3000
        [prod_main_image] => Hydrophane-Guardian-fly-rug.jpg
        [prod_deliverable] => 1
    )

[1] => stdClass Object
    (
        [prod_id] => 6
        [prod_sku] => CITRONE
        [prod_cat] => 14
        [prod_title] => NAF Off Citronella Tags
        [prod_desc] => BRAND NEW TO THE MARKET FOR SUMMER 2012 - /NAF OFF Citronella Tags.
        [prod_price] => 9.10
        [prod_weight] => 200
        [prod_main_image] => NAF-Citronella-Tags.jpg
        [prod_deliverable] => 1
    )

[2] => stdClass Object
    (
        [prod_id] => 7
        [prod_sku] => FLARE-T
        [prod_cat] => 37
        [prod_title] => Liveryman Flare Trimmer 
        [prod_desc] => <p><font color="#6e5d3f" size="2">Very Quiet, Petit and Light Trimmer.</font></p>
        [prod_price] => 20.00
        [prod_weight] => 750
        [prod_main_image] => Liveryman-Flare-Trimmers.jpg
        [prod_deliverable] => 1
    )
)
4

2 回答 2

1

试试这个...

if(is_array($query))
{
    foreach($query as $row)  //LINE 15
    {
        $row->sale = 0;
        ++$i;
    }
}
于 2012-08-07T11:38:45.363 回答
0

$query不是数组或可迭代对象。结果是var_dump($query)什么?

于 2012-08-07T11:41:51.537 回答