1

I want to make codeigniter pagination with where db query.

My model: (update)

public function get_all_produk_row($url = ''){
  data = array();
  $this->db->where(array('kategori.url'=>$url,'produk.status_produk'=>'1'));
  $this->db->order_by('kategori_id');
  $this->db->join('kategori','kategori.id_kategori=produk.kategori_id');
  $this->db->num_rows();
  return $data;
}

But I got Fatal error: Call to undefined method CI_DB_mysql_driver::num_rows().

How can I solve it?


My database

Kategori:

id_kategori
kode_kategori
url

Produk:

id_produk
kategori_id
kode_produk
status_produk
Vinsens
  • 199
  • 6
  • 19
  • produce: `SELECT * FROM (produk) JOIN kategori ON kategori.id_kategori=produk.kategori_id WHERE kategori.url = '$url' AND produk.status_produk = '1' ORDER BY kategori_id`. Where I must put `num_rows()` code? – Vinsens Feb 13 '16 at 02:19
  • U need to use numrows after execute query function – devpro Feb 14 '16 at 20:22

3 Answers3

1

You can get no of rows after calling get() function as:

public function get_all_produk_row($url = '')
{ 
    $data = array(); 
    $this->db->where(array('kategori.url'=>$url,'produk.status_produk'=>'1')); 
    $this->db->join('kategori','kategori.id_kategori=produk.kategori_id'); 
    $this->db->order_by('kategori_id'); 
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {
         $data = $query->result_array();
    }
    return $data;
}
devpro
  • 16,184
  • 3
  • 27
  • 38
0

You are missing table name here

public function get_all_produk_row($url = '')
{     
  $data = $this->db->get_where("kategori",array('kategori.url'=>$url,'produk.status_produk'=>'1'))->num_rows(); 
 //or one method can be if you can write query 
 //$data = $this->db->query("select count(*) as total tbl_name")->row()->total; 
  return $data;
}
Praveen Kumar
  • 2,408
  • 1
  • 12
  • 20
0

Your query should be as below:-

$this->db->select('*');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();

OR

$this->db->select('*');
$this->db->where('whatever');
$query = $this->db->get('table');
$num = $query->num_rows();
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42