[英]I can't display the result of query in CodeIgniter
I can't display my query from database to CodeIgniter. 我无法将我的查询从数据库显示到CodeIgniter。
My table has " id
, description
, date_stamp
". 我的表有“ id
, description
, date_stamp
”。 I just want to get only the date_stamp
. 我只想获得date_stamp
。
Here is my code: 这是我的代码:
Model 模型
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;
}
Controller: 控制器:
$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'];
}
In your code, inside foreach ($query->result() as $row)
loop 在你的代码中,在foreach ($query->result() as $row)
里面foreach ($query->result() as $row)
循环
$data[] = $query->row();
should be 应该
$data[] = $row;
or, just return the result return $query->result()
from the model and do the loop in the controller, because you are again doing same thing. 或者,只是从模型返回结果return $query->result()
并在控制器中执行循环,因为您再次执行相同的操作。 So, you can do in your model
instead 所以,你可以在你的model
做
if($query->num_rows() > 0)
{
return $query->result();
}
return false;
Then, in your controller
you may do 然后,在您的controller
您可以这样做
$holidays = $this->emp->get_holidays();
if($holidays) {
// do the loop or whatever you want
}
But, make sure to echo
the result in view
, hope you do so, also, don't forget to load the model
. 但是,请确保在view
echo
显结果,希望您这样做,也不要忘记加载model
。
I usually print the value from model in view while controller is where I navigate the page to. 我通常在视图中打印模型中的值,而控制器是我浏览页面的位置。 Well, in model: 嗯,在模型中:
public function get_holidays()
{
$this->db->select('date_stamp');
$this->db->from('holidays');
return $this->db->get();
}
and in view: 在视野中:
$this->load->model('holiday_model');
$holidays = $this->holiday_model->get_holidays();
foreach( $holidays->result AS $items )
{
echo $item->date_stamp;
}
Updated Model 更新的模型
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;
}
Updated Controller 更新控制器
$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
}
Now it will work fine 现在它会正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.