无法将数据从模型传递到视图

时间:2016-07-19 01:50:16

标签: php codeigniter

您好我将一些数据从我的模型传递到我的视图时遇到问题,使用以下代码:

控制器:

class Welcome extends CI_Controller {


    function __construct()
     {
        parent::__construct();

        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('url');

     }
    public function testing(){

      $query = $this->Model->getEmployees();
      if(isset($query)){
       $data['fds'] =  $query;
      }

      $this->load->view('UserPage', $data);
     }
}

型号:

class Model extends CI_Model{

 function __construct()
     {
        parent::__construct();

        $this->load->library('session');
     }

 public function getEmployees(){
        $this->db->select('*');
         $this->db->where("idcliente='".$_SESSION['userid']."'");
         $query=$this->db->get('marcacao');
        return $query->num_rows() > 0 ? $query->result() : NULL;
 }
}

查看:

<table class="table table-striped table-bordered">
     <tr> 
      <td><strong>Employee Id</strong></td>
      <td><strong>First Name</strong></td>
      <td><strong>Last Name</strong></td>
    </tr> 
     <?php foreach($fds as $employee){?>
     <tr>
      <td><? =$employee->data;?></td>
      <td><? =$employee->hora;?></td>
      <td><? =$employee->tipo;?></td>
     </tr>     
        <?php }?>  
    </table>

我已经尝试了几件我在这里见过但却无效的事情......希望你能帮助我。

4 个答案:

答案 0 :(得分:0)

首先要改变这一点:

  $query = $this->Model->getEmployees();
  if(isset($query)){
   $data['fds'] =  $query;
  }

成为:

$id = $_SESSION['userid'];
$data["fds"] = $this->Model->getEmployees($id);

现在你的模特:

public function getEmployees($id){
     $this->db->select('*');
     $this->db->where("idcliente=",$id);
     $query=$this->db->get('marcacao');
     return $query->result_array();

现在在您的视图中()

<?php
 if($fds != NULL){
 foreach($fds as $employee){?>
   <tr>
     <td><?=$employee["data"];?></td>
     <td><?=$employee["hora"];?></td>
     <td><?=$employee["tipo"];?></td>
   </tr>     
 <?php 
 }
  }?>  

答案 1 :(得分:0)

首先,正如我在代码中看到的那样,变量$data在if块中初始化,因此其范围将保持在if块中。在调用模型之前,您可以加载模型。

public function testing(){
      $data = array();
      $this->load->model("Model"); $query = $this->Model->getEmployees(); if(isset($query)){ $data['fds'] = $query; } $this->load->view('UserPage', $data); }

答案 2 :(得分:0)

尝试这样做....

对于控制器:

AD-Computer

对于模型:

class Welcome extends CI_Controller {


function __construct()
 {
    parent::__construct();

    $this->load->library('session');
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->model('Model'); 

 }
public function testing(){

  $query = $this->Model->getEmployees(); // replace "Model_name" with the name of your model

  $data = array();
  if(isset($query)){
   $data['fds'] =  $query;
  }

  $this->load->view('UserPage', $data);
 } }

现在名为UserPage.php的视图

 class Model extends CI_Model{

 function __construct()
     {
        parent::__construct();

        $this->load->library('session');
        $this->load->database();
     }

 public function getEmployees(){
        $this->db->select('*');
         $this->db->where("idcliente='".$_SESSION['userid']."'");
         $query=$this->db->get('marcacao');
         if($query->num_rows() > 0) 
             { return $query->result(); } 
               else{ return NULL; }
 }
}

答案 3 :(得分:0)

想出来......不得不把它放在模型函数中:

$this->load->database();