dongshan1036 2017-08-24 21:18
浏览 17
已采纳

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
}
  • 写回答

1条回答 默认 最新

  • dqysi86208 2017-08-24 21:20
    关注

    You use

    if ($announcements->num_rows() == 0) {
     //code for no rows
    }
    

    https://www.codeigniter.com/userguide3/database/results.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式