我在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
)
)
答案 0 :(得分:1)
试试这个......
if(is_array($query))
{
foreach($query as $row) //LINE 15
{
$row->sale = 0;
++$i;
}
}
答案 1 :(得分:0)
$query
不是数组或可迭代对象。 var_dump($query)
的结果是什么?