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
2
  • 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? Commented Feb 13, 2016 at 2:19
  • U need to use numrows after execute query function Commented Feb 14, 2016 at 20:22

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

I got Error Number: 1064. SELECT COUNT(*) AS numrows WHERE kategori.url = 'baterai' AND produk.status_produk = '1'
Your query should be :- SELECT COUNT(*) AS numrows FROM TableName WHERE kategori.url = 'baterai' AND produk.status_produk = '1' . You have missed TableName
I using array where codeigniter. And I got that produce
Check my second portion of answer.
Thanks dude, but what I should fill 'table' while I have 2 tables here?
|
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;
}

3 Comments

The table name is: kategori and produk. I want count how much row I get from this where query
How these 2 table's are connected to each other ?
i mean is there any common column name ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.