Moorcam Posted January 1, 2023 Share Posted January 1, 2023 So, when a user logs in, I would like to grab data from the db and display it based on their id, which is in a session. Here is my Controller: public function jobs() { if($this->session->userdata('e_id')){ $id = $this->session->userdata('e_id'); $data['tour'] = $this->usermodel->job($id); $this->load->view('tour',$data); }else{ echo "No results"; } } Here is my Model: public function job($id) { $this->db->select('*') ->from('tbl_tour') ->where('e_id', $id); return $this->db->get()->result(); } And finally, View: <table class="table table-striped"> <tr> <th>Date</th> <th>Tour</th> </tr> <?php foreach($tour as $job):?> <tr> <td><?php echo $job->start_date;?></td> <td><?php echo $job->name;?></td> </tr> <?php endforeach;?> </table> When executing I get Undefined variable: tour And also: Message: Invalid argument supplied for foreach() If anyone can help with this it would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/315746-showing-data-assigned-to-user/ Share on other sites More sharing options...
requinix Posted January 1, 2023 Share Posted January 1, 2023 Are you sure you're passing data to the view correctly? That array keys in your $data will turn into variables inside the view file? Quote Link to comment https://forums.phpfreaks.com/topic/315746-showing-data-assigned-to-user/#findComment-1604077 Share on other sites More sharing options...
Moorcam Posted January 1, 2023 Author Share Posted January 1, 2023 Ok, I have added the following to the Controller index and now getting no error. But, not getting anything at all lol. $data['tour'] = $this->Model_dashboard->tour('p_id'); Quote Link to comment https://forums.phpfreaks.com/topic/315746-showing-data-assigned-to-user/#findComment-1604079 Share on other sites More sharing options...
Solution Moorcam Posted January 1, 2023 Author Solution Share Posted January 1, 2023 Fixed Change the following: <td><?php echo $job->start_date;?></td> <td><?php echo $job->name;?></td> To... <td><?php echo $row['p_start_date']; ?></td> <td><?php echo $row['p_name']; ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/315746-showing-data-assigned-to-user/#findComment-1604107 Share on other sites More sharing options...
maxxd Posted January 1, 2023 Share Posted January 1, 2023 That makes no sense whatsoever given the code you posted. Where is $row defined? Quote Link to comment https://forums.phpfreaks.com/topic/315746-showing-data-assigned-to-user/#findComment-1604109 Share on other sites More sharing options...
Moorcam Posted January 2, 2023 Author Share Posted January 2, 2023 (edited) Hi mate, Sorry. Made a few changes but the above was the culprit really. Here is the full update. Controller: public function index() { $data['setting'] = $this->Model_common->get_setting_data(); $data['tour'] = $this->Model_dashboard->all_tour('p_id'); $data['total_category'] = $this->Model_dashboard->show_total_category(); $data['total_news'] = $this->Model_dashboard->show_total_news(); $data['total_motel'] = $this->Model_dashboard->show_total_motel(); $data['total_tour'] = $this->Model_dashboard->show_total_tour(); $data['total_team_member'] = $this->Model_dashboard->show_total_team_member(); $data['total_agent'] = $this->Model_dashboard->show_total_agent(); $data['total_service'] = $this->Model_dashboard->show_total_service(); $data['total_testimonial'] = $this->Model_dashboard->show_total_testimonial(); $data['total_traveller'] = $this->Model_dashboard->show_total_traveller(); $data['amount_paid'] = $this->Model_dashboard->show_amount_paid(); $data['total_fleet'] = $this->Model_dashboard->show_total_fleet(); $data['total_driver'] = $this->Model_dashboard->show_total_driver(); $this->load->view('admin/view_header',$data); $this->load->view('admin/view_dashboard',$data); $this->load->view('admin/view_footer'); } Model: public function all_tour() { $e_id = $this->session->userdata('e_id'); $query = $this->db->query("SELECT * FROM tbl_tour WHERE e_id = $e_id ORDER BY p_id ASC"); return $query->result_array(); } View: <table class="table table-striped"> <tr> <th>Tour</th> <th>Departure Date</th> <th>Return Date</th> <th>Fleet Number</th> </tr> <?php foreach($tour as $row):?> <tr> <td><?php echo $row['p_name']; ?></td> <td><?php echo $row['p_start_date']; ?></td> <td><?php echo $row['p_end_date']; ?></td> <td><?php echo $row['v_number']; ?></td> </tr> <?php endforeach;?> </table> Edited January 2, 2023 by Moorcam Quote Link to comment https://forums.phpfreaks.com/topic/315746-showing-data-assigned-to-user/#findComment-1604113 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.