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

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.

Related

PHP-CODEIGNITER how to get data in session

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

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);
}

MVC query join command denied

I should be simple enough, I have my query i return it as a variable I then set that variable as a array, the pass it into the view. how ever I get this error..
Error Number: 1142
SELECT command denied to user '******** ip.secureserver.net' for table 'Comments'
SELECT * FROM `Report_Comments`.`Comments`, `Report_Comments`.`Comment_Date`, `Login`.`Username` WHERE `ReportID` = '53'
Filename: models/report/Report_model.php
Line Number: 92
anyone see where i have gone wrong ?
Model
function get_comment()
{
$query = $this->db->get('Report_Comments.Comments, Report_Comments.Comment_Date, Login.Username')
->from('Report_Comments')
->join('Login', 'Report_Comments.UserID = Login.LoginID');
return $query->result();
}
View
<?php if (isset($reports)) :
foreach ($reports as $row) : ?>
<tr>
<td><?php echo $row->Comments; ?></td>
</tr>
<tr>
<td><?php echo $row->Comment_Date; ?></td>
</tr>
<tr>
<td><?php echo $row->Username; ?></td>
</tr>
</table>
<hr>
<?php endforeach; ?>
<?php else : ?>
<p>No Comments</p>
<?php endif; ?>
Controller
function comments()
{
$data = array();
$this->db->where('ReportID', $this->uri->segment(3));
if ($query = $this->report_model->get_comment()) {
$data['reports'] = $query;
}
$this->template['middle'] = $this->load->view($this->middle = 'comments/comment_view', $data, true);
}
New answer:
Sql is telling you the specified user does not have permission to run a select command on table Comments. You'll want to check that your user has the appropriate permissions for the db and/or table to resolve that mysql issue. Your PHP shouldn't have anything to do with that.
Original answer to original question/issue:
(The edit is throwing me off.)
In order to get a result object that includes rows you need to invoke the function that returns that object.
So in your controller
$data['reports'] = $result->result();
Variable assignment usage
Also in your model, it is useless to set $result = $this->db->get(); The return is going to just pass back $this->db->get(); - remove $result =.
And in your controller, you are testing the value of $this->report_model->get_comment() in if($result = $this->report_model->get_comment()) so, if the value is a get object then how php interprets that as true or false is somewhat loose ended - it'll return its "truthiness" which is not always straight forward. Alternatively, you could do something definite like:
$query = $this->report_model->get_comment();
if ($query->num_rows() > 0) {
$data['reports'] = $result->result();
}
You could also just pass that $query right to the view and do that test in place of your if (isset($reports)) => if ($query->num_rows() > 0): foreach ($query->result() as $row):. That would reduce one if check.
Database query builder across functions
So, you'll end up with more bugs or weird situations when you build a query through various levels of functions. Also, it's harder to maintain your codebase as it grows since you use your model function in unpredictable ways. Instead of setting the where just before calling the model function, pass the id as a parameter with a predefined value if you want it to be optional:
function get_comment($report_id = null)
{
if (isset($report_id)) {
$this->db->where('ReportID', $report_id);
}
$this->db->select('Report_Comments.Comments, Report_Comments.Comment_Date, Login.Username')
->from('Report_Comments')
->join('Login', 'Report_Comments.UserID = Login.LoginID');
return $this->db->get();
}
And your controller:
function comments()
{
$data = array();
$query = $this->report_model->get_comment($this->uri->segment(3));
if($query->num_rows() > 0)
{
$data['reports'] = $query->result();
}
$this->template['middle'] = $this->load->view ($this->middle = 'comments/comment_view',$data, true);
$this->layout();
}

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');

Categories