duannao3402 2017-04-13 07:36
浏览 30
已采纳

没有在数据库中找到的记录,如错误显示为foreach()提供的无效参数和视图页面不显示

if records does not exist in database like that form i want to display but empty form displaying

Model code

  • No record found in database, Invalid argument supplied for foreach() like that error display and in view page form will not display if
    data found in database it will display please help me

    public function buyer_details() {

            $this->db->select('*');
            $this->db->from('customer_otherdetails');
            $this->db->where('customerid_fk', $this->session->id);
            $query = $this->db->get();
          //  return $query->result();
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    //add all data to session
                    $newdataa = array(
    
                        'address' => $row->address,
                         'pincode' => $row->pincode,
                         'city' => $row->city,
                         'district' => $row->district,
                         'state' => $row->state,
                        'country' => $row->country,
    
                    );
                }
                $this->session->set_userdata($newdataa);
                return $query->result();
            }
        }
    

controller code

$data['buyerdetails'] = $this->Profile_model->buyer_details();
        $this->load->view('buyerdetails', $data);

view page

<?php
        foreach ($buyerdetails as $row) {
            ?>
<li class="has-sub">
                                            <span>Area</span>
                                            <span><?php echo $row->address; ?></span>
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapsefive">

                                                <?php
                                                if (empty($buyerdetails) == $this->session->userdata('address')) {
                                                    ?>
                                                    <span class="profile-edit">Add</span>
                                                <?php } else { ?>
                                                    <span class="profile-edit">Edit</span>
                                                <?php } ?>
                                            </a>
                                            <div style="clear:both;"></div>
                                        </li>
                                        <div class="sub">
                                            <div class="panel-body ">
                                                <div class="row">
                                                    <div class="col-lg-6 col-lg-offset-3 text-center">

                                                        <label style="display:inline;">Area</label> 
                                                         <input type="hidden" name="id" value="<?php echo $row->customerid_fk; ?>" />
                                                        <input style="width:50%;height:20px;" type ="text" name="address" class="form-control" value="<?php echo $row->address; ?>"/></br>
                                                        <p style="font-size: 12px;"><b>Note:</b> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
                                                        <input type="submit" value="Save" class="btn btn-success" style="width:70px;">
                                                        <!--                                            <button type="button" class="btn btn-success">Save</button>-->
                                                        <button type="button" class="btn btn-warning cancel-name">Cancel</button>

                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <?php } ?>
  • 写回答

1条回答 默认 最新

  • dongzong2017 2017-04-13 15:05
    关注

    The problem is that buyer_details does not always return something. You need to put a return outside of the if($query->num_rows() > 0) condition.

    public function buyer_details()
    {
        $this->db->select('*');
        $this->db->from('customer_otherdetails');
        $this->db->where('customerid_fk', $this->session->id);
        $query = $this->db->get();
        $rows = $query->result(); //so you only have to call result once
        if($query->num_rows() > 0)
        {
            foreach($rows as $row)
            {
                //add all data to session
                $newdataa = array(
                  'address' => $row->address,
                  'pincode' => $row->pincode,
                  'city' => $row->city,
                  'district' => $row->district,
                  'state' => $row->state,
                  'country' => $row->country,
                );
            }
            $this->session->set_userdata($newdataa);
            //put the return outside the if
            //return $query->result();
        }
        return $rows; //this will be an empty array if no data found
    }
    

    $rows may be an empty array if no data is found. But foreach won't complain about an empty array. An empty array is still a valid argument.

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

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上