我正在从Joomla 2.5中的自定义类别表中加载值。这很好用,我可以显示每个类别的名称。
然后我想将具有特定id的类别添加到新数组中,但我无法使其工作。 在C#中,我确切地知道如何做到这一点,但不是在PHP中。
这是我的代码(用于修复代码格式的代码):
<?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;
}
}
?>
答案 0 :(得分:0)
根据joomla docs loadObjectList()返回如下数组:
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 )
)
请尝试$cat->id
(小写I)