如何使用CodeIgniter从DB查看记录

时间:2017-02-04 08:54:59

标签: php html mysql twitter-bootstrap codeigniter

我尝试按照此site的教程按照Code Igniter查看我的记录,但它显示错误消息

我可以尝试将其识别出来吗?

Belows is view" view_inlist.php"代码

<table class="table table-striped table-bordered">
        <tr>
            <td><strong>ID</strong></td>
            <td><strong>Name</strong></td>
            <td><strong>Number</strong></td>
            <td><strong>Type</strong></td>
            <td><strong>Unit</strong></td>  
            <td><strong>Date</strong></td> 
        </tr> 
        <?php foreach($LIST as $list)
        {?>
        <tr>
            <td><?=$list->ID;?></td>
            <td><?=$list->Name;?></td>
            <td><?=$list->Letter_Number;?></td>
            <td><?=$list->Letter_Type;?></td>
            <td><?=$list->Unit;?></td>
            <td><?=$list->Date;?></td>
        </tr>     
        <?php }?>
    </table>

这是控制器&#34; Main.php&#34;

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Main extends CI_Controller {
      public function check_inList() {
          $this->load->model('login');
          $query = $this->login->check_inlist();
          $data['LIST'] = null;
          if($query){
             $data['LIST'] =  $query;
          }
          $this->load->view('form/view_inlist', $data);
      }        
    }

这是Model&#34; login.php&#34;

function check_inlist(){                                                 
   $this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,Date");
   $this->db->from('in_list');
   $query = $this->db->get();
   return $query->result();
}

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

try this

    in controller

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Main extends CI_Controller {

        public function __construct() {
    parent::__construct();
     $this->load->model('login');
  }

      public function check_inList() {
          $data['all_list'] = $this->login->check_inlist();

          $this->load->view('form/view_inlist', $data);
      }


    }





in model 

<?php

class Login extends CI_Model {


 function check_inlist(){                                                 
   $this->db->select('*');
   $this->db->from('in_list');
   $query = $this->db->get();
   return $query->result();
}

}

in view page 


       <?php foreach($all_list as $list) {?>

         <tr>       

            <td><?php echo $list->ID;?></td>
            <td><?php echo $list->Name;?></td>
            <td><?php echo $list->Letter_Number;?></td>
            <td><?php echo $list->Letter_Type;?></td>
            <td><?php echo $list->Unit;?></td>
            <td><?php echo $list->Date;?></td>
        </tr> 
    <?php }?>    

答案 1 :(得分:0)

首先检查您的数据库结构,因为在模型中您可以获取 EMAIL

$this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,EMAIL");

并在视图中打印日期列<td><?=$list->Date;?></td>

<强> 1.Controller Main.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
    public function __construct()
    {
            parent::__construct();
            $this->load->model('login');
            $this->load->helper('url_helper');
    }


  public function check_inList() {
      $query = $this->login->check_inlist();
      $data['LIST'] = null;
      if($query){

         $data['LIST'] =  $query;
      }
      $this->load->view('view_inlist', $data);
  }        
}
?>

<强> 2)浏览 view_inlist.php

    <table class="table table-striped table-bordered">
    <tr>
        <td><strong>ID</strong></td>
        <td><strong>Name</strong></td>
        <td><strong>Number</strong></td>
        <td><strong>Type</strong></td>
        <td><strong>Unit</strong></td>  
        <td><strong>Date</strong></td> 
    </tr> 
    <?php foreach($LIST as $list)
    {?>
    <tr>
        <td><?=$list->ID;?></td>
        <td><?=$list->Name;?></td>
        <td><?=$list->Letter_Number;?></td>
        <td><?=$list->Letter_Type;?></td>
        <td><?=$list->Unit;?></td>
        <td><?=$list->EMAIL;?></td>
    </tr>     
    <?php }?>
</table>

3.Model Login.php

    <?php

    class Login extends CI_Model {
    function HomeModel(){
    parent::Model();
    }

    function check_inlist(){                                                 
    $this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,EMAIL");
    $this->db->from('in_list');
    $query = $this->db->get();
    return $query->result();
    }

    }
    ?>