尝试使用codeigniter获取非对象的属性

时间:2017-04-18 07:27:00

标签: php codeigniter

[error page image][1] 

尝试使用codeigniter获取非对象的属性,如在数据库表中找不到记录时在视图页面中显示该错误消息请解决我的问题(当数据库表单中没有找到的记录应该显示在视图页面中)

控制器

 public function buyerdetails_form() {
            $this->load->helper('url');

             $result = $this->Profile_model->buyer_details();
            $data['c_address']=$result->address;
            $data['c_pincode']=$result->pincode;
            $data['c_district']=$result->district;
            $data['c_city']=$result->city;
            $data['c_state']=$result->state;
            $data['c_country']=$result->country;
            $this->load->view('buyerdetails_form',$data);

    }

模型

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
     return $query->row();

}

查看

<li class="has-sub" >
                            <span>Country</span>
                            <span><?php echo  $c_country; ?></span>
                            <a onclick="hideHasSub()" data-toggle="collapse" data-parent="#accordion" href="#collapseone">
                                <span class="profile-edit" onclick="" >Edit</span></span>
                            </a>
                            <div style="clear:both;"></div>
                        </li>
                        <div  class="sub">
                            <div class="panel-body ">
                                <div class="row">
                                    <div class="col-lg-8 col-lg-offset-2 text-center">

                                             <label style="display:inline;">Country</label> 
                                             <input type="text" name="id" value="<?php echo  $this->session->id; ?>" />
                                             <input style="width:50%;height:20px;" type ="text" class="form-control" name="country" value="<?php echo  $c_country; ?>"/></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"  style="margin:0 10px;">Save</button>-->

                                        <button  type="button" class="btn btn-warning cancel-name " >Cancel</button>
                                    </div>
                                </div>
                            </div>
                        </div>

1 个答案:

答案 0 :(得分:0)

在模型中更改此设置 $rows = $query->result_array();

并使用if(isset){}检查空