• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
me failing with $query->result()

#1
[eluser]devvee[/eluser]
Hi!

I think I have a fairly simple issue that I just can't seem to solve due to inexperience.

I have a model, a controller and a view. The controller loads the model and uses a function.

Code:
$this->load->model('Question_model', '', TRUE);
$data['query'] = $this->Question_model->get_open_questions();

The function inside the model performs a query (which I simplified, since that one isn't causing the problem) and returns the result.

Code:
$query = $this->CI->db->query("SELECT * FROM table");
return $query->result();

The query gets executed properly and even $query->result() doesn't cause any issues (I took this method straight from the model page in the user guide).

Now the results are in the controller in $data['query']. The controller now opens the view:

Code:
$this->load->view('home_view', $data);

Now my big question is.. how do I loop through the results on the view page? Of course "foreach($query->result() as $row):" won't work, because the result has already been returned and I'm quite clueless.

Thanks in advance Smile

#2
[eluser]n0xie[/eluser]
[quote author="devvee" date="1241723328"]Hi!
Code:
$this->load->model('Question_model', '', TRUE);
$data['query'] = $this->Question_model->get_open_questions();
[/quote]

Your resultset is stored in the $data array as 'query'. The load->view automatically serializes this for you so your resultset is available through $query inside your view.

Try this from your view:
Code:
var_dump($query);

It should return the full resultset. From there you can use any iteration you prefer to extract the data you want. The most used would be something like this (depending on what type of dataset you returned:

Code:
foreach ($query as $row)
{
  echo $row['somefield'];
  // or if you returned an object
  echo $row->somefield;
}

#3
[eluser]Evil Wizard[/eluser]
depending on how the result is passed into the view in the data array, e.g.
Code:
$data['question_results'] = $query->result();

Then in the view you would itterate through the array just like you normally would.

Code:
foreach($question_results as $question) {
         ... //processing here
    }

alternatively you could reset the array pointer and walk through the array.

#4
[eluser]the_namdeeW[/eluser]
I would do...

IN YOUR CONTROLLER:
foreach ($query as $row)
{
$data['somefield'] = $row->somefield;
}
$this->load->view('some_view', $data);

IN YOUR VIEW:
<?=$somefield?>

This will gather all your data from your loop attaching it to $data[]. Then you just echo it out where ever seems fit.

Cheers.

#5
[eluser]devvee[/eluser]
Thanks for the responses! I actually tried the answer given in the first reply before and it didn't work, but that's probably due to a typo on my end because it works like a charm now Smile

[quote author="Skootz 25" date="1241730521"]I would do...

IN YOUR CONTROLLER:
foreach ($query as $row)
{
$data['somefield'] = $row->somefield;
}
$this->load->view('some_view', $data);

IN YOUR VIEW:
<?=$somefield?>

This will gather all your data from your loop attaching it to $data[]. Then you just echo it out where ever seems fit.

Cheers.[/quote]
You'd still need some kind of loop when multiple rows are returned, but it's definitely a nice option if you only need one row of information Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.