SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard silver trophy
    Join Date
    Aug 2003
    Location
    Southern California
    Posts
    4,686
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Trouble appending data to mySQL array results

    Hey developers -

    I'm building a controller that needs to run through the results of a mySQL query and append a few extra fields based on what the row contains. Basically I'm just running a loop to keep it out of the template and make things more portable.

    However, I'm not figuring out how to add info back to the main array generated from the mySQL query.

    The goal is to leave an array intact at the end with all of the results plus the new fields on each row so I can open it up in a corresponding template, loop through them and display it all.

    Code:

    PHP Code:
    $id 0;
    $activities $this->activity_model->getActivity();
    foreach(
    $activities->result_array() as $activity){
    $activities[$id]['show'] = 0;
    ...

    This produces the error: Cannot use object of type CI_DB_mysql_result as array in...

    Simply appending via $activity['show']=0; works fine of course but does not carry through back to the main array and into the template.

    Help?
    - Ted S

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you post the getActivity() method please Ted?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Aug 2003
    Location
    Southern California
    Posts
    4,686
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Can you post the getActivity() method please Ted?
    Of course. Simplified significantly as it's an extensive amount of query logic but only a single action / output method.

    PHP Code:
    function getActivity($user_id,$friends,$limit)
    {

    $this->db->select('
        activity.user_id,
        activity.friend_id,
        activity.action
    '
    );
    $this->db->where('action !=''facebook');
    return 
    $this->db->get('activity'10);

    get() is a function from code igniter's active record database class.
    - Ted S

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try this Ted.
    PHP Code:
    return $this->db->get('activity'10)->result_array(); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Aug 2003
    Location
    Southern California
    Posts
    4,686
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)
    That did it. Thanks!
    - Ted S


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •