Codeigniter multiple foreach loop

robins

I have two tables meals type and meals. I want to print each meals corresponding to its meals type.

Error is that only showing last meals type meals only

view page is like

View

<?php
foreach ($mealstype as $type) {
    ?>  
    <h2><?php echo $type->type; ?></h2>
    <table class="table table-bordered">
        <tr>
            <th>Meals</th>
            <th>Price</th>

        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($meals as $meals_get) {
            ?>
            <tr>
                <td><?php echo $meals_get->item; ?></td>
                <td><?php echo $meals_get->price; ?></td>

            </tr>
        <?php } ?>
        <tr>
            <td> <input type="checkbox" class="ck" value="total">  Toal</td>
            <td>2050</td>

        </tr>
    </tbody>
    </table>
    <?php
}
?>

Controller

function availability() {

    $this->data['mealstype'] = $this->Home_model->mealstype();
    foreach ($this->data['mealstype'] as $type) {
        $typeid = $type->id;
        $meals[] = $this->Home_model->meals($typeid, $hotel_id);
    }
    $this->data['meals'] = $meals[];
    $this->load->view('home2', $this->data);
}
Robert

Please check this answer

Controller:

public function availability() {  
    $build_array = array();
    mealstype= $this->Home_model->mealstype();
    foreach($mealstype as $row){
        $build_array[] = array(
        'parent_array' => $row,
        'child_array' =>$this->Home_model->meals($row->id,$hotel_id),
        'child_sum'     =>$this->Home_model->meals_sum($row->id,$hotel_id)
        );
    }
    $this->data['build_array'] = $build_array;
}

and the view page is like this:

View:

<?php foreach($build_array as $type){?> 
<h2><?php echo $type['parent_array']->type;?></h2>
<table class="table table-bordered">
    <tr>
        <th>Meals</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
<?php
foreach ($type["child_array"] as $meals_get) {?>
    <tr>
        <td><?php echo $meals_get->item;?></td>
        <td><?php echo $meals_get->price;?></td>
    </tr>
<?php }?>
    <tr>
        <td>  Toal</td>
        <td>2050</td>
    </tr>
<?php } ?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Multiple Foreach Loop Codeigniter

foreach loop as category with Codeigniter

Codeigniter foreach loop error

Echo arrays in foreach loop codeigniter

Codeigniter simple foreach Loop is not working

Multiple inserts in a loop codeigniter

Codeigniter - multiple input with loop

Multiple sheets for ForEach Loop

Array within array using foreach loop in codeigniter

CodeIgniter delete query does not work in foreach loop?

codeigniter calling library function stops foreach loop

Codeigniter set_value() foreach loop

i want to display ads in a foreach loop in codeigniter

Foreach loop not display result in Codeigniter View

Creating Multiple List in foreach loop

multiple foreach loop with mysql join

Exclude Multiple OUs in a foreach loop

Running multiple functions in a foreach loop

codeigniter foreach multiple entries with same ID

Use SSIS Foreach loop to loop multiple folders

how to use foreach loop into another foreach loop to traverse two different datatables in codeigniter?

How to pass value by comma separated to a variable using foreach loop in codeigniter

Cannot get record from two tables in foreach loop in Codeigniter

result_array value are not showing inside foreach loop in codeigniter

how to skip foreach loop when value match in codeigniter

Adding row_array to result_array foreach loop with codeigniter?

How to assign array value into variable inside foreach loop in codeigniter

Need to get keys form my array for a foreach loop for where in codeigniter

Unable to access data from array inside foreach loop - Codeigniter