• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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:

= array();
$query mysqli_query($conn"SELECT id, name FROM categories");
$categories mysqli_fetch_assoc($query);

$categories as $category) {

?><h3><? echo $category['name']; ?></h3><?php

= array();
$category_id $category['id'];
$query mysqli_query($conn"SELECT id, title, description, price FROM books WHERE category='$category_id'");
$books mysqli_fetch_assoc($query);

$books as $book) {

?><p><b><?php echo $book['title']; ?></b><br><br><?php echo $book['description'?><br><br><b><u><?php echo $book['price']; ?></u></b></p><?php


below I tried to start coding myself.

PHP Code:
class Books_model extends CI_Model {

 public function 
__construct() {


 public function 
get_books() {
$query $this->db->get('books');

class Books extends CI_Controller() {

 public function 
__construct()) {


 public function 
index() {

$data['title'] = "Books";
$data['books'] = $this->books_model->get_books();


<h2><?php echo $title?></h2>

<?php foreach $books as $book): ?>
 <h3><?php echo $book['title']; ?></h3>
 <?php echo $book['description']; ?><br><br>
 <b><u></b><?php echo $book['price']; ?></u></b>
<?php endforeach; ?>

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.
Test your translation files with Translation Tester

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();

$data['categories'] as $cat) {

$cat['id']. "<br>";

$data['books'] = $this->books_model->get_books($cat['id']);

$data['books'] as $book) {

$book['name']. "<br>";


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! Sad

I would do something like that:

PHP Code:
$data['categories'] = $this->books_model->get_categories();

foreach (
$data['categories'] as &$cat) {
    $cat['books'] = $this->books_model->get_books($cat['id']);

In the view you can loop like that:
PHP Code:
foreach ($categories as $cat) {
    echo $cat['name'] . "<br>";

    foreach ($cat['books'] as $book) {
        echo $book['title']. "<br>";

Test your translation files with Translation Tester

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.