无法从codeigniter php

时间:2016-11-01 10:36:05

标签: php codeigniter

一旦用户登录到站点无法从数据库获取数据获取空白页面,如果我写foreach条件这里是我的代码。获取用户名和登录验证工作正常。

控制器:

public function index()
{
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
}
else{

    $this->load->view('welcome');
}
}

型号:

function getprofiledata($id)
{
    $this->db->select('profile_details.*');     
    $this->db->from('profile_details');     
    $this->db->where(array('profile_details.profile_id'=>$id));     
    $q=$this->db->get();        
    if($q->num_rows()>0)
      {
    return $q->result();
        }
    else
    {
    return false;
    }
}

查看:

<div id="legend">
    <legend class="">Profile Information</legend>
</div>   
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
            <?php foreach($records as $r):?>
    <form action="<?php echo base_url();?>profile/updateprofile" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
    <?php
        echo form_hidden('profile_id',$r->profile_id);
    ?>
    <div class="form-group">
      <label class="control-label col-sm-2 " for="name">Name:</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2 " for="profilename">Profile Name:</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="profile_name" placeholder="Enter Profile name">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2 " for="designation">Designation:</label>
      <div class="col-sm-4 col-sm-offset-1">
        <input type="text" class="form-control" id="designation" placeholder="Enter Designation">
      </div>
    </div>
    <button type="submit" class="btn">Submit</button>
    </form>
    <?php endforeach;endif;?>

2 个答案:

答案 0 :(得分:0)

您在第$data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));行选择了错误的细分编号。

请注意,段计数从零开始,因此段号3实际上是uri中的第4个。

如果您在会话中保留用户ID,则应将$data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));替换为$records = $this->profile_model->getprofiledata($this->session->userdata('profile_id'))‌​‌​‌​;。你已经完成了。

答案 1 :(得分:0)

在登录过程中添加一个新会话,如登录模型中的bellow:

<?php
public function login_user($user_name = '', $password=''){ 
    $userdetails = array( 
    'email' => $user_name, 
    'password' => md5($password), 
    'status'=>1,     
    ); 

    $this->db->where($userdetails); 
    $query = $this->db->get('profile_details');  

    if($query->num_rows()): 
        $user = $query->result(); 
        $sess_arry = array( 
            'profile_id' => $user[0]->profile_id, // add new session profile_id
            'first_name' => $user[0]->first_name 
        );  
        $this->session->set_userdata('admin_logged_in', $sess_arry);    //add admin details to session   
        return true; 
    else: 
        return false; 
    endif; 
}
?>

有些人会改变你的index方法,如下:

<?php
public function index() 
{ 
    if($this->session->userdata('admin_logged_in')){ 
        $data['admin_details'] = $this->session->userdata('admin_logged_in'); 
        $data['country'] = $this->signup_model->getcountry();   
        $data['states'] = $this->profile_model->getstates();

        $profile_id = $this->session->userdata('profile_id');
        $records = $this->profile_model->getprofiledata($profile_id)‌​‌​;

        $data['records']= $records; 

        $data['mainpage']='profile'; 
        $this->load->view('templates/template',$data); 
        $this->load->view('templates/sidebar',$data); 
    } 
    else{ 
        $this->load->view('welcome'); 
    } 
} 
?>

我添加了新的3行,因为我认为您在$this->uri->segment(3)

中没有正确获取个人资料ID

所以,

$profile_id = $this->session->userdata('profile_id');
$records = $this->profile_model->getprofiledata($profile_id)‌​‌​; 

$data['records']= $records;