CodeIgniter foreach
I am fairly new to CodeIgniter and I'm sure there's a simple answer. I need to determine if there are records returned prior to executing this line of code in my VIEW:
foreach ($announcements->result_array() as $announcement_data){
My disconnect is that I can't check if array is empty because it is not an array yet until the foreach line is executed. Checking if $announcements is empty also doesn't work.
CONTROLLER:
public function summary() {
$this->load->model('Propinfo_model');
$data['propid'] = "516";
$data['propinfo'] = $this->Propinfo_model->get_propinfo($data['propid']);
// query for proposal info
$data['announcements'] = $this->Propinfo_model->get_announcements($data['propid']);
$this->load->view('summary_view', $data);
}
MODEL:
// Select all records from 'prop_announcements' table per prop ID
public function get_announcements($propid) {
// Build query
$this->db->select('*');
$this->db->from('prop_announcements P');
$this->db->join('ssp_users', 'P.user = ssp_users.userid');
$this->db->where('P.propid', $propid);
return $this->db->get();
}
VIEW:
foreach ($announcements->result_array() as $announcement_data){
echo $announcement_data['user']; // example field
}
我是CodeIgniter的新手,我确信这是一个简单的答案。 我需要确定在我的VIEW中执行这行代码之前是否有返回的记录: p>
foreach($ announcements-> result_array()as $ announcement_data){\ n code> pre>
我的断开连接是我无法检查数组是否为空,因为在执行foreach行之前它不是数组。 检查$ announcements是否为空也不起作用。 p>
CONTROLLER: strong> p>
公共功能摘要(){
$ this-> load-&gt ; model('Propinfo_model');
$ data ['propid'] =“516”;
$ data ['propinfo'] = $ this-> Propinfo_model-> get_propinfo($ data ['propid'] );
//查询提案信息
$ data ['announcements'] = $ this-> Propinfo_model-> get_announcements($ data ['propid']);
$ this-> load-> view('summary_view',$ data);
}
code> pre>
MODEL: strong> p>
//从'中选择所有记录' 每个道具ID的prop_announcements'表
公共函数get_announcements($ propid){
//构建查询
$ this-> db-> select('*');
$ this-> db-> from('prop_announcements P');
$ this-> db-> join('ssp_users','P.user = ssp_users.userid');
$ this-> db-> where('P.propid',$ propid) ;
返回$ this-> db-> get();
}
code> pre>
查看: strong> p>
foreach($ announcements-> result_array()as $ announcement_data){
echo $ announcement_data ['user']; //示例字段
}
code> pre>
div>
You use
if ($announcements->num_rows() == 0) {
//code for no rows
}
https://www.codeigniter.com/userguide3/database/results.html