• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
foreach loop in a foreach loop

#1
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

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

foreach(
$categories as $category) {

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

 $books 
= 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);

 foreach(
$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:
// MODEL
class Books_model extends CI_Model {

 public function 
__construct() {

 
$this->load->database();
 }

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

// CONTROLLER
class Books extends CI_Controller() {

 public function 
__construct()) {

 
$this->load->model('books_model'); 
 }

 public function 
index() {

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

 
$this->load->view('template/header'$data);
 
$this->load->view('books/index'$data);
 
$this->load->view('template/footer');
 }
}

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

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



Hopefully someone can help me out.
kindest regards.Dirk
Reply

#2
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
Reply

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

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

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

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

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

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

Reply

#4
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.
Reply

#5
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
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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