如何使用会话在codeigniter中显示用户配置文件数据?

时间:2018-02-05 11:13:22

标签: php codeigniter

我是CI的新手,我已经制作了登录系统,但我试图在用户个人资料视图页面上显示用户名和电子邮件中的用户个人资料数据。我很想尝试,但我无法解决这个问题。这是我的代码。 可以告诉我如何在个人资料视图页面中显示用户个人资料数据吗?我有太多尝试,但没有解决方案可用于在codeigniter中显示用户配置文件数据。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class UserLogin extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper(array('form', 'url'));
        $this->load->model('login_model');
    }

    public function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->view('UserLoginPage');
    }

    public function userLoginProcess()
    {
      // $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
      // echo "login reached";
      $this->form_validation->set_rules('username', 'Username', 'required|alpha|trim');
      $this->form_validation->set_rules('password', 'Password','required');
      $this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
      $this->session->set_flashdata("<p class='text-danger'>","</p>");


        if ($this->form_validation->run() ){
            //username aur password araha hay
              $username = $this->input->post('username');
                    $password = $this->input->post('password');

              $this->load->model('login_model');

              //$loginObj session ko check variable haa..!
              $loginObj = $this->login_model->login_valid($username,$password);
              if($loginObj){
                   // print_r($loginObj->password);
                   $this->session->set_userdata('userSessionObj', $loginObj);
                   //print_r($loginObj);
                           $this->load->view('userDashboard');
                }
                else{
               // echo "<script>alert('UserName And Passowrd Are Wrong....!!!! ');</script>";
              // $this->session->set_flashdata('error', 'Invalid Username and Password');
              $this->load->view('UserLoginPage');
              // echo "<script language=\"javascript\">alert('Username And Password Are Worng');</script>";
              $this->session->set_flashdata('error','<p class="text-danger"> you entered invalid username and password');
                }   // end of else

        }  // end of public function
        else
            {

                $this->load->view('userLoginPage');

            } // end of else

      }  //end of function
    //logout function Start
    public function logout()
    {
      $this->session->sess_destroy();
      $this->session->unset_userdata('username','password');
       return redirect("userLogin");
    }
        //logout function End
    public function register()
    {

       $this->load->view('registered');

    } //end of register function
    public function preRegister()
    {
      $this->form_validation->set_rules('username', 'Username', 'required|alpha|trim|min_length[5]|max_length[12]|is_unique[user.username]');
      $this->form_validation->set_rules('email', 'email','required|valid_email|is_unique[user.email]');
      $this->form_validation->set_rules('password', 'Password','required');
      $this->form_validation->set_rules('confirmpassword', 'ConfirmPassword','required|matches[password]');


      $this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");

      if($this->form_validation->run())
      {
        $store = array(
        'username' => $this->input->post('username'),
        'email' => $this->input->post('email'),
        'dateOfbirth' => $this->input->post('dateOfbirth'),
        'password' => $this->input->post('password'),
        'confirmpassword'=> $this->input->post('confirmpassword'),
        'gender' => $this->input->post('gender')
        );

          $this->login_model->insert_mod($store);
          redirect('UserLogin/index');

      } // end of if form_validation
      else
      {
          $this->load->view('registered');

      } // end of else

  }  //end of preRegister function

  public function employess()   // employes of add ka page view kr rha haaaa
    {
            $this->load->view('userDashboard');
    }                          // employes of add ka page view kr rha haaaa

  public function proEmployess()
  {

    // $this->load->view('addEmployess');
    // $this->load->view('addEmployess');
    $employessData = array(
    // 'id' => $this->input->post('id'),
    'name' => $this->input->post('name'),
    'address' => $this->input->post('address'),
    'department' => $this->input->post('department')
    );

      $this->login_model->employess_add($employessData);
      redirect('UserLogin/employess');
  }

  public function myProfile(){

    // $myProfile = $this->session->userdata();
    $this->load->view('headerDashboard.php');
    $myProfiledata ['profiles'] = $this->login_model->profileVeiw();
    $this->load->view('myProfileView', $myProfiledata);
    $this->load->view('footerDashboard.php');
  }

  //  public function myProfile(){
  //
  //     $this->load->view('headerDashboard');
  //     //$myProfiledata ['datas'] = $this->login_model->veiw_Employess();
  //     //$employessRecord['datas'] = $this->login_model->veiw_Employess();
  //     $this->load->view('myProfileView');
  //     $this->load->view('footerDashboard');
  //
  // }
}
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_model extends CI_Model{

    public function login_valid($username ,$password)
    {
        $q = $this->db->where(['username'=>$username, 'password'=>$password])
                        ->get('user');


        if( $q->num_rows() )
        {
            //return pori row ho rhe ha jo database sa arhe haaa
             return $q->row();
            // return $q->row()->id;
            // return TRUE;
        }
else
        {
            return FALSE;
        }

    }

    public function insert_mod($store)
    {
        $this->db->insert('user', $store);
    }
    public function employess_add($employessData)
    {
        $this->db->insert('employessRecord',$employessData);
    }
    public function view_employess(){

        $query = $this->db->get('employessrecord');
        return $query;
    }public function profileVeiw(){

              $queries = $this->db->get('user');
             //$queries = $this->session->userdata();
            // print_r($queries);
            return $queries;

        }
}
<div class="col-xl-6">
        <div class="card" >
            <div class="card-header">
                <h4>Profile Details</h4>
            </div><br>
            <?php foreach($profiles->result() as $profile):?>
            <div class="col-md-8">
              <label for="name">Username:</label>
              <?php echo $profile->username;?>
            </div><br><br>
            <div class="col-md-8">
              <label for="address">Email:</label>
              <?php echo $profile->email;?>
            </div><br><br>
            <div class="col-md-8">
              <label for="address">Passowrd:</label>
              <?php echo $profile->password;?>
            </div><br><br>
            <div class="col-md-8">
              <label for="address">Gender:</label>
              <?php echo $profile->gender;?>
            </div><br><br>
          <?php endforeach; ?>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

有开口标签php标签

所以您从表单中收到数据并将其插入数据库$this->login_model->insert_mod($store)

当你调用myProfile函数时,你在$myProfiledata ['profiles'] = $this->login_model->profileVeiw();获取数据并将其传递给view。如果这是正确的,那么首先尝试打印$myProfiledata然后传递到视图,如果一切正常,那么尝试在“myProfileView”视图上打印$profiles

$这 - &GT;会话而&GT;用户数据( 'userSessionObj');  在这里你可以检查会话数据