1

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);
}
3
  • Re edit your question with your model functions Commented Jul 27, 2017 at 12:04
  • Did any of the answers help? Commented Jul 28, 2017 at 10:50
  • This is an XY problem. If you'd show us your model functions, we might be able to help you with creating a single database query with a JOIN so that all subsequent processes can be simplified. Commented Mar 2 at 3:59

4 Answers 4

1

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 } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Controller, check "hotel_id"

function availability()
{
    $this->data['mealstype'] = $this->Home_model->mealstype();

    if( count( $this->data['mealstype'] ) > 0 )
    {
        foreach($this->data['mealstype'] as $type)
        {
            $type->meals = $this->Home_model->meals( $type->id, $hotel_id ); // hotel_id no declared???
        }
    }
    $this->load->view('home2', $this->data);
}

View

<?php if( count( $mealstype ) > 0): foreach( $mealstype as $type ): ?>  
<h2><?php echo $type->type; ?></h2>
<table class="table table-bordered">
    <thead>
        <tr>
            <th>Meals</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <?php if( count( $type->meals ) > 0 ): foreach( $type->meals as $meals_get ): ?>
        <tr>
            <td><?php echo $meals_get->item; ?></td>
            <td><?php echo $meals_get->price; ?></td>
        </tr>
        <?php endforeach; endif; ?>
        <tr>
            <td><input type="checkbox" class="ck" value="total">  Toal</td>
            <td>2050</td>
        </tr>
    </tbody>
</table>
<?php endforeach; endif ?>

Comments

0

Try to replace into this :

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

check it and print_r($data); what it comes set on the basis of that in view file..

1 Comment

Make sure you are return $query->result_array() from the model
0

You have $hotel_id I am not sure where you set this I can not see it any where else.

We also need to see your model

public function availability() {

    $data['mealstypes'] = array();

    $mealstypes = $this->home_model->mealstype();

    foreach ($mealstypes as $mealstype)
    {
        $meals = array();

        $meals_results = $this->home_model->meals($mealstype->id, $hotel_id);

        foreach ($meals_results $meal_result) {
            $meals[] = array(
                'mealstype' => $meal_result->mealstype,
                'price' => $meal_result->price
            );
        }

        $data['mealstypes'][] = array(
            'type' =>  $mealstype->type
            'meals' => $meals
        );
    }

    $this->load->view('someview', $data);
}

View

<?php foreach ($mealstypes as $mealstype) {?>

<h2><?php echo $mealstype['type'];?></h2>

<?php foreach ($mealstype['meals'] as $meal) {?>
    <?php echo $meal['mealstype'];?>

    <?php echo $meal['price'];?>
<?php }?>

<?php }?>

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.