I'm trying to get my model to return two queries, one for the data itself which are multiple records from the table 'Categories', and a count field, from the table 'Posts'.
I've tried alot of possible solutions, but so far none fixed it for me.
My view does work with the correct route /forum/$id, but loading the categories and a post count per category won't work.
Categories Controller:
$this->load->database();
$this->load->model("Categories_model");
$results=$this->Categories_model->getCategories();
$data=array('results'=>$results);
$this->load->view('Categories_view',$results);
Categories Model:
// this works if I only want to get all the categories
// $query=$this->db->get('Categories');
// return $query->result();
$query1 = $this->db->get('Categories');
$query2 = $this->db->query("SELECT COUNT(*) AS rowCount FROM Posts");
$return array('categories' => $query1, 'count' => $query2);
Categories View:
<tbody>
<?php foreach ($results['categories'] as $r):?>
<tr>
<td><a href="<?php echo site_url('forum'); ?>/<?=$r->CategorieId?>"><?=$r->Name?></a></td>
<td><?=$r->rowCount?></td>
</tr>
<?php endforeach;?>
</tbody>
When I load the Categories view, I get this error:
syntax error, unexpected 'array' (T_ARRAY), Filename: models/Categories_model.php
Can somebody help me with some sample code on how to do this, or a fix for my current code?
Thank you in advance!
$query1 = $this->db->get('Categories')->result();$query2 = $this->db->query("SELECT COUNT(*) AS rowCount FROM Posts")->result();Also include the full error message. and should bereturnnot$return=after$returnor may be you needreturnonly without$