0

I'm having an issue querying multiple values from rows that have a certain value. I use the SQL code below to grab column "group_id"'s values if the "id" equals 999. In the "xx_groupmap" table, the id 999 shows up three times with different values. Below is my table.

table xx_groupmap
-------------------
id   |  group_id
-------------------
999  |  2
999  |  7
999  |  8

Below is the code I use

        $db = JFactory::getDbo();
        $query = "SELECT group_id FROM xx_groupmap WHERE id = '999'";
        $db->setQuery($query);
        $results = $db->loadObjectList();
        foreach ($results as $t) {
        }

So in the end when I want to return the three values of id 999 I use this code

echo $t->group_id;

but it only outputs one number. How do I build an array with the three values from id 999?

1 Answer 1

3

Thats because, when you are in the foreach, $t is always updated, and you will get tha last $t only. Put them into an array.

$ids = array();
foreach ($results as $t) {
    $ids[] = $t->group_id;
}
var_dump($ids);
Sign up to request clarification or add additional context in comments.

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.