PHP-CODEIGNITER how to get data in session - php

I want echo my list of trip, when I try print_r the value can show but when I echo the result always
Message: Undefined variable: adventure
Filename: views/profile.php
Line Number: 121
Backtrace:
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/profile.php
Line Number: 121
this is my controller :
public function getListTrip(){
$this->load->model('userModel');
$data['adventure'] = $this->userModel->getTrip()->result_array();
//echo ($data);
$this->load->view('profile', $data);
}
and this is my model :
function getTrip(){
$userId = $this->session->userdata('user_id');
return $this->db->get_where('adventure',['user_id' => $userId]);
}
this is my view
<table>
<?php
foreach ($adventure as $b){
echo "<tr>
<td>$b->name</td>
<td>$b->place</td>
<td>$b->category</td>
</tr>";
}
?>
</table>
so how should I edit my code to make the value show in my page whit echo or foreach not in print_r... thanks a lot

Change your model
function getTrip(){
$userId = $this->session->userdata('user_id');
$query= $this->db->get_where('adventure',['user_id' => $userId]);
return $query->result();
}
Also chnage in your controller
$data['adventure'] = $this->userModel->getTrip()->result_array();
To
$data['adventure'] = $this->userModel->getTrip();

echo is used to output one or more strings, since your $data is an array you see the error, you need to pass data to view and iterate it in the view itself, like:
public function getListTrip(){
$this->load->model('userModel');
$data['adventure'] = $this->userModel->getTrip()->result_array();
//pass data to your view
$this->load->view('yourviewname', $data);
}
For more information check Codeigniter Views
Update
Check if your array is not empty before trying to iterate thru it, like in your view::
<?php
if( !empty($adventure) ) {
foreach($adventure as $b):
?>
<tr>
<td><?php echo $b['name']; ?></td>
<td><?php echo $b['place']; ?></td>
<td><?php echo $b['category']; ?></td>
</tr>
<?php
endforeach;
}
?>

You cannot echo the content of an Array.
So whenever you want to view the Content of an array use print_r($arrayName);
And When you just want to print any variable just use echo $variableName;

I don't see any issue with your code. If you're not using autoloader to load the session library that might be your issue:
$this->load->library('session');
You can check the documentation Codeigniter sessions

Related

Message: Illegal String Offset Upgrade from Codeigniter2x Version to Codeigniter 3x version

