foreach loop in a foreach loop |
Hi there,
i am new to code igniter, and trying to understand CI 3 first. Now i am stuck with following situation: I would like to get all books from a database, that is working fine. but when I want to group it by category i am stuck. below the first code block is the preferred situation, below that code block I tried to do this in CI 3 format. Can someone help me out with: - How do I create a model function with a query bases on other query results? - how do I put this correct through the controller an make the output in the view? PHP Code: <?php below I tried to start coding myself. PHP Code: // MODEL Hopefully someone can help me out. kindest regards.Dirk
Unfortunately the user guide is currently unavailable, there’s some trouble with the website, so I can’t link to the right page for the database queries. But, what you need to do is get all the data first, and pass this to your view so it only have to display it.
First, in your controller you need to get all the categories. Loop for each category, get the books from this category and add them to the array as a new property for this category. You will need to add a “where” clause in your model and a parameter to the get_books() function to pass which category id we want. Then in your view you only have to loop for each category, then for each books of this category.
Oke, if I understand correctly, this gets everything from the database as needed.
But how do I bind this together in a (multidimensional?) array? tried a lot and was looking for some relevant topics but couldn't find the answer. many thanks in advance. PHP Code: $data['categories'] = $this->books_model->get_categories();
tried a lot, and found a way to fix it.
foreach loop in the controller and just load the view in there and that's working fine.
Sorry for the late reply. It looks like the forum stopped sending email notifications, again!
![]() I would do something like that: PHP Code: $data['categories'] = $this->books_model->get_categories(); In the view you can loop like that: PHP Code: foreach ($categories as $cat) { |
Welcome Guest, Not a member yet? Register Sign In |