Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:57:51+00:00 2026-06-09T04:57:51+00:00

I have a function in codeigniter which takes data extracted from the database, checks

  • 0

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
    )
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:57:54+00:00Added an answer on June 9, 2026 at 4:57 am

    try this…

    if(is_array($query))
    {
        foreach($query as $row)  //LINE 15
        {
            $row->sale = 0;
            ++$i;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which adds an item to the basket. In this function,
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have a preg_replace_callback which takes a closure (anonymous) function as 2nd parameter and
I have a Codeigniter controller which takes a full URL as the first argument,
I have a function in CodeIgniter which populates a list of days. I called
I have a Javascript function which should update a hidden input field in my
I have some CodeIgniter controllers which should only be accessed by users who have
I have a function in my model(codeigniter framework) which sets two values in cookies.
I have a validation function I'm using inside of codeigniter. function valid_image() { if
I have a model function that updates a user in my CodeIgniter application: //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.