Я новичок в CI. Используя codeigniter, я пытаюсь напечатать категории и их элементы, как показано ниже:
Имя категории 1
- item 1
- item 2
Имя категории 2
-
item 3
-
item 4
-
item 5
и т. д. Я создал массив и подмассив внутри контроллера, как показано ниже:
$data['categories'] = $this->publicpages_model->getcategories();
foreach($data['categories'] as $category)
{
$data['categories'][$category->cID]= $this->publicpages_model->getitems($category->cID);
}
Я пытаюсь использовать следующий код, но не могу его завершить, чтобы получить желаемый результат, как описано выше.
foreach($categories AS $category)
{
echo '<h1>' . $category->name . '</h1>'; //Missing code to print the items echo "<br>";
}
Хотя название категории напечатано, но я также получаю следующую ошибку:
Severity: Notice
Message: Trying to get property of non-object
print_r ($ category) дает следующий результат:
Array ( [0] => stdClass Object ( [cID] => 1 [name] => Breakfast ) [1] => Array ( [0] => stdClass Object ( [itemID] => 1 [name] => Aaloo Paratha [description] => descriptio 1 [menu] => 1 [price] => 22 [tax] => 20 [sStatus] => ) ) [2] => stdClass Object ( [cID] => 7 [name] => Fast Food ) [5] => Array ( [0] => stdClass Object ( [itemID] => 5 [name] => Missi Roti [description] => Hot and Crunchy [menu] => 5 [price] => 5 [tax] => 1 [sStatus] => 1 ) ) [7] => )
vardump дает следующий вывод:
array(5) { [0]=> object(stdClass)#22 (2) { ["cID"]=> string(1) "1" ["name"]=> string(9) "Breakfast" } [1]=> array(1) { [0]=> object(stdClass)#25 (7) { ["itemID"]=> string(1) "1" ["name"]=> string(13) "Aaloo Paratha" ["description"]=> string(12) "descriptio 1" ["menu"]=> string(1) "1" ["price"]=> string(2) "22" ["tax"]=> string(2) "20" ["sStatus"]=> string(0) "" } } [2]=> object(stdClass)#24 (2) { ["cID"]=> string(1) "7" ["name"]=> string(9) "Fast Food" } [5]=> array(1) { [0]=> object(stdClass)#26 (7) { ["itemID"]=> string(1) "5" ["name"]=> string(10) "Missi Roti" ["description"]=> string(15) "Hot and Crunchy" ["menu"]=> string(1) "5" ["price"]=> string(1) "5" ["tax"]=> string(1) "1" ["sStatus"]=> string(1) "1" } } [7]=> bool(false) }
Пожалуйста, помогите с решением.