doupin1073 2014-08-22 05:54
浏览 22
已采纳

获取未找到记录而不是为foreach()提供的无效参数 - CodeIgniter

I have the following code in CodeIgniter Controller.It returns the Invalid argument supplied for foreach() error when it enable to retrieve the data from the model. I want to display the "No records found" instead of the Invalid argument.. error .

Here is my full code

Controller

function registered_customers_bod(){
        // get the data and pass to records Variable
        if($query =  $this->mod_reports->registered_customers_bod())
        {
            $data['records'] = $query ;
        }else{
            $data['records'] = 'No records found';
        }
        $data['report_name'] = $this->input->post('report_name');
        // load the view passing data Variable
        $this->load->view('admin/reports/user_reports_bod.html',$data);
    }

View - user_reports_bod

  <?php if (isset($records)) : $i = 1; $delivered_qty_total  = 0;   foreach ($records as $row) : ?>
        <tr>
        <td><?php  echo $i++; ?></td>
        <td><?php  echo $row->cus_id; ?></td>
        <td><?php  echo $row->cus_name; ?></td>
        <td><?php  echo $row->cus_email; ?></td>
        <td><?php  echo $row->cus_phone; ?></td>
        <td><?php  echo $row->cus_mobile; ?></td>
        <td><?php  echo $row->cus_addr_city; ?></td>
        <td><?php  echo $row->user_status; ?></td>
        <td><?php  echo gmdate("Y/m/d", $row->user_timestamp); ?></td>
        </tr>
<?php  endforeach; ?>
<?php  endif; ?>

Model

function registered_customers_bod(){
        $to = new DateTime($this->input->post('to'));
        $from = new DateTime($this->input->post('from'));

        $this->db->select(array(
            'tbl_customer_registration.cus_id',
            'tbl_customer_registration.cus_name',
            'tbl_customer_registration.cus_email',
            'tbl_customer_registration.cus_phone',
            'tbl_customer_registration.cus_mobile',
            'tbl_customer_registration.cus_addr_no',
            'tbl_customer_registration.cus_addr_street',
            'tbl_customer_registration.cus_addr_city',
            'tbl_user_registration.user_status',
            'tbl_user_registration.user_reason_status',
            'tbl_user_registration.user_timestamp',
        ));
        $this->db->from('tbl_customer_registration');
        $this->db->join('tbl_user_registration', 'tbl_user_registration.user_id=tbl_customer_registration.user_id');
        $this->db->group_by("tbl_customer_registration.cus_id"); //view single record that contain two contact numbers
        $this->db->where('user_timestamp >=',$to->getTimestamp());
        $this->db->where('user_timestamp <=',$from->getTimestamp());
        $query = $this->db->get();
        return $query->result();
    }
  • 写回答

1条回答 默认 最新

  • dorkahemp972157683 2014-08-22 06:06
    关注
       if($query =  $this->mod_reports->registered_customers_bod()){
            $data['records'] = $query ;
        }else{
            $data['records'] = new stdClass(); // change here
        }
    

    the reason behind the error is when no record is found from db your $data['records'] is a String and which is surely Invalid argument for foreach()

    and instead of isset($records) in view use empty($records)

    and put else in view EDIT

    $arr = (array)$records;
    if(!empty($arr)){
        $i = 1;
        $delivered_qty_total  = 0;
        foreach($records as $row) { ?>
         <tr>
         <td><?php  echo $i++; ?></td>
         <td><?php  echo $row->cus_id; ?></td>
         <td><?php  echo $row->cus_name; ?></td>
         <td><?php  echo $row->cus_email; ?></td>
         <td><?php  echo $row->cus_phone; ?></td>
         <td><?php  echo $row->cus_mobile; ?></td>
         <td><?php  echo $row->cus_addr_city; ?></td>
         <td><?php  echo $row->user_status; ?></td>
         <td><?php  echo gmdate("Y/m/d", $row->user_timestamp); ?></td>
         </tr>
     <?php } ?>
    }
    else { echo "No records found";}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看