robins
robins

Reputation: 1668

How to print multiple HTML tables from dynamic data in a CodeIgniter application?

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

Upvotes: 2

Views: 5105

Answers (4)

Robert
Robert

Reputation: 147

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 } ?>

Upvotes: 2

user4419336
user4419336

Reputation:

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 }?>

Upvotes: 0

Madhur
Madhur

Reputation: 1772

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..

Upvotes: 0

MikeSouto
MikeSouto

Reputation: 240

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 ?>

Upvotes: 0

Related Questions