Welcome Guest, Not a member yet? Register   Sign In
Changing db results in model before passing
#7

[eluser]timotheus[/eluser]
Zesty discussion, I like it! Smile

Here is the result_array() example code from the documentation..

Code:
$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}

The foreach loop is running through a new copy of the query results. When it's done looping through though, that new array disappears. It was only used for the loop.

If you set a variable to hold what result_array() returns, then go through that..

Code:
$query = $this->db->query("YOUR QUERY");

$result = $query->result_array();
foreach ($result as &$row)
{
   $row['title']=$row['title']."edited!";
   $row['name']=$row['name']."edited!";
   $row['body']=$row['body']."edited!";
}

Then $result will have your query results, that were edited.

This works, but maybe my reasoning for why this works is wrong. I appreciate talking through this with you. I want to be sure I fully understand this.


Messages In This Thread
Changing db results in model before passing - by El Forum - 09-21-2010, 02:45 PM
Changing db results in model before passing - by El Forum - 09-21-2010, 04:26 PM
Changing db results in model before passing - by El Forum - 09-21-2010, 05:40 PM
Changing db results in model before passing - by El Forum - 09-21-2010, 08:30 PM
Changing db results in model before passing - by El Forum - 09-22-2010, 09:53 AM
Changing db results in model before passing - by El Forum - 09-22-2010, 10:01 AM
Changing db results in model before passing - by El Forum - 09-22-2010, 10:21 AM
Changing db results in model before passing - by El Forum - 09-22-2010, 10:26 AM
Changing db results in model before passing - by El Forum - 09-22-2010, 10:37 AM
Changing db results in model before passing - by El Forum - 09-22-2010, 11:01 AM
Changing db results in model before passing - by El Forum - 09-22-2010, 11:38 AM
Changing db results in model before passing - by El Forum - 09-22-2010, 12:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB