您好,我只是想知道如何只显示数据库中的一行。我可以使用 foreach 循环显示,但没有 foreach 循环就无法显示这是我的代码:
此控制器用于查看模型中的数据
public function view_main_body() { $data = array(); $data['main_body_details'] = $this->sa_model->select_main_body(); $data['admin_content'] = $this->load->view('admin/view_page_content', $data, true); $this->load->view('admin/index', $data); }
here is the model which show data from database
public function select_main_body() { $this->db->select('*'); $this->db->from('tbl_main_body'); $result_query = $this->db->get(); $result = $result_query->result(); return $result; }
here is the welcome controller which show in frontpage
> public function index() > { > $data=array(); > $data['menu_details'] = $this->sa_model->select_menu(); > $data['maincontent']=$this->load->view('home_message',$data,true); > $this->load->view('index',$data); > }
i can show with foreach loop
> <?php foreach ($main_body_details as $mb_details) { ?> > <?php echo $mb_details->main_body; ?> > <?php } ?>
但是没有foreach循环就无法显示。 当我尝试其给定的错误时。我想知道如何在没有 foreach 循环的情况下发布数据,该循环显示没有错误
提前致谢
更新此函数
public function select_main_body() {
$this->db->select('*');
$this->db->from('tbl_main_body');
$result_query = $this->db->get();
$result = $result_query->row();
return $result;
}
现在删除foreach只是看起来像
<?php echo $main_body_details->main_body; ?>
在
模型中使用
$result = $result_query->row();
return $result;
这只返回一行。如果您有多行,它将返回第一行
并且鉴于您可以访问
$main_body_details->main_body
参考