How to add pagination to union tables in codeignit

2019-08-04 12:38发布

问题:

How to add pagination to unionTable?

Usually i did something like this:

    $query = $this->db->get('tutorial', $num, $offset);

but i don`t know how to do it here help.

    $query = $this->db->query("SELECT * FROM
             ($subQuery1 UNION $subQuery2 UNION $subQuery3)
             AS unionTable GROUP BY date DESC");

回答1:

if you are using Mysql, try the following code:

$query = $this->db->query("SELECT * FROM
         ($subQuery1 UNION $subQuery2 UNION $subQuery3)
         AS unionTable GROUP BY date DESC LIMIT $num, $offset");

NOTICE: make sure that you have set the correct values to the variables $num and $offset before query.



回答2:

Ok, so you're using query instead of get, and you will have to, in addition to 600Mhz answer of adding $num and $offset previously calculated, to retrieve the values from the active record query, so, you'll have to execute the query:

$query = $this->db->query("SELECT * FROM
         ($subQuery1 UNION $subQuery2 UNION $subQuery3)
         AS unionTable GROUP BY date DESC LIMIT $num, $offset")
         ->result();

This way, it'll emulate the get function and $query will have the array of objects that you usually retrieve with get. If you already knew it, accept 600MHz answer, as it answered first, XD