使用Codeigniter Framework

时间:2018-01-25 05:34:51

标签: php codeigniter

帮我解决问题... 我现在使用Codeigniter Framework几天,并试图对如何获取我的数据库表数据进行一些练习

这是我的代码:

控制器     

public function view($page = 'login'){

    if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
        show_404();
    }


    $data['title'] = ucfirst($page);
    $this->load->view('templates/header');
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer');

}

//this is my main controller.
}

模型

 Class Query_model extends CI_Controller{

 //what to put here???

 }

视图

<table class="table table-striped">
<thead>
 <tr>
  <th>Invoice Number</th>
  <th>Name</th>
  <th>Amount Due</th>
  <th>Date</th>
  <th>Due</th>
  <th>Status</th>
  <th>Actions</th>
 </tr>
</thead>
<tbody>
<?php 
 //here i wanna display those data..
?>
</tbody>

1 个答案:

答案 0 :(得分:0)

在控制器中

public function view($page = 'login'){

    if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
        show_404();
    }
    $this->load->model('Query_model'); # load model

    $data['invoices'] = $this->Query_model->getData(); # get data

    $data['title'] = ucfirst($page);
    $this->load->view('templates/header');
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer');

}

在模型中

Class Query_model extends CI_Model{
    public function getData(){
      $query = $this->db->get('mytable');
      $result = $query->result_array();
      return $result;
   }
}

在视图中

<tbody>
<?php 
    foreach ($invoices as $key => $invoice) {
        ?>
        <tr>
            <td><?php echo $invoice[''] ?></td> // InvoiceNumber
            <td><?php echo $invoice[''] ?></td> // Name
            <td><?php echo $invoice[''] ?></td> // Amount Due
            <td><?php echo $invoice[''] ?></td> // Date
            <td><?php echo $invoice[''] ?></td> // Due
            <td><?php echo $invoice[''] ?></td> // Status
            <td><?php echo $invoice[''] ?></td> // Actions
        </tr>

        <?php
    }
?>
</tbody>

阅读这些

  1. How to fetch title of an item from a database and send it to the header template in CodeIgniter
  2. selecting data