0

I'm loading values from a custom category table in Joomla 2.5. This works fine, I can display the name for each category.

Then I want to add categories with a specific id to a new array, but I can't make it work. In C# i know exactly how to do this, but not in PHP.

Here's my code (thx for fixing the code format):

<?php
// Get a db connection.
$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);
$query->select('*');
$query->from('ind_category');
$query->order('Name ASC');

echo $query . "</br>";
// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$categories = $db->loadObjectList();

// show the retrieved categories
if ($categories != null) {
    foreach ($categories as $cat) {
        echo "Name: " . $cat->Name . "</br>";
    }

} else {
    echo "<br> NULL results from " . $query;
}
echo "<br>";
echo "<br>";

// add objects from $categories with specific Id to $arr2
$arr2 = array();

foreach ($categories as $cat) {
    if ($cat->Id == 1) {
        $arr2[] = $cat;
    }
}
?>
PitAttack76
  • 2,120
  • 5
  • 31
  • 44
  • whats the problem? `$arr2` remains empty? tried `$cat->id` (lowercase I) ? – bagonyi Dec 03 '13 at 16:09
  • On a side note, you should use `#__` to define the table prefix rather than `ind_` so it will be `$query->from('#__category');` – Lodder Dec 04 '13 at 12:24

1 Answers1

0

According to joomla docs loadObjectList() returns an array like this:

Array ( 
    [0] => stdClass Object ( [id] => 1 [name] => John Smith 
    [email] => johnsmith@domain.example [username] => johnsmith ) 
    [1] => stdClass Object ( [id] => 2 [name] => Magda Hellman 
    [email] => magda_h@domain.example [username] => magdah ) 
    [2] => stdClass Object ( [id] => 3 [name] => Yvonne de Gaulle 
    [email] => ydg@domain.example [username] => ydegaulle ) 
)

So try $cat->id (lowercase I)

bagonyi
  • 3,258
  • 2
  • 22
  • 20