Joomla 2.5 SELECT query result in array

General questions relating to Joomla! 2.5. Note: All 1.6 and 1.7 releases have reached end of life and should be updated to 2.5. There are other boards for more specific help on Joomla! features and extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Locked
vdyl
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Dec 03, 2012 1:33 pm

Joomla 2.5 SELECT query result in array

Post by vdyl » Mon Dec 03, 2012 1:37 pm

Code: Select all

function getCategorie()
{
    $db = JFactory::getDBO();
    
    $query = "SELECT id,title FROM #__categories ORDER BY title ASC";
    
    $db->setQuery($query);
    return $db->loadObjectList();
}
results in

Code: Select all

array(8) {
  [0]=>
  object(stdClass)#165 (2) {
    ["id"]=>
    string(1) "2"
    ["title"]=>
    string(17) "Ongecategoriseerd"
  }
  [1]=>
  object(stdClass)#195 (2) {
    ["id"]=>
    string(1) "3"
    ["title"]=>
    string(17) "Ongecategoriseerd"
  }
}
but how do I make a foreach statement of it? When i try to do a foreach I get the following error message
Fatal error: Cannot use object of type stdClass as array in

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24974
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Joomla 2.5 SELECT query result in array

Post by pe7er » Mon Dec 03, 2012 2:34 pm

Welcome to Joomla forum!

What's the variable that contains the $db->loadObjectList() object?
if it's $result, then use print_r($result) to see the complete object with all arrays.

maybe you could use something like:

Code: Select all

foreach($result as $item){
    echo $item->id." - ". $item->title."<br>";
}
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com


Locked

Return to “General Questions/New to Joomla! 2.5”