Welcome Guest, Not a member yet? Register   Sign In
Amending result_array
#1

[eluser]CinoGenX[/eluser]
Hi @ all

In my model I'm trying to amend values in an array generated from a phpbb query before passing back to my controller....

i.e.
$sql = "SELECT post_id, post_subject, post_text, bbcode_uid, bbcode_bitfield, bbcode_options FROM phpbb_posts WHERE .......";

$query = $this->db->query($sql);

I now want to foreach through the results and change the post_text using the phpbb3 function "generate_text_for_display".

Can someone help me out on this please....banging my head on the table on this one.

Many thanks.
#2

[eluser]Aken[/eluser]
Code:
<?php

$query = $this->db->query($sql);

// Get an array of resulting rows.
$results = $query->result();

// Loop through the results array by reference
foreach ($results as &$row)
{
// Modify the "content" column for the current row
$row->content = generate_text_for_display($row->content);
}

// $results will now be amended
#3

[eluser]bretticus[/eluser]
[quote author="Aken" date="1354059587"]
Code:
<?php
//...
// Loop through the results array by reference
foreach ($results as &$row)
{
//...
[/quote]

Note the ampersand before $row in this example. In case you didn't know, that's called passing a variable by reference instead of by value. It just means that as each element of the original array ($results) is looped through, you are modifying $result's actual array element instead of just assigning $row as a value (or a copy.)
#4

[eluser]CinoGenX[/eluser]
That explains alot. Many thanks Guys, ill give it a try.

Im familiar with passing by reference from C++, but thats functions, didnt realise it was used like this in php for a foreach loop. Cheers for the explanation guys.
#5

[eluser]CinoGenX[/eluser]
hmmm having a little problem still.

Fatal error: Cannot use object of type stdClass as array...

This is what i have so far.

Code:
// in Model
public function get_recent_posts()
{
  
  $sql = "SELECT * FROM vw_recent_posts";
  $query = $this->db->query($sql);
  $results = $query->result();

  foreach ($results as &$row)
  {
   $row->post_text = 'TESTING';
   // testing the update
  }
  return $results;
}

// Then in controller:

public function home()
{

       $data['title'] = 'Home';
              $data['page'] = 'home_view';
       $data['news'] = $this->home_model->get_news();
       $data['recent_posts'] = $this->home_model->get_recent_posts();
       $this->page($data);
}

//page function just calls views passing the array

I think i need the $data['recent_posts'] to be result_array as $data has arrays inside array or something?

Help?! Sad

Thanks.
#6

[eluser]Aken[/eluser]
Chances are your view is expecting an array when looping through your recent posts. You need to change it to object syntax or use result_array() and change your get_recent_posts() to reflect that as well.
#7

[eluser]CinoGenX[/eluser]
Thanks for reply Aken.

Code:
Here is the view calls in controller :

public function page($pagedata)
{
      
  $this->load->view('templates/header', $pagedata);
  $this->load->view('templates/topnav');
  $this->load->view('pages/'.element('page',$pagedata), $pagedata);
  $this->load->view('pages/right_view',$pagedata);
  $this->load->view('templates/footer');
}

And this is the view:

Code:
<?php foreach ($recent_posts as $post_item): ?>
<div class="post">
  <p> <a href="http://localhost/forum/viewtopic.php?f=&lt;?php echo $post_item['forum_id'];?&gt;&t=&lt;?php echo $post_item['topic_id'];?&gt;#p&lt;?php echo $post_item['post_id'];?&gt;">    "&lt;?php echo word_limiter($post_item['post_subject'],2); ?&gt;"</a>
  </p>


</div><br />
&lt;?php endforeach ?&gt;

So how am i referrring to it as object, or how should i change to results_array()? Should i change $data in the controller (further up) to results_array or.....?
#8

[eluser]Aken[/eluser]
The problem is you aren't referring to it like an object. You're fetching array indexes in your view, when you should be fetching object properties. Each $post_item is an object as your code stands.
#9

[eluser]CinoGenX[/eluser]
Thanks Aken!!!

Updated view to call object properties (i.e. $post_item->topic_id etc) and it works.

Just out of interest, how would i have got it to work as array instead of object? I tried just changing:

$results = $query->result();
to
$results = $query->result_array();

....in the home_Model. But that didn't work.

What else would i have had to do?

Thanks again.
#10

[eluser]Aken[/eluser]
Using result_array() should've worked fine for your view code, but you would've needed to update your get_recent_posts() model method, also, so that your foreach() loop there used array notation also.




Theme © iAndrew 2016 - Forum software by © MyBB