Basically, I have a string of functions that load a set of blog entries to build the page. At the bottom of the page I have a link that initiates a jQuery Ajax to an Ajax controller that uses the exact same string of functions to load more blog entries with a different offset.
I have two queries initially in one of the functions. One that counts how many rows exist total, and the main function that returns the data for 10 or less rows called. I’m getting all of the data, except for the main blog entry data.
Here’s most of the code, I cut out everything else in the functions that isn’t relevant:
Main view controller
public function blog() {
$this->load->helper('date');
$this->load->model('blog_model');
$result = $this->blog_model->process_uri(0);
$this->load->view($result['view'],$result);
}
Ajax controller
public function blog_data() {
$this->load->helper('date');
$this->load->model('blog_model');
//$offset = $this->input->post('offset');
$data = $this->blog_model->process_uri(0);
echo json_encode($data);
}
Model
public function process_uri($offset) {
$result;
$uri = $this->uri;
// There's more this function, but I cut it out.
$result = array('type' => 'list');
return $this->blog_model->get_blog_data($result,$offset);
}
Where I think the problem lies
private function get_blog_data($data,$offset) {
$return = array('type' => $data['type']);
$limit;
$select;
if (false) {
}
else {
$return['view'] = 'blog_view';
$this->db->select('id');
$r = $this->db->get('blog');
$return['total'] = $r->num_rows() - ($offset + 10);
$return['current'] = $offset + 10; // This returns
$limit = 10;
$select = 'blog.id,title,type,date,message,images';
$this->db->order_by('blog.id','desc');
if (isset($data['date'])) $this->db->like('date',$data['date'],'after');
}
$this->db->select($select);
$this->db->from('blog');
$this->db->join('blog_img','blog_img.id = blog.id','left');
$this->db->limit($limit,$offset);
// This does not return any data in the Ajax call.
$return['entries'] = $this->blog_model->process_data($this->db->get());
return $return;
}
private function process_data($data) {
foreach($data->result() as $i => $row) {
//Formats the data
}
}
The return data I get in JavaScript is:
{"type":"list","view":"blog_view","total":5,"current":10,"entries":[]}
As you can see, even if I pass identical data through the Ajax controller, the entries array is still empty. I’m not too familiar with CodeIgniter, and especially using Ajax with it, so I would greatly appreciate if anyone could help with this, as it’s really killing me.
Also, I know the problem is not in the JavaScript code, because I tried adding some static data in the process_data function to see if it would show up and it does, hence that is the reason why I didn’t add the JavaScript code.
After some searching around I found the last_query codeigniter function which returns the last query, so I could see what was going wrong.
And here’s the query that was being run through AJAX
I was eventually able to find the problem