Yosra Ammar
Yosra Ammar

Reputation: 3

show data with join table in codeigniter

i want to show all reservation of user this is my tables:

CREATE TABLE IF NOT EXISTS `reservation` (

id_passager int(11) NOT NULL, idvoyage int(11) NOT NULL, etat varchar(20) NOT NULL, dateres timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id_passager,idvoyage), KEY iduser (id_passager,idvoyage), KEY idvoyage (idvoyage) );

CREATE TABLE IF NOT EXISTS `trajet` (

id int(11) NOT NULL AUTO_INCREMENT, villedep varchar(100) NOT NULL, villearrivee varchar(100) NOT NULL, PRIMARY KEY (id) );

CREATE TABLE IF NOT EXISTS `user` (

id int(11) NOT NULL AUTO_INCREMENT, nom varchar(50) NOT NULL, prenom varchar(50) NOT NULL, datenaiss date NOT NULL, sexe varchar(10) NOT NULL, email varchar(50) NOT NULL, numtel varchar(14) NOT NULL, mdp varchar(16) NOT NULL, imageprofil varchar(100) DEFAULT NULL, nbsignal int(11) DEFAULT NULL, pseudo varchar(20) NOT NULL, musique varchar(20) DEFAULT NULL, fumeur varchar(20) DEFAULT NULL, bavardage varchar(20) DEFAULT NULL, sexecovoiturage varchar(20) DEFAULT NULL, description varchar(200) DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY email (email), UNIQUE KEY numtel (numtel), UNIQUE KEY pseudo (pseudo) );

CREATE TABLE IF NOT EXISTS `vehicule` (

matricule varchar(50) NOT NULL, marque varchar(50) NOT NULL, modele varchar(50) NOT NULL, confort varchar(50) NOT NULL, couleur varchar(20) NOT NULL, image1 varchar(100) DEFAULT NULL, id_utilisateur int(11) NOT NULL, PRIMARY KEY (matricule), KEY id_utilisateur (id_utilisateur) );

and this is my controller function:

public function reservation()
{
    $this->load->view('headerprofile');
    $data['reservation']=$this->User_model->getReservation();
    $this->load->view('trajet/reservation',$data);
    $this->load->view('footerprofile');
}

this is my model:

function getReservation()
{
    $results = array();

    $id=$this->session->userdata('id');
    $this->db->select('res.*, v.*, t.*');
    $this->db->from('reservation AS res, voyage AS v, trajet AS t,user AS u,vehicule AS veh ');
    $this->db->where('res.id_passager',$id);
    $this->db->where('u.id',$id);
    $this->db->where('res.idvoyage=v.id');
    $this->db->where('t.id=v.id_trajet');
    $this->db->where('veh.id_utilisateur',$id);
    //$this->db->where('v.id_vehicule=veh.matricule');

    $query = $this->db->get();
    if($query->num_rows()>1)
    {
        $results =$query->result();
    }
   return $results;
}

Upvotes: 0

Views: 984

Answers (1)

Joe
Joe

Reputation: 502

You probably need to include the join in there somewhere. I'm not cool enough to remember your foreign keys and write out an example that you can paste directly into your code, but you have to:

$this->db->from('table1');
$this->db->join('table2', 'table2.index = table1.index');

You can specify the type of join if needed. Refer to the manual for a more thorough explanation, in case you're trying to do something fancy.

https://www.codeigniter.com/userguide3/database/query_builder.html

Upvotes: 2

Related Questions