如何在codeigniter中将数据显示到视图文件中

时间:2015-07-25 03:04:01

标签: php codeigniter

我想在按下按钮的视图中显示查询结果。

控制器代码

public function reservation(){

    $data['result']=$this->Books_model->get_reservations();
    $this->load->view('restaurants/booking',$data);

}

型号代码

  public function get_reservations(){

    $sql ="SELECT `reservation.reservation_id`, `reservation.rest_names`, `reservation.rest_start_time`, `reservation.rest_end_time`, `reservation.no_of_people`, `reservation.date_id` , `date.date_id`
    FROM `reservation`,`date`
    WHERE `reservation.date_id`=`date.date_id`";
    $query =$this->db->query($sql);
    return $query->result();


}

并查看文件

 <div class="col-lg-2 pager">
                <div class="col-sm-2">
                    <input type="text" placeholder="Number of People " id="number_of_people" name="no_people"
                           class="form-control">
                </div>

                <input type="date" class="form-control" placeholder="Date">
            </div>
            <div class="col-lg-3 pager">
                <label for="start_time" class="col-lg-2 pager">Start_Time</label>
                <div class="dropdown">
                    <select  class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                        <option> Start Time </option>
                        <option> 1:00 </option>
                        <option> 2:00 </option>
                        <option> 3:30 </option>
                        <option> 4:00 </option>
                        <option> 5:00 </option>
                        <option> 6:00 </option>
                        <option> 7:00 </option>
  </select>

                </div>

            </div>


                </div>
            <div class="col-lg-1 pager"> <a href="" class="btn btn-warning"> check Availability  </a> </div>
    </div>

请指导我在我的视图文件中做了哪些更改,我将得到我的愿望输出。

2 个答案:

答案 0 :(得分:0)

您传递给视图的$ data数组为您提供了与您的键相等的变量(请参阅PHP extract()函数)。因此,在您的情况下,您可以在视图中使用$ result。请在您的视图中尝试使用此功能。

<?php
var_dump($result);
?>

答案 1 :(得分:0)

控制器

$data['result'] = Anythings
$this->load->view('restaurants/booking',$data);

查看餐厅/预订

<?=var_dump($result)?>

在codegintaor中编写模型的最佳方式:

protected function get_reservations()
{

        $sql = "SELECT 
       `reservation.reservation_id`, 
       `reservation.rest_names`, 
       `reservation.rest_start_time`, 
       `reservation.rest_end_time`, 
       `reservation.no_of_people`, 
       `reservation.date_id` , 
       `date.date_id`
            FROM `reservation`,`date`
            WHERE `reservation.date_id`=`date.date_id`";

       $query =$this->db->query($sql);

       if($query->num_rows() > 0 )
       {
            return $query->row_array();// for one row
            or 
            return $query->result_array(); // for more then one row
       }

    }