简体   繁体   English

使用Codeigniter Framework从数据库中获取表数据的正确方法

[英]Proper way of fetching Table data from Database using Codeigniter Framework

Help me solve my problem... I'm using Codeigniter Framework for a couple of days now and trying to do some exercise on how to fetch my database table data 帮助我解决问题...现在我正在使用Codeigniter Framework几天,并尝试就如何获取数据库表数据做一些练习

here is my code: 这是我的代码:

Controller 控制者

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

Model 模型

 Class Query_model extends CI_Controller{

 //what to put here???

 }

Views 观看次数

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

In Controller 在控制器中

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

}

In Model 在模型中

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

In View 视野中

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

Read these too 也阅读这些

  1. How to fetch title of an item from a database and send it to the header template in CodeIgniter 如何从数据库中获取项目标题并将其发送到CodeIgniter中的标题模板
  2. selecting data 选择数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM