0

I am new to CI. Using codeigniter, I am trying to print categories and their items as below:

Category 1 Name

  1. item 1
  2. item 2

Category 2 Name

  1. item 3

  2. item 4

  3. item 5

and so on. I have build an array and subarray inside controller as below:

$data['categories'] = $this->publicpages_model->getcategories();
foreach($data['categories'] as $category)
{
    $data['categories'][$category->cID]= $this->publicpages_model->getitems($category->cID);
}

In the view, I am trying to use the following code but I am not able to complete it to get the desired output as discussed above.

foreach($categories AS $category)
{
    echo '<h1>' . $category->name . '</h1>';

    //Missing code to print the items

    echo "<br>";
}

Although the name of category is printed but I am also getting the following error:

Severity: Notice
Message: Trying to get property of non-object

print_r($categories) gives following result:

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 gives following output:

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) }

Please help with the solution.

5
  • Can you please dump the result of the $this->publicpages_model->getitems($category->cID); and add in the question so that we can help you better Commented Dec 21, 2017 at 7:42
  • post complete error message and post data arrays as well Commented Dec 21, 2017 at 7:43
  • @ChannaveerHakari I have updated the dumps in question detail Commented Dec 21, 2017 at 7:49
  • @AbdullaNilam I have updated the dumps in question detail Commented Dec 21, 2017 at 7:49
  • Check your foreach, there is something wrong. You was change $data['categories'] as a category. But you still call in the foreach using $data['categories'] Commented Dec 21, 2017 at 8:35

2 Answers 2

1

You have a little problem there in your code. What you are trying to do is fine, but how you do it is the problem.

In this foreach loop, you are modifying the same array which you loop; hence it breaks the structure of the $data['categories'] array completely. As a result of that, your second element of the array is not having a name key, so it throws that warning.

foreach($data['categories'] as $category)
{
    $data['categories'][$category->cID] = ...;
}

If you were trying to get the subitems of each category, and add them as a new key, you need to modify the $category array. Not the outer array :) So change it like this.

foreach($data['categories'] as $category)
{
    $category->subItems = $this->publicpages_model->getitems($category->cID);
}

Or if you wanted the $category->cID instead of the key subItems, you can change the above to this:

foreach($data['categories'] as $category)
{
    $category->{$category->cID} = $this->publicpages_model->getitems($category->cID);
}

I hope it helps :) feel free to ask anything if it's not clear.

Sign up to request clarification or add additional context in comments.

Comments

0

imho what you are doing makes no sense - you should add the items to the category

try the following instead

your controller

$data['categories'] = $this->publicpages_model->getcategories();
foreach($data['categories'] as $category)
{
    $category->arrItems = $this->publicpages_model->getitems($category->cID);
}

your view

foreach($categories AS $category)
{
    echo '<h1>' . $category->name . '</h1>';

    if (isset($category->arrItems) && is_array($category->arrItems))
    {
        foreach($category->arrItems AS $objItem)
        {
            print_r($objItem);
        }
    }

    echo "<br>";
}

Comments

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.