1

Why am I getting this error when trying to access any of the array keys using the following?

$product_cat = '22';
$db = JFactory::getDbo();
$query = $db->getQuery( true );
$query->select( $db->quoteName( array( 'name', 'alias', 'parent' ) ) );
$query->from( $db->quoteName( '#__k2_categories' ) );
$query->where( $db->quoteName( 'parent' )." = " .$product_cat );
$db->setQuery( $query );
$row = $db->loadObjectList();
print_r($row);
foreach ($row as $value) {
    foreach($value as $result) {
        echo '<ul>';
        echo '<li><a href="#">' . $result['name'] . '</a></li>';
        echo '</ul>';
    }
}

The following array is being returned:

Array (
[0] => stdClass Object ( [name] => Stimulation Chemicals [alias] => stimulation-chemicals [parent] => 22 )
[1] => stdClass Object ( [name] => Cementing Chemicals [alias] => cementing-chemicals [parent] => 22 )
[2] => stdClass Object ( [name] => Improved Oil Recovery [alias] => improved-oil-recovery [parent] => 22 )
[3] => stdClass Object ( [name] => Drilling Fluid Additives [alias] => drilling-fluid-additives [parent] => 22 )
[4] => stdClass Object ( [name] => Solvents and Surfactants [alias] => solvents-and-surfactants [parent] => 22 )
[5] => stdClass Object ( [name] => Coil Tubing Chemicals [alias] => coil-tubing-chemicals [parent] => 22 )
[6] => stdClass Object ( [name] => Production Chemicals [alias] => production-chemicals [parent] => 22 ) )

So I'm not sure what's happening, I'm simply trying to access 'name, 'alias' and 'parent' and spit them out in different places in a echoed html string. Any ideas?

3
  • On exactly which line is the error reported? You don't have an array of arrays, you have an array of stdClass objects. But that shouldn't break the foreach ($value as $result). You can iterate over an object in that way. Commented Feb 17, 2015 at 14:03
  • Sorry Michael, the code that was breaking it was '$result['name']'. I'll update it to reflect this for future readers. Commented Feb 17, 2015 at 14:08
  • Oh I see - for some reason I was missing that in the inner loop and reading it as though you had just $result there (which would have worked) rather than $result['name']. Commented Feb 17, 2015 at 14:11

1 Answer 1

2

Try this:

foreach ($row as $value) {
   echo '<ul>';
   echo '<li><a href="#">' . $value->name . '</a></li>';
   echo '<li><a href="#">' . $value->alias . '</a></li>';
   echo '<li><a href="#">' . $value->parent . '</a></li>';
   echo '</ul>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting, I'll mark this off as the correct answer as soon as I can. Thank you sir. I'm still a baby at this so excuse my stupidity, but in a previous project I was able to return an array with database values and access it using $whatever['name'] and so on and so forth, why doesn't that work here?
@jasenmp $value is an object and have the data of name and etc... so no need to iterate it again this will make slower to render the page. after second foreach $result variable have the value of index directly. so this ($result['name']) will not work. you need to give only $result and can access the value directly.
Thank you for the explanation Code Lover.

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.