Welcome Guest, Not a member yet? Register   Sign In
Pagination using two functions, is it possible just to get it down to one?
#1

[eluser]Otemu[/eluser]
Hi,

I have two calls to the model class for my pagination to work, but I was wondering if I could get this down to just one function.

The first function queries the database and returns the number of rows
Code:
function getNewsAllN() {
    $data = array();
    $this->db->select('articles.articleid as newsid, articles.template, articles.intro_text, articles.page_title, articles.page_url, articles.display, images.articleid, images.imageurl, images.description');
    $this->db->from('articles', 'images');
    $this->db->join('images', 'articles.articleid = images.articleid','left');
    $this->db->where('articles.parentid', 2);
    $this->db->where('articles.template', 'default_article');
    $this->db->where('articles.display', 'yes');
    $Q = $this->db->get();
    $total = $Q->num_rows();
    $data = $total;
    $Q->free_result();
    return $data;
    }

and the second function queries the database and returns the rows.
Code:
function getNewsAll($num, $offset) {
    $data = array();
    $this->db->select('articles.articleid as newsid, articles.template, articles.intro_text, articles.page_title, articles.page_url, articles.display, images.articleid, images.imageurl, images.description');
    $this->db->from('articles', 'images');
    $this->db->join('images', 'articles.articleid = images.articleid','left');
    $this->db->where('articles.parentid', 2);
    $this->db->where('articles.template', 'default_article');
    $this->db->where('articles.display', 'yes');
    $this->db->limit($num);
    $this->db->offset($offset);
    $Q = $this->db->get();
    $Q->num_rows();
    if ($Q-> num_rows() > 0){
        foreach ($Q-> result_array() as $row){
            $data[] = $row;
            }
        }
        
    $Q->free_result();
    return $data;
    }
The Controller then uses both these functions for my pagination to work
Code:
$numrows = $this->M_Articles->getNewsAllN();
$config['total_rows'] = $numrows;
$data['records'] = $this->M_Articles->getNewsAll($config['per_page'],$this->uri->segment(3));

is it possible some how to narrow this just to one function call?
#2

[eluser]Jareish[/eluser]
Your $data['records'] is an array. And like any array in php, you can count the number of objects in it using count();

Think $numrows = count($data['records']); should do it
#3

[eluser]n0xie[/eluser]
[quote author="Jareish" date="1290711958"]Your $data['records'] is an array. And like any array in php, you can count the number of objects in it using count();

Think $numrows = count($data['records']); should do it[/quote]
No. that would just count your 'per_page' number.

You can't do it without using 2 queries afaik.
#4

[eluser]Otemu[/eluser]
Thanks for the reponse's guys, that helps a lot




Theme © iAndrew 2016 - Forum software by © MyBB