Phil Young
Phil Young

Reputation: 1354

Valid array for foreach loop?

I have a function in codeigniter which takes data extracted from the database, checks if it should be a sales item and adds sales properties if it is. I have 2 identical functions that extract data, one for the first 20 results and to load the consequent next 20 results via ajax.

The function which extracts the first 20 results runs through the sales converter fine, but the function which extracts the next 20 results returns the following error message:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: models/products_model.php

Line Number: 15

Here is the sales converter function (with the error causing line 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;
    
}

Here is the function which extracts the initial 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());
}

And here is the function which returns the next 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());*/

}

Here is an example of the first 3 result in the array which is causing the error:

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
    )
)

Upvotes: 0

Views: 1043

Answers (2)

Mark
Mark

Reputation: 1852

$query isn't an array or iterable object. What is the result of var_dump($query) ?

Upvotes: 0

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

try this...

if(is_array($query))
{
    foreach($query as $row)  //LINE 15
    {
        $row->sale = 0;
        ++$i;
    }
}

Upvotes: 1

Related Questions