修改并重做codeigniter中的最后一个查询

Can I get last query made with active record class and modify some parts of it and redo that query in codeigniter. I am trying to get total pages for my pagination. I can't directly write my query because it is dynamically created on runtime within some for loops and if statements depending on the form submitted to the page.

Do you run your query twice because of the total pages? If yes, check SQL_CALC_FOUND_ROWS and FOUND_ROWS() in SQL.

$this->db->select("SQL_CALC_FOUND_ROwS id",false);
$this->db->any_AR_function(....);
$this->db->limit(...);
$result = $this->db->get();

After this you can call this:

$this->db->select("FOUND_ROWS() as count",false);
$count = $this->db->get();

I hope this is what you want.

You can get last query using:

$last_query = $this->db->last_query();

You can modify this query and then do:

$query = $this->db->query($modified_query);
foreach($query->result() as $row)
{

}