【问题标题】:How to view record from DB with CodeIgniter如何使用 CodeIgniter 从数据库中查看记录
【发布时间】:2017-06-21 15:52:09
【问题描述】:

我尝试按照site 上的教程通过 Code Igniter 从 DB 中查看我的记录,但它显示了错误消息

我可以尝试什么来识别它?

以下是查看“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>

这里是控制器“Main.php”

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

这里是模型“login.php”

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

【问题讨论】:

  • 您在 check_inList() 操作中遇到错误吗?
  • 它无法识别视图中的 $LIST。不知道是怎么互相交流的。 @B.Desai
  • 我在问你“你什么时候会收到这个错误?”给你的网址示例
  • 打印控制器中$query的值,即print_r($query);退出;
  • 我想你忘了在控制器 Main.php 中加载模型。要在控制器中加载模型,只需编写 $this->load->model('login');

标签: php html mysql twitter-bootstrap codeigniter


【解决方案1】:
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 }?>    

【讨论】:

    【解决方案2】:

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

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

    然后你在视图中打印日期列&lt;td&gt;&lt;?=$list-&gt;Date;?&gt;&lt;/td&gt;

    1.控制器 主.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();
        }
    
        }
        ?>
    

    【讨论】:

    • 不可能。我尝试了所有代码并成功获得了结果
    • 我也想知道我的代码,你能在这上面命令你所有的代码,然后我会一步一步地检查它来找出我的错误
    • @PhearumChheang 我把
    • @PhearumChheang 检查您在模型中使用的表结构
    • php版本有问题吗?
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多