2

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!

4
  • Try using this $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 be return not $return Commented Apr 29, 2015 at 12:44
  • Thanks for your quick reply, however I don't think it has anything to do with my queries, hence the error message I get. I think it's because it's not an array that I pass to the view? Commented Apr 29, 2015 at 12:46
  • I guess you need = after $return or may be you need return only without $ Commented Apr 29, 2015 at 12:48
  • I had a really dumb mistake.. #return instead of return Commented Apr 29, 2015 at 12:57

3 Answers 3

3

return is wrong in your code remove $ from your return type

$return array('categories' => $query1, 'count' => $query2);

it should be

return array('categories' => $query1, 'count' => $query2);
Sign up to request clarification or add additional context in comments.

2 Comments

Wow that was dumb of me haha! Can you take a look into my View code? I need to also get data out of the $results['count'], but another foreach won't fix the problem cause it needs to get printed on the same table row.
@StephanB your $data array contain your array so you pass $data into view not $result $this->load->view('Categories_view',$data);
1

Running Transactions:
To run your queries using transactions you will use the $this->db->trans_start() and $this->db->trans_complete() functions as follows:

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();

You can run as many queries as you want between the start/complete functions and they will all be committed or rolled back based on success or failure of any given query. Click here to see more

Comments

0
$query['categories'] = $this->db->get('Categories')->result_array();
$query['count']  = $this->db->query("SELECT COUNT(*) AS rowCount FROM Posts")->result();

return $query;

Try this. Not done anything great. Just used result_array() and result(). I hope they are compatible with your version of CI!!

2 Comments

@saty fixed the (dumb) problem. Although I can't seem to find out how to get data out of the $results['count'] (in the view), I think another foreach won't fix the problem cause it needs to get printed on the same table row.
Looking at your code the categories will be present in $categories and the count in $count. You can do print_r($categories) or print_r($count) and check it in your view!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.