【问题标题】:I can't display the result of query in CodeIgniter我无法在 CodeIgniter 中显示查询结果
【发布时间】:2013-09-19 03:48:53
【问题描述】:

我无法将我的查询从数据库显示到 CodeIgniter。

我的表有“iddescriptiondate_stamp”。我只想得到date_stamp

这是我的代码:

型号

public function get_holidays()
{
    $this->db->select('date_stamp');
    $this->db->from('holidays');
    $query = $this->db->get();

    if($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $data[] = $query->row();
        }
    }
    else
    {
        $data = "";
    }
    return $data;
}

控制器:

    $holidays = $this->emp->get_holidays();
    //the result should be like this
    //$holidays=array("2013-09-20","2013-09-23", "2013-09-25");
    foreach($holidays as $holiday){
        // 
         echo $holiday['date_stamp'];
    }

【问题讨论】:

  • 我还收到一条消息“方法名称必须是字符串”
  • 你加载了model 吗?
  • 哦,是的。从一开始。我加了这个。 $this->load->model('mdl_employee','emp');

标签: php codeigniter


【解决方案1】:

我通常在视图中打印模型中的值,而控制器是我将页面导航到的位置。好吧,在模型中:

public function get_holidays()
{
    $this->db->select('date_stamp');
    $this->db->from('holidays');
    return $this->db->get();
}

在视野中:

$this->load->model('holiday_model');
$holidays = $this->holiday_model->get_holidays();

foreach( $holidays->result AS $items ) 
{
    echo $item->date_stamp;
}

【讨论】:

    【解决方案2】:

    在您的代码中,在 foreach ($query->result() as $row) 循环中

    $data[] = $query->row();
    

    应该是

    $data[] = $row;
    

    或者,只需从模型返回结果return $query->result() 并在控制器中执行循环,因为您又在做同样的事情。所以,你可以改用model

    if($query->num_rows() > 0)
    {
        return $query->result();
    }
    return false;
    

    然后,在您的controller 中,您可以这样做

    $holidays = $this->emp->get_holidays();
    if($holidays) {
        // do the loop or whatever you want
    }
    

    但是,请确保echoview 中的结果,希望你这样做,也不要忘记加载model

    【讨论】:

      【解决方案3】:

      更新模型

        public function get_holidays()
        {
          $data = array();
          $this->db->select('date_stamp');
          $this->db->from('holidays');
           $query = $this->db->get();
      
      if($query->num_rows() > 0)
      {
          foreach ($query->result() as $row)
          {
      
              $data[]=$row->date_stamp; //changed here
             // array_push($data, $row->date_stamp); 
      
          }
      }
      else
      {
          $data = array();
      }
      return $data;
      }
      

      更新的控制器

          $this->load->model('holiday_model');
      
          $holidays = $this->emp->get_holidays();
          //the result should be like this
          //$holidays=array("2013-09-20","2013-09-23", "2013-09-25");
      
      
          if($holidays) { //checking $holiday 
            foreach($holidays as $holiday){
               echo $holiday; //changed here
           }
          } else {
              echo 'No Holiday Found; //changed here
          }
      

      现在可以正常使用了

      【讨论】:

      • array_push($data, $row->date_stamp);$data[]=$row->date_stamp 不必要且昂贵。
      猜你喜欢
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多