Welcome Guest, Not a member yet? Register   Sign In
adding object to a db query result
#1

[eluser]kidego32[/eluser]
Hello,


I have a $data['result'] in my controller that was generated from a $query->result() in my model. I have a date field inside my result set that I'd like to split into the month, day, and year before passing it to the view.

I am able to access the field and split it, but I'm at a loss as to how to add the newly split fields back to the $data['result'] in order to pass it to the view.

Code:
$data['result'] = $this->Customers_model->get();
            
// Split up the date into the month, day, and year
$arr=split("-",$data['result']->dob); // split the array

$year=$arr[0]; // first element of the array is year
$month=$arr[1]; // second element is month
$day=$arr[2]; // third element is day
            
            
// Add these new values to the $data['result'] array
            
            
$this->load->view('customers/update_view',$data);

Does anyone know how I can add the $year, $month, and $day variables to the $data['result'] set?
#2

[eluser]Colin Williams[/eluser]
Easy enough.

Code:
$data['result'] = $this->Customers_model->get();
            
// Split up the date into the month, day, and year
list($data['result']->year, $data['result']->month, $data['result']->day) = split("-",$data['result']->dob);
            
$this->load->view('customers/update_view',$data);
#3

[eluser]kidego32[/eluser]
Thank you. It worked perfectly!
#4

[eluser]Colin Williams[/eluser]
You are welcome. Your object properties are read/write, so you can always do $object->property = $x; Also, the list() statement is a nice shortcut for extracting a known number for variables from an array.




Theme © iAndrew 2016 - Forum software by © MyBB