last day (14 days later) » 

12:08 PM
0
A: Invalid argument supplied for foreach() error after filter data according to login user

NiranjanIn model change $this->session->$this->session->userdata['logged_in']['id']; to $this->session->userdata['logged_in']['id']; and try once

 
this error is solved.but it not filter data.plz help me
 
you mean the problem is with query??
 
my problem is filter data according to login user id.but its not working
 
Is the value stored in session correct?? i mean are you storing correct logged in user's id in session?? Coz, rest everythig is correct
 
yes.will i send you the my entire code
 
12:08 PM
Ya sure... I will check..
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to start session in order to access it through CI

//The controller class is extends CI_Contoller
Class User_Authentication extends CI_Controller {

public function __construct() {
parent::__construct();

// Load form helper library
$this->load->helper('form');

// Load form validation library
$this->load->library('form_validation');

// Load session library
$this->load->library('session');

// Load database
this is my controller for login and registration
this is the header


<html lang="en">
<?php
if (isset($this->session->userdata['logged_in'])) {
$firstname = ($this->session->userdata['logged_in']['firstname']);
$email = ($this->session->userdata['logged_in']['email']);
$id = ($this->session->userdata['logged_in']['id']);
} else {
header("location: login");
}
 
Corrections
1) you dont have to start session using session_start in codeigniter. Just set encryption key in config.php
Where are u setting the session
 
12:27 PM
in contreoller which i have sent to you
 
print_r($this->session->userdata); And check what values it prints
 
ok
Array ( [session_id] => 2db29e73304bcb48e84c9483ee4d9ff3 [ip_address] => 127.0.0.1 [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36 [last_activity] => 1440205494 [user_data] => [logged_in] => Array ( [firstname] => lak [email] => lak@gmail.com [id] => 1 ) [user_name] => [email] => )
 
if you can see the id in session, write query like this

$this->db->select("select required fields here");
$this->db->where("your column name",$this->session->userdata['"logged_in"]['id']);
$this->db->get("your table");
 
do u mean in another controller which i need to filter data?
 
12:45 PM
io... in model write this query to get filtered data
 
no bro it shows an error called Invalid argument supplied for foreach()
 
Where?? in model??
 
no in view
<?php foreach($post as $post){?>
<tr>
<td><?php echo $post->id ; echo '&nbsp; &nbsp;';?></td>
<td><?php echo $post->title ; echo '&nbsp; &nbsp;';?></td>
</tr>
<?php } ?>
 
write condition
if($post){
// table here
} else {
}
in else no records to display
Is the issue still coming??
 
i will try again
where do i write this code.in model?
 
1:00 PM
no. in view before foreach, check if $post has any value
 
what does mean"// table here"
 
<?php if($post) { ?>
<?php foreach($post as $post){?>
<tr>
<td><?php echo $post->id ; echo '&nbsp; &nbsp;';?></td>
<td><?php echo $post->title ; echo '&nbsp; &nbsp;';?></td>
</tr>
<?php }
} else {?>
<tr><td clospan="4" align="center">No records found to display</td></tr>
<?php } ?>
 
thanks bro. it works.but there is a issue.it display the message No records found to display.but in databse there is a value for this login user
 
1:18 PM
a u busy?
 
check ur query bro
print the query
run in phpmyadmin
u ll eget to no the issue
echo $this->db->last_query(); => to print last query
do it in model
after u ve written query
 
1:36 PM
thank u very much bro.it works. thank u again
 

last day (14 days later) »