• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
working with two tables under one function

#1
Exclamation 
hi guys,am new to code igniter though am learning quickly.my issue is am trying to get data from from two tables lets say after a user registers they are taken to login which is fine my issue is.when a new user ligs in they are redirected to a page to fill in their profile but when they come back to the site and they filled their profile i want them redirected to their respective profiles.

when trying to get data from the two tables its working but when the profile table doesnt contain the foreign key it displays a page yes but with the data array error.since the array is empty.
my code below
                     
Sense & Simplicity
Reply

#2
No, the array isn't empty, but the $data variable is not being defined if there is no query result.

Solution (in your model):
PHP Code:
if ($query->num_rows() > ) {
 
 return $query->result_array();  //this is way shorter than your current foreach loop
}
else {
 
 return FALSE;


Next, check if the result is FALSE or not (in your controller) before you load the view to display the result.
Reply

#3
(03-27-2017, 10:52 AM)Wouter60 Wrote: No, the array isn't empty, but the $data variable is not being defined if there is no query result.

Solution (in your model):
PHP Code:
if ($query->num_rows() > ) {
 
 return $query->result_array();  //this is way shorter than your current foreach loop
}
else {
 
 return FALSE;


Next, check if the result is FALSE or not (in your controller) before you load the view to display the result.
i did this but now cant access my values  from the array here's my line of code.am still new so kindly bear with my ignorance

    function index()
    {        
        
        $details = $this->user_model->get_user_by_id($this->session->userdata('uid'));    
        
        //kama row haina kitu kufanyike nini?------some code
        if ($details ){
            $data = array('details' => $details);
            
        
        //Hii code inaload row moja yenye session iko active  in both tables ie facility na users
        
        $data['uname'] = $details[0]->name;
        $data['uemail'] = $details[0]->email;
        $data['ucat'] = $details[0]->category;
        $data['fmail'] = $details[0]->emaile;
        $data['age'] = $details[0]->age;
        
            $this->load->view('profile_view', $data);
            
        }
        else{
            $this->load->view('welcome_message.php');
        }
Sense & Simplicity
Reply

#4
Thanks for your response it solved one issue but now am stuck on my second step.
how do i get values form the passed array,still new to codeigniter kindly bear with me.my code below
(controller)
function index()
{

$details = $this->user_model->get_user_by_id($this->session->userdata('uid'));

//kama row haina kitu kufanyike nini?------some code
if ($details ){
$data = array('details' => $details);


//Hii code inaload row moja yenye session iko active in both tables ie facility na users

$data['uname'] = $details[0]->name;
$data['uemail'] = $details[0]->email;
$data['ucat'] = $details[0]->category;
$data['fmail'] = $details[0]->emaile;
$data['age'] = $details[0]->age;

$this->load->view('profile_view', $data);

}
else{
$this->load->view('welcome_message.php');
}
Sense & Simplicity
Reply

#5
thanks for the response have got rud of my first error when data is null but now my issue in controller how to retrieve the returned values to work with them
mycode:


        function index()
    {        
        
        $details = $this->user_model->get_user_by_id($this->session->userdata('uid'));    
        
        //kama row haina kitu kufanyike nini?------some code
        if ($details ){
            
        
        //Hii code inaload row moja yenye session iko active  in both tables ie facility na users
        
        $data['uname'] = $details[0]->name;
        $data['uemail'] = $details[0]->email;
        $data['ucat'] = $details[0]->category;
        $data['fmail'] = $details[0]->emaile;
        $data['age'] = $details[0]->age;
            
            $this->load->view('profile_view', $data);
            
            
        }
        else if ($data['ucat'] =='service' && $data['fmail']!=$data['uemail'] )
        {
            
            
            $this->load->view('facility_view', $data);
            
            
        }
        
        else
        {
            $this->load->view('welcome_message.php');
        }
        }

thanks
Sense & Simplicity
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.