簡體   English   中英

我無法在CodeIgniter中顯示查詢結果

[英]I can't display the result of query in CodeIgniter

我無法將我的查詢從數據庫顯示到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'];
    }

在你的代碼中,在foreach ($query->result() as $row)里面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
}

但是,請確保在view echo顯結果,希望您這樣做,也不要忘記加載model

我通常在視圖中打印模型中的值,而控制器是我瀏覽頁面的位置。 嗯,在模型中:

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

更新的模型

  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
    }

現在它會正常工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM