使用CodeIgniter中的user_id回显用户信息详细信息

时间:2017-09-20 10:12:55

标签: php codeigniter codeigniter-3

我正在CodeIgniter中创建一个网站,以便用户可以将产品添加到我的网站。当用户上传产品时,user_id将保存到名为“products”的表和名为user_id的行中。在产品详细信息页面上,我想回显一些用户信息,例如上传特定产品的用户名。或产品所有者的用户位置。现在很明显我不能这样回应:

 <div class="user_id"><?php echo $product['user_id']; ?> </div>

因为它只会回显用户ID。但是,我如何回应该产品所有者的用户名?

我希望有人可以帮助我

这是我想要回应产品所有者详细信息的产品详细信息视图页面:

    <div class="container">
 <div class="row">
    <div class="col-md-4" style="margin-top:24px;">
    <img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>

<div class="col-md-5">
   <div class="product_naam"> <?php echo $product['product_naam']; ?>  </div>
   <h2>Over dit cadeau</h2>
     <div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
   </div>
   <div class="col-md-3">
     <a href="<?php echo base_url() ?>/Cadeauaanvragen"> <button type="button" class="btn btn-default">Cadeau aanvragen</button></a>
     <div class="aangeboden_door">   Aangeboden door:  Peter      </div>
     </div>
     </div>
</div> 

这是控制器中的详细信息功能:

 public function details($product_id) {
 //get product details
 $data['product'] = $this->Product_model->get_product_details($product_id);

 //laad view
 $data['main_content'] = 'details';
 $this->load->view('details',$data);

}

这是我模型中的获取产品详细信息功能:

public function get_product_details($product_id) {
    $arrReturn = array();
    $this->db->select('*');
    $this->db->from('products');
    $this->db->where('product_id', $product_id);
    $query = $this->db->get();
    $result = $query->result_array();
    if (!empty($result)) {
        $arrReturn = $result[0];
    }
    return $arrReturn;
}

这是用户身份验证登录注册文件:

 public function login()
{
    //laad login view

        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
        if ($this->form_validation->run() == TRUE) {

        $email = $_POST['email'];
        $wachtwoord = ($_POST['wachtwoord']);


        //check gebruiker in database
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where(array('email'=>$email, 'wachtwoord' => $wachtwoord));
        $query = $this->db->get();

        $user = $query->row();
        //Als gebruiker bestaat
        if($user->email) {

            //tijdelijke berichten wanneer ingelogd of inloggen niet gelukt
            $this->session->set_flashdata("success","U bent nu ingelogd");

        $_SESSION['user_logged'] = TRUE;
        $_SESSION['user_id'] = $user->user_id;
        $_SESSION['email'] = $user->email;
        $_SESSION['voornaam'] = $user->voornaam;
        $_SESSION['achternaam'] = $user->achternaam;
        $_SESSION['woonplaats'] = $user->woonplaats;
        $_SESSION['straat'] = $user->straat;
        $_SESSION['huisnummer'] = $user->huisnummer;
        $_SESSION['postcode'] = $user->postcode;
        $_SESSION['beschrijving'] = $user->beschrijving;
        $_SESSION['profiel_foto'] = $user->profiel_foto;


        //link naar profiel pagina
        redirect("user/profile", "refresh");
        } else {

            $this->session->set_flashdata('error','Invalid email or password');
            //wanneer er een foutmelding is link weer naar de login pagina
            redirect("https://kadokado-ferran10.c9users.io/auth/login" , "refresh");
        }



        }
              //laad login view
              $this->load->view('login');
}



  public function register()
{

    if (isset($_POST['register'])) {
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
        $this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
        $this->form_validation->set_rules('wachtwoord', 'Herhaal wachtwoord', 'required|min_length[5]|matches[wachtwoord]');
        $this->form_validation->set_rules('achternaam', 'Achternaam', 'required');
        $this->form_validation->set_rules('postcode', 'Postcode', 'required');
        $this->form_validation->set_rules('woonplaats', 'Woonplaats', 'required|min_length[3]');
        $this->form_validation->set_rules('beschrijving', 'Beschrijving', 'required|min_length[5]');
        $this->form_validation->set_rules('huisnummer', 'Huisnummer', 'required');
        $this->form_validation->set_rules('geboortedatum', 'Geboortedatum', 'required');
        $this->form_validation->set_rules('geslacht', 'Geslacht', 'required');
        //If form validation true
        if ($this->form_validation->run() == TRUE) {
           // echo 'form validated';


      $target_dir = "upload/";
                $target_file = $target_dir . time().basename($_FILES["profiel_foto"]["name"]);
                $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
                $imgName = time().basename($_FILES["profiel_foto"]["name"]);
                move_uploaded_file($_FILES["profiel_foto"]["tmp_name"], $target_file);


            //voeg gebruiker toe aan database
            $data = array (
                'voornaam'=>$_POST['voornaam'],
                'achternaam'=>$_POST['achternaam'],
                'email'=>$_POST['email'],
                'wachtwoord'=>  ($_POST['wachtwoord']),
                'startdatum'=>date('Y-m-d'),
                'postcode'=>$_POST['postcode'],
                'huisnummer'=>$_POST['huisnummer'],
                'woonplaats'=>$_POST['woonplaats'],
                'beschrijving'=>$_POST['beschrijving'],
                'geboortedatum'=>$_POST['geboortedatum'],
                'geslacht'=>$_POST['geslacht'],
                'profiel_foto'=>$imgName

                );
            $this->db->insert('users',$data);

            $this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
            redirect("auth/register", "refresh");
        }

数据库信息:

表用户:

user_id (primary_key)
email
voornaam
achternaam
beschrijving

表产品:

product_id (primary_key)
product_naam
product_beschrijving
user_id
category_id

1 个答案:

答案 0 :(得分:0)

您可以按以下代码更改模态函数:

public function get_product_details($product_id) {
        $this->db->select('products.*, users.*');
        $this->db->from('products');
        $this->db->join('users', 'users.user_id = products.user_id', 'left');
        $this->db->where('product_id', $product_id);
        $query = $this->db->get();
        $result = $query->row_array();
        if (!empty($result)) {
            return $result;
        } else {
            return array();
        }
    }