Welcome Guest, Not a member yet? Register   Sign In
Attempting to modify the result_array. Help!
#1

[eluser]maverickmind[/eluser]
Apologies for the noob question. I've been using CodeIgniter and PHP for only a few weeks now. I attempted to find an answer to my question searching the forums. But after going through 9 pages of posts I'm hoping someone can point me in the right direction.

Basically I'm trying to figure out how to modify the result_array before I pass it back to my view. I need to parse a DateTime field into Data and Time before it gets displayed in the view.

I'm trying to turn this:[DateTime] => 2009-09-07 09:00:03

into this [Date] => 08/07/09 [Time] => 10:00 am

When I dump the contents of my $data variable the "Date", "Time" keys aren't in the array. It is driving me crazy. Any and all help would be greatly appreciated.

In the model I do this.

Code:
function GetTestMemos($TestID, $ProfileID)
{
   $this->db->where('TestID', $TestID);
   $this->db->where('ProfileID', $ProfileID);
   $this->db->order_by('DateTime', 'desc');
   $query = $this->db->get('TestMemos');
        
   if($query->num_rows() > 0)
   {
     return $query->result_array() ;
   }


Then I do this in my controller
Code:
$data['test']  = $this->dashboard_model->GetTestData( $this->session->userdata('TestID'), $this->session->userdata('ProfileID'));

foreach ($data['tests'] as $test)
{    
   $dT = explode(" ", $test['DateTime']);
   $tempdate = $dT[0];
   $time = $dT[1];

   $test['Date'] = date("m/d/y", strtotime($tempdate));
   $test['Time'] = date("g:i a", strtotime($time));
}

$this->load->view('/layout/header');
$this->load->view('/dash/confirm', $data);
$this->load->view('/layout/footer');

When I dump the contents of my $data variable the "Date", "Time" keys aren't in the array. It is driving me crazy. Any and all help would be greatly appreciated.

Hopefully this made sense if not please let me know what I need to provide.

Thanks in advance!
#2

[eluser]jedd[/eluser]
Hi maverickmind and welcome to the CI forums.

If you do a var_dump($data) just before you call your views, you'll see your Date and Time entries haven't been inserted.

I think you'll need to generate a new array within your loop, and then be sure that you assign that array to $data['something'] once you're done.

EDIT: Oh, if you have sensible data - it's not clear, but I guess $data['tests'] is the same as $data['test'] ? - you can modify your foreach to use the $key=>$value thing, and then insert back into the original array using the $key from there. This is pretty basic PHP stuff though - no CI magic here.
#3

[eluser]maverickmind[/eluser]
THAT is exactly what is happening. They're not there. I suspected I might have to make a new array and push everything over but i'm not sure WHY that is. It's as if the key is set during the loop then erased afterwardsSad
#4

[eluser]jedd[/eluser]
[quote author="maverickmind" date="1255152957"]THAT is exactly what is happening. They're not there. I suspected I might have to make a new array and push everything over but i'm not sure WHY that is. It's as if the key is set during the loop then erased afterwardsSad[/quote]

I just modified my previous post. You're seeing normal PHP behaviour.

Consider [url="http://uk.php.net/manual/en/control-structures.foreach.php"]this page[/url] on the php.net documentation site:
Quote:Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.
#5

[eluser]John_Betong[/eluser]
 
Try adding this line to your query builder:
Code:
$this->db->select('TestID, ProfileID, DateTime, etc');
   ...
   ...
   if($query->num_rows() > 0)
   {
     echo $this->db->last_query();

     echo '<pre>';
       print_r( $query->result_array() );
     echo '</pre>';
     die;

     return $query->result_array() ;
   }
&nbsp;
&nbsp;
&nbsp;
edit: included missing "$query->result_array()" in print_r(...) function.
#6

[eluser]jedd[/eluser]
Oh, and you might want to do this in your model - I think the received wisdom is that your model should do all of this kind of data mangling, and just produce shiny little lumps of nicely formatted data.
#7

[eluser]jedd[/eluser]
Veering off ever so slightly to one side -- I've never done the &$var thing in order to insert data into an extant array like this - instead I've simply generated a new array, often bringing one of the values from the array (say the id field from the table) into play as the key for a new dimension. Err, you know what I mean.

Anyhoo, out of curiosity I had a quick play with the reference approach, as described on that PHP.net page I cited earlier, and I'm seeing an odd appearance of an ampersand in my output.

Here's my code:
Code:
&lt;?php
    $foo = array();
    $foo[] = array ("a"=>"alpha", 'b'=>"bravo", 'c'=>"charlie");
    $foo[] = array ("a"=>"alpha", 'b'=>"bravo", 'c'=>"charlie");

    var_dump ($foo);

    echo " --- ";

    foreach ($foo as &$bar)
        $bar['d'] = 'delta';

    var_dump ($foo);

This is the unadulterated output that's generated - specifically note the 1 = & bit in the second section.
Code:
array
  0 =>
    array
      'a' => string 'alpha' (length=5)
      'b' => string 'bravo' (length=5)
      'c' => string 'charlie' (length=7)
  1 =>
    array
      'a' => string 'alpha' (length=5)
      'b' => string 'bravo' (length=5)
      'c' => string 'charlie' (length=7)

---
array
  0 =>
    array
      'a' => string 'alpha' (length=5)
      'b' => string 'bravo' (length=5)
      'c' => string 'charlie' (length=7)
      'd' => string 'delta' (length=5)
  1 => &
    array
      'a' => string 'alpha' (length=5)
      'b' => string 'bravo' (length=5)
      'c' => string 'charlie' (length=7)
      'd' => string 'delta' (length=5)

If I add in an extra array in the setup - the & appears on 2 => &, but not on 1 - ie, it follows the last array's index.

Any ideas?
#8

[eluser]maverickmind[/eluser]
Ahhhhh thanks for the heads up about how foreach operates. I'm just happy to know that i'm not completely losing my mind. Thanks everyone. This was a great help! Now I have a clearer understanding of what I need to do.




Theme © iAndrew 2016 - Forum software by © MyBB