![]() |
populating an associative array from an sql return stryct - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: populating an associative array from an sql return stryct (/showthread.php?tid=71534) |
populating an associative array from an sql return stryct - richb201 - 08-25-2018 Trying to move data from mysql over to a structure. $data['email'] = $row->email; $data['year'] = $row->year; But when I look at $data, the fields are null even though the $row is populated. What is wrong with this? I need to get the data into an associative array. RE: populating an associative array from an sql return stryct - richb201 - 08-25-2018 I changed it to this: $data = array( 'email' => $row->email, 'year' => $row->year, 'campaign' => $row->campaign, 'data_time' => uniqid(), 'hours' => $row->hours, 'week' => $row->week, 'business_component' => $row->business_comp, 'project' => $row->project, 'activity' => $row->activity); But it is still not working. The elements of $data are still null. RE: populating an associative array from an sql return stryct - InsiteFX - 08-25-2018 Place these two methods in a helper file and load it. PHP Code: // ----------------------------------------------------------------------- These two methods will convert (array to object and object to array). RE: populating an associative array from an sql return stryct - richb201 - 08-25-2018 I create MY_array_helper and put it in the helpers directory. I load it with: $this->load->helper('string','MY_array_helper'); And then I try calling it with $data=objectToArray($result); The $result struct is from my call to mySQL Query(). I get Message: Call to undefined function objectToArray(). Is the result from the mysql query an object? Perhaps I want to do $data2=objectToArray($result) instead, and then have: $data = array( 'email' => $data2->email, 'year' => $data2->year, 'campaign' => $data2->campaign, 'data_time' => uniqid(), 'hours' => $data2->hours, 'week' => $data2->week, 'business_component' => $data2->business_comp, 'project' => $data2->project, 'activity' => $data2->activity); RE: populating an associative array from an sql return stryct - richb201 - 08-25-2018 Got it. This is the solution: $data = array( 'email' => $row['email'], 'year' => $row['year'], 'campaign' => $row['campaign'], 'data_time' => uniqid(), 'hours' => $row['hours'], 'week' => $row['week'], 'business_component' => $row['business_comp'], 'project' => $row['project'], 'activity' => $row['activity']); RE: populating an associative array from an sql return stryct - InsiteFX - 08-26-2018 Not sure where you got $result from but it looks like you should have passed $row to the method. RE: populating an associative array from an sql return stryct - richb201 - 08-26-2018 (08-26-2018, 04:28 AM)InsiteFX Wrote: Not sure where you got $result from but it looks like you should have passed $row to the method. https://www.codeigniter.com/user_guide/database/results.html RE: populating an associative array from an sql return stryct - InsiteFX - 08-27-2018 I know all about those results. In your first post you mention $row but then you are passing $result to the $data = objectToArray($result); This should be passed the result from the database. $data = objectToArray($row); |