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";}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP