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.
Related
i know this is common question, bet i've tried everything on here and my problem not solved yet. im new at codeigniter and maybe PHP.
like what i've mention above. i have a problem with my code. error show when i try to get data from my database table and show them on html table.
1st error is, when i try to use "foreach" error says "Undefined variable: finmasuk".
2nd error is, when "Undefined variable: finmasuk" disapear, there's some error on my table.
this is my code..
controller
public function finmasuk(){
$this->load->model('m_input');
$data["finmasuk"] = $this->m_input->finmasuk();
$this->load->view("input/standar", $data);
}
model
public function finmasuk(){
$this->db->select("*");
$this->db->from("pemasukan");
$query = $this->db->get();
return $query;
}
view
<?php if($finmasuk->num_rows() > 0){
foreach($finmasuk->result() as $row) { ?>
<tr>
<td><?php echo $row->nomor;?></td>
<td><?php echo $row->tanggal_input;?></td>
<td><?php echo $row->jenis_pemasukan;?></td>
<td><?php echo $row->nominal;?></td>
<td><?php echo $row->keterangan;?></td>
</tr>
<?php }}
else
{ ?>
<tr>
<td colspan="5">No Data Found</td>
</tr>
<?php }?>
error says that "Undefined variable: finmasuk" but i think i have
$data["finmasuk"]
try this:
model
public function finmasuk(){
$this->db->select("*");
$this->db->from("pemasukan");
$query = $this->db->get();
return $query->result();
}
View
<?php if(!empty($finmasuk)){
foreach($finmasuk as $row) { ?>
<tr>
<td><?php echo $row->nomor;?></td>
<td><?php echo $row->tanggal_input;?></td>
<td><?php echo $row->jenis_pemasukan;?></td>
<td><?php echo $row->nominal;?></td>
<td><?php echo $row->keterangan;?></td>
</tr>
<?php }}
else
{ ?>
<tr>
<td colspan="5">No Data Found</td>
</tr>
<?php }?>
You could try a few debugging tricks to check if your query is returning any rows at all.
Controller
public function finmasuk(){
$this->load->model('m_input');
$rowsReturned = $this->m_input->finmasuk()->num_rows();
echo $rowsReturned;
}
Model
public function finmasuk(){
return $this->db->get('pemasukan');
}
Another Option - This checks for query results as an array
Controller
public function finmasuk(){
$this->load->model('m_input');
$resultArray = $this->m_input->finmasuk()->result_array();
var_dump($resultArray);
echo "<br>";
echo "Count - ".count($resultArray); //Get number of rows in
}
Model
public function finmasuk(){
return $this->db->get('pemasukan');
}
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 !! :(';
}
?>
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>
: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');
I need to fill in a table in my view with the data extracted from a database table. However when I tried this, it returned error:
Fatal error: Call to a member function result_array() on a non-object in C:\xampp\htdocs\bit\application\views\admin\add_new_room.php on line 32
View:
<table class="table table-hover">
<?php foreach ($query->result_array() as $row): { ?> // Line 32
<tr>
<td><?php echo $row['room_number'];?></td>
<td><?php echo $row['rt_name'];?></td>
<td><?php echo $row['housekeeping_status'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
</table>
Controller:
function index() {
$this->load->model('admin/edit_other_details');
$roomType['rt_name'] = $this->edit_other_details->get_room_details();
$data['query'] = $this->edit_other_details->roomsTable();
$tmp = array_merge($roomType, $data);
$this->load->view('/admin/add_new_room', $tmp);
}
Model:
function roomsTable() {
$query = $this->db->select(array('room_number','rt_name'))->get('rooms');
}
That is because you forgot to return data in your model.
try this
Model
function roomsTable() {
$query = $this->db->select(array('room_number','rt_name'))->get('rooms');
return $query; //<---here
}