CODEIGNITER undefined index - php

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

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.

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 !! :(';
}
?>

error read data in codeigniter

i have an applicataion that must to read data from database. i think it simple but why i still got error like this :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/detail_usulan.php
i have tried to repair this code but its still cant work. i'll show u my code. i need your help:(
My controller :
function detail_usulan(){
$data = array('model_usulan' => $this->model_usulan->get_all());
//$this->load->view('usulan/detail_usulan', $data);
$this->render('usulan/detail_usulan', $data);
}
My models :
class model_usulan extends MY_Model{
function get_all(){
return $this->db->get('usulan_rkau');
}
}
My view :
<?php foreach ($data as $view_data) { ?>
<tr>
<th> Tahun : </th>
<td><?php echo $view_data->tahun; ?></td>
</tr>
You could just try it like below on controller instead of it begin in an array()
$data['model_usulan'] = $this->model_usulan->get_all();
$this->load->view('usulan/detail_usulan', $data);
On your view change this $data
<?php foreach ($data as $view_data) { ?>
<tr>
<th> Tahun : </th>
<td><?php echo $view_data->tahun; ?></td>
</tr>
<?php }?>
To $model_usulan in the array
<?php foreach ($model_usulan as $view_data) { ?>
<tr>
<th> Tahun : </th>
<td><?php echo $view_data->tahun; ?></td>
</tr>
<?php }?>
Note: Your class name and file name should have the FIRST letter upper case ONLY same applies for controllers and libraries etc
As explained here Classnames And Filenames
Filename: Model_usulan.php
<?php
class Model_usulan extends CI_Model {
}
Also On your model function try
return $this->db->get('usulan_rkau')->result();
Or
$query = $this->db->get('usulan_rkau');
return $query->result();
And may be change this
MY_Model {
To
CI_Model {
you need to do this change in your controller
function detail_usulan(){
$data['data'] = array('model_usulan' => $this->model_usulan->get_all());
$this->load->view('usulan/detail_usulan', $data);
//$this->render('usulan/detail_usulan', $data);
}
or
change in your view
foreach ($model_usulan as $view_data) {
Hope this will work.

Error Undefined variable: result + Error Invalid argument supplied for foreach() in CodeIgniter 2

Hi I was trying to place the foreach block in my view but i encountered 2 errors here :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
and
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Here is my view :
<?php foreach($result as $data_barang):?>
<tr>
<td><?php echo $data_barang->barang.kode_item;?></td>
<td><?php echo $data_barang->nama_item;?></td>
<td><?php echo $data_barang->nama_ruang;?></td>
<td><?php echo $data_barang->jml_item_kondisi;?></td>
<td><?php echo $data_barang->kondisi;?></td>
</tr>
<?php endforeach;?>
For addition i'll place the model and the controller here if it's needed :
controller :
public function index(){
// load data barang yg akan ditampilkan
$data['result']=$this->admin_model->get_data_table()->result_array();
// load view
$this->load->view('dashboard_admin');
}
*note : i also have tried with ->result()
model :
function get_data_table(){
$query_result = $this->db->query('SELECT barang.kode_item, nama_item, nama_ruang, jml_item_kondisi, kondisi
FROM barang
INNER JOIN info_barang ON barang.kode_item = info_barang.kode_item
INNER JOIN (
SELECT ruang.nama_ruang, campur_table.kode_item
FROM ruang
INNER JOIN rekap_isi_ruang AS campur_table ON campur_table.nomor_ruang = ruang.nomor_ruang) AS barang_campur
ON barang.kode_item = barang_campur.kode_item');
return $query_result;
}
I've tried this and this but still doesn't resolve my problems.
By the way, i'm sorry if there's any words that doesn't look familiar to you. Thanks
Try this
public function index(){
// load data barang yg akan ditampilkan
$data['result']=$this->admin_model->get_data_table()->result_array();
// load view
$this->load->view('dashboard_admin',$data);
}
Problem In Controller
You Store Data in $data['result'] variable but you not pass to View $data variable
so you pass $data variable in controller like this in controller
public function index(){
// load data barang yg akan ditampilkan
$data['result']=$this->admin_model->get_data_table()->result_array();
// load view
$this->load->view('dashboard_admin',$data);
}
this work fine
Change your model into this
function get_data_table(){
$query_result = $this->db->query('SELECT barang.kode_item, nama_item, nama_ruang, jml_item_kondisi, kondisi
FROM barang
INNER JOIN info_barang ON barang.kode_item = info_barang.kode_item
INNER JOIN (
SELECT ruang.nama_ruang, campur_table.kode_item
FROM ruang
INNER JOIN rekap_isi_ruang AS campur_table ON campur_table.nomor_ruang = ruang.nomor_ruang) AS barang_campur
ON barang.kode_item = barang_campur.kode_item')->result();
return $query_result;
}
If you want array use result_array() instead of query()->result();
try this::
// Controller
public function index(){
$data['result']=$this->admin_model->get_data_table()->result_array();
$this->load->view('dashboard_admin',$data);
}
// View
<?php
if(!empty(result)) {
foreach($result as $data_barang):?>
<tr>
<td><?php echo $data_barang->barang.kode_item;?></td>
<td><?php echo $data_barang->nama_item;?></td>
<td><?php echo $data_barang->nama_ruang;?></td>
<td><?php echo $data_barang->jml_item_kondisi;?></td>
<td><?php echo $data_barang->kondisi;?></td>
</tr>
<?php
endforeach;
} else { ?>
<tr>
<td colspan="5">Some message here</td>
</tr>

Categories