I recently switched my project package from Codeigniter 2X version to Codeigniter 3X version.
Earlier all my code worked fine but now with new package its showing some minute errors where I am not able to run my code.
The following is the error:
Message: Illegal String Offset:
My Controller:
public function proflist(){
$data = "";
$this->load->model('feedbackmodel');
$data['teachers'] = $this->feedbackmodel->getFaculty();
$this->load->view('feedback/proflist',$data);
}
My Model:
public function getFaculty(){
$query = $this->db->query('SELECT * FROM teacher');
return $query->result_array();
}
My View:
<?php
if(!empty($teachers)) {
foreach($teachers as $y){
?>
<tr>
<td><?php echo $y->fid; ?></td>
<td><?php echo $y->fname." ". $y->lname; ?></td>
<td><?php echo $y->email; ?> </td>
<td>
...
Is there any mistake ? How shall i change it. Please let me know:) Thank You.
The reason for this is most likely the version of PHP you're using. Before PHP 7.1 it was only a Notice thrown for the use of an illegal offset. 7.1+ will now throw a Warning. You can avoid it by wrapping an 'if not, isset' in your controller around the offending array. Be sure to cast the array also.
You'll find a good explanation for it here.
public function proflist() {
$data = "";
$this->load->model('feedbackmodel');
$data['teachers'] = (array) $this->feedbackmodel->getFaculty();
if(!isset($data['teachers'])) {
$data['teachers'] = '';
}
$this->load->view('feedback/proflist',$data);
}
To be certain of the version of PHP you're running, you can use phpinfo();.
You need to be more aware of what your variable types are.
In your controller
public function proflist(){
$data = "";
$this->load->model('feedbackmodel');
$data['teachers'] = $this->feedbackmodel->getFaculty();
$this->load->view('feedback/proflist',$data);
}
You declare $data as a STRING by way of $data = "";,
Then you decide it's going to be an array by way of
$data['teachers'] = $this->feedbackmodel->getFaculty();
To be correct $data should be declared as an empty array, not an emtpy string.
public function proflist(){
$data = array(); // Using the older style of declaring an array.
$this->load->model('feedbackmodel');
$data['teachers'] = $this->feedbackmodel->getFaculty();
$this->load->view('feedback/proflist',$data);
}
Now in your Model your returned data is created via
return $query->result_array();
If you look up the codeigniter documentation on this, it will tell you that the resulting data is an Associative Array i.e $data['teachers'] will be of the form
$data['teachers']['fid'] etc.
Your View requires it to be an object.
So you can change your view which requires $teachers to be an object,from...
<?php
// $teachers should be returned from result() which is an object.
if(!empty($teachers)) {
foreach($teachers as $y){
?>
<tr>
<td><?php echo $y->fid; ?></td>
<td><?php echo $y->fname." ". $y->lname; ?></td>
<td><?php echo $y->email; ?> </td>
<td>
...
To
<?php
// $teachers is returned from result_array() which is an associative array.
if(!empty($teachers)) {
foreach($teachers as $y){
?>
<tr>
<td><?php echo $y['fid']; ?></td>
<td><?php echo $y['fname']." ". $y['name']; ?></td>
<td><?php echo $y['email']; ?> </td>
<td>
...
OR simply change the return on your model which is a one liner. So change your Model from
public function getFaculty(){
$query = $this->db->query('SELECT * FROM teacher');
return $query->result_array(); // Return an associative array
}
To
public function getFaculty(){
$query = $this->db->query('SELECT * FROM teacher');
return $query->result; // Return an object
}
So the short version is:
Check your returned array structure. You should use var_dump() for this.
Change your code to suit.
Either change the return on your model from result_array() to result() or
Change your views from addressing objects to associative arrays.
I'm pushed for time to go into any more detail at this stage, but this should help you see what is going on.

I cant pass variable from controller to view in Codeigniter

I get error saying:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/businessinfo_view.php
My model:
function getPosts(){
$query = $this->db->query("SELECT * FROM customer WHERE
customer_id=12;");
return $query->result_array();
}
My controller:
function ind() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
MY view:
<?php foreach($data as $d){?>
<tr>
<td><?php echo $d->customer_id;?></td>
<td><?php echo $d->first_name);?></td>
</tr>
<?php }?>
I tried 100 different ways, still can pass the variables, I know my query is correct, I can fetch and echo data in controller, I can't just pass it into view. someone please help me!
function controller() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
Whenever you pass variable in view, try to access it with key inside view.
This $customerinfo variable will have all your data.
For EX. $customerinfo because your actual variable is $data['customerinfo'].
if var name is $data['extraVal'], in view access through $extraVal.
Try if you have the $data['customerinfo'] then on the view you would use like $customerinfo
http://www.codeigniter.com/user_guide/general/views.html#creating-loops
Controller
function ind() {
$data['customerinfo'] = array();
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
Model
function getPosts(){
$this->db->where('customer_id', '12');
$query = $this->db->get('customer');
return $query->result_array();
}
View
<?php foreach($customerinfo as $d){?>
<tr>
<td><?php echo $d['customer_id'];?></td>
<td><?php echo $d['first_name'];?></td>
</tr>
<?php }?>
When model function returns result_array() <?php echo $d['first_name'];?>
When model function returns result() <?php echo $d->first_name;?>
Example found here
Try exactly this :
My model:
function getPosts(){
$query = $this->db->query("SELECT * FROM customer WHERE
customer_id = '12' ");
return $query->result();
}
My controller:
function ind() {
$data['customerinfo'] = $this->user_model->getPosts();
$this->load->view('businesssinfo_view', $data);
}
MY view:
<?php
if(!empty($customerinfo))
{
foreach($customerinfo as $d){?>
<tr>
<td><?php echo $d->customer_id;?></td>
<td><?php echo $d->first_name;?></td>
</tr>
<?php }
}
else{
echo 'Some problem with the variable !! :(';
}
?>

Display 2 tables on codeigniter

im new to codeigniter and php, need some enlightment to display 2 tables with mvc method. just displaying two tables in one page (camera and visitor table). here is my code
Model :
function m_report() {
$camera = $this->db->get('camera');
return $camera->result();
$report = $this->db->get('visitor');
return $report->result();
}
View:
<?php foreach($data1 as $report){ ?>
<tr>
<td><?php echo $report->Country; ?></td>
<td><?php echo $report->Days; ?></td>
</tr>
<?php } ?>
<?php foreach($data_camera as $camera){ ?>
<tr>
<td><?php echo $camera->cameratype; ?></td>
</tr>
<?php } ?>
Controller :
function report(){
$data['data_camera']=$this->m_data->m_report();
$data1['data1']=$this->m_data->m_report();
$this->load->view('v_report',$data,$data1);
}
the problem is, i can display camera table but visitor got error Message: Undefined variable: data1
Can anyone help me to figure it out? Much appreciate
You can only return ONE thing from a method - once you return something, execution of code stops.
function m_report() {
$camera = $this->db->get('camera')->result();
$report = $this->db->get('visitor')->result();
return array_merge($camera, $report);
}
Now you get an array with all the results from both "camera" and "visitor". You can specify it out if you'd like with an associative array.
function m_report() {
$data['camera'] = $this->db->get('camera')->result();
$data['visitor'] = $this->db->get('visitor')->result();
return $data;
}
you can not make 2 returns in one method
function m_report() {
$camera = $this->db->get('camera');
return $camera->result();
$report = $this->db->get('visitor');
return $report->result();
}
i think it is better to make each query in single function
function m_camera_report() {
$camera = $this->db->get('camera');
return $camera->result();
}
function m_visitor_report() {
$report = $this->db->get('visitor');
return $report->result();
}
then call them separately in controller
function report(){
$data['data_camera']=$this->m_data->m_camera_report();
$data['data_visitor']=$this->m_data->m_visitor_report();
$data1['data1']=$this->m_data->m_report();
$this->load->view('v_report',$data,$data1);
}

CODEIGNITER undefined index

:D
got some problems here
error code
A PHP Error was encountered
Severity: Notice
Message: Undefined index: DESCRIPTION
Filename: views/dashboard_view.php
Line Number: 13
the controller:
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['USERNAME'];
$data['companyid'] = $session_data['COMPANYID'];
$data['category']=$this->main_model->get_category();
$this->load->view('dashboard_view', $data);
}
else
{
//If no session, redirect to login page
redirect('main', 'refresh');
}
}
model
function get_category(){
$this->db->select('*');
$this->db->from('view_category');
$category=$this->db->get();
return $category->result();
}
view
<h2>Welcome <?php echo $username; ?>!</h2>
<?= form_hidden ($companyid); ?>
<br>
Logout
<? foreach($category ):?>
<tr>
<td><?= $category['DESCRIPTION']; ?></td><-- this is line 13
</tr>
<? endforeach;?>
Follow these steps :-
Check if your model is loaded correctly or not . Either load it manually before calling it or you can autoload your model in config/autoload file.
Before looping through your catogory in view file , try printing the array . I think there is no index named description in your category array .
Change :-
$category->result();
To :-
$category->result_array();
Also :-
<td><?= $category['DESCRIPTION']; ?></td>
To :-
<td><?php echo $category['DESCRIPTION']; ?></td>
Hope it helps you :)
You foreach should be this:
<?php foreach($category as $rows): ?>
^^^ add this
And then get the data by this:
<td><?php echo $rows['DESCRIPTION']; ?></td>
You can also check that what it is returning to get that just do print_r($rows); in the foreach loop.
Note: avoid to use the sort tag of PHP as many servers are not supporting to this.
Have you tried loading the model before using it?
Example: $this->load->model('main_model');

PHP data array variable not defined in view

I am using codeIgniter and I am trying to pass an array of data. I have written like this:
$data['username']="Dumbo";
I also wrote this:
$data['shouts']=$this->Musers->getShout(); // retrieve data from table
Then I write:
$this->load->view("welcome_message", $data);
In view page, I wrote:
<?php echo $username;
foreach ($shouts as $shout)
{
echo $shout->shout;
echo '<br>';
echo $shout->timeStamp;
}
?>
Problem is that while the view did retrieve data from table and display results in view page, an error came up for $data['username'] saying:
"Undefined variable: username"
Why is that? The $data['username'] is already defined! Or what did I do wrong?
<?php echo $data['username']; ?>
If you wrote this, the error will occur.
Correct way is to write like
<?php echo $username; ?>
'username' is the index in the data array, which is passed to the view using the load method
$this->load->view("welcome_message", $data);
If you need to pass an array...
$data['usernames'] = $username_array;
$this->load->view("welcome_message", $data);
Then in the view,
<?php print_r($usernames); ?>
In your view, do this..
$data = array();
$data['username'] = "something";
$data['shouts']=$this->Musers->getShout();

Categories