1

In my application I'm trying to print out information with a foreach. The SQL is written correctly. When I do a var_dump of my function I get the correct information with:

$eventgroupinfo = $event->GetEventGroupInfoByEventGroupId($event_group_id);
var_dump($eventgroupinfo);

When I try to print it out it is giving this error:

Warning: Illegal string offset

But I get the information when I var_dump it. What am I doing wrong?

FOREACH

foreach ($eventgroupinfo as $g) {
  echo $g['user_id'] . $g['avatar'];
}

FUNCTION:

public function GetEventGroupInfoByEventGroupId($event_group_id)
{
    $db = new Db();

    $select = "SELECT
        p.event_progress_id,
        p.user_id,
        p.event_progress_distance,
        p.event_progress_date,
        p.event_group_id,
        u.user_id,
        u.name,
        u.surname,
        u.avatar
    FROM tblevent_progress p INNER JOIN tblusers u ON p.user_id = u.user_id  
    WHERE p.event_group_id = " . $event_group_id ;

    var_dump($select);
    $result = $db->conn->query($select);
    return $data=$result->fetch_assoc();
    }
}

VAR_DUMP RESULTS

array (size=8)
'event_progress_id' => string '1' (length=1)
'user_id' => string '1' (length=1)
'event_progress_distance' => string '2532' (length=4)
'event_progress_date' => string '2014-05-10 12:48:27' (length=19)
'event_group_id' => string '50' (length=2)
'name' => string 'Vandenbergh' (length=11)
'surname' => string 'Jan' (length=3)
'avatar' => string '139404100.jpg' (length=21)
1
  • Show us the var_dump results, otherwise we don't know the structure of your array. Commented May 18, 2014 at 23:10

2 Answers 2

1

Just:

echo   $eventgroupinfo['user_id'] . $eventgroupinfo['avatar'];

Instead of

foreach ($eventgroupinfo as $g) {
   echo   $g['user_id'] . $g['avatar'];
}

P.S. : As $eventgroupinfo is a one-dimensional array you are iterating through it like:

'event_progress_id'->'user_id'->...->'avatar'

So you $g is of string type not array so should not access it like $g['user_id'], just $g.

Sign up to request clarification or add additional context in comments.

Comments

0

Your foreach loop is going to execute one time for each element in your array. In your case, you will have an iteration for each of these:

'event_progress_id'
'user_id'
'event_progress_distance'
'event_progress_date'
'event_group_id'
'name'
'surname'
'avatar'

If you are expecting multiple result sets (which it looks like you would), you'll need to return a multi-level array from your function call:

public function GetEventGroupInfoByEventGroupId($event_group_id)
    {
        $db = new Db();

                $select = "SELECT

                            p.event_progress_id,
                            p.user_id,
                            p.event_progress_distance,
                            p.event_progress_date,
                            p.event_group_id,
                            u.user_id,
                            u.name,
                            u.surname,
                            u.avatar


                        FROM tblevent_progress p INNER JOIN tblusers u ON p.user_id = u.user_id  
                        WHERE p.event_group_id = " . $event_group_id ;

        var_dump($select);
        $result = $db->conn->query($select);
        $entries = array();            
        while($data=$result->fetch_assoc())
        {
            $entries[] = $data;
        }
        return $entries;
    }

Then your loop should output one instance for each entry in the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.