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.

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

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址