Welcome Guest, Not a member yet? Register   Sign In
Pagination issue [Message: Invalid argument supplied for foreach()]
#1

[eluser]EdgeTube[/eluser]
Hi all,

I am attempting to integrate the pagination library with my site, except I'm having this error of invalid argument. It seems like no data is being passed to the view?

Here's my code:

Model:
Code:
<?
class Proddb extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

    public function record_count()
    {
        return $this->db->count_all('products');
    }

    public function fetch_products($limit,$start,$new_str)
    {
        $query = $this->db->query("SELECT * FROM products WHERE `category` = '$new_str' LIMIT $limit, $start");
        
        if($query->num_rows()>0)
        {
            foreach ($query->result() as $row)
            {
                $datas[] = $row;
            }
    
            return $datas;
        }
        else
        {
            return FALSE;
        }
    }
    
}
    ?>

Controller:
Code:
$new_cat =  str_replace(" "," ", $category);
        $new_str = urldecode($new_cat);
        $data['category'] = $new_str;
        
        $config['base_url'] = base_url() . 'category/index';
        $config['total_rows'] = $this->Proddb->record_count();
        $config['per_page'] = 5;
        $config['uri_segment'] = 4;
        $this->pagination->initialize($config);

        $page = ($this->uri->segment(4))? $this->uri->segment(4) : 0;
        $data['results'] = $this->Proddb->fetch_products($config['per_page'],$page, $new_str);
        $data['links'] = $this->pagination->create_links();
        $this->load->view('category/view',$data);

View:
Code:
<?

foreach ($results as $row_sub):
   echo "".$row_sub->title."";
endforeach;


?><?=$links?>

Not sure what I'm doing wrong with the pagination.

Here's the error:

Code:
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: category/view.php

Some advice on how to fix the issue would be appreciated. I bet there are a lot of errors, but forgive my first time trying to understand this pagination library!

Thanks!
#2

[eluser]InsiteFX[/eluser]
Code:
foreach ($query->result_array() as $row)

InsiteFX
#3

[eluser]EdgeTube[/eluser]
hmm, that gives me this error:

Code:
Fatal error: Call to a member function result_array() on a non-object...
#4

[eluser]osci[/eluser]
in your model

Code:
if ($query->num_rows > 0)
{
  return $query->result();
} else {
  return false;
}

and in your view since you might return false.

Code:
if (isset($records) & ($records <> NULL)) {
   foreach ($results as $row_sub):
      echo "".$row_sub->title."";
   endforeach;
}
#5

[eluser]EdgeTube[/eluser]
Great thanks!

Just realized my other mistake.

In the line

Code:
$data['results'] = $this->Proddb->fetch_products($config['per_page'],$page, $new_str);

It should be


Code:
$data['results'] = $this->Proddb->fetch_products($page, $config['per_page'], $new_str);

since I want to get $config['per_page'] records starting from $page.




Theme © iAndrew 2016 - Forum software by © MyBB