CodeIgniter: Populating a table (HTML/ form) from database (table) - php

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
}

Related

codeigniter Undefined variable: finmasuk and if it disapear, datatable wont show

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

CodeIgniter 3.1.4 - PHP foreach not working the way I intend for $row elements

What I want to do is append a list of all users and data relating to them to table rows using a PHP foreach, where this info is obtained from a database. The problem I am getting with this is the following error:
[Severity: Error Message: Call to a member function result_array() on array]
I read a similar question about this but it does not seem to fix my problem, as I then get a multitude of errors to do with trying to get a property from a non-object when removing the result_array() part from either the model or controller.
Here is the User.php Model:
Class User extends CI_Model
{
function populateUsers()
{
$this->db->select('username, accessLevel, fullName');
$this->db->from('users');
$userquery = $this->db->get();
if($userquery -> num_rows()>=1)
{
return $userquery->result_array();
}
else
{
return false;
}
}
}
And here is a snippet from the AdminCP.php Controller:
public function index()
{
$this->load->helper(array('form')); // for user creation
$session_data = $this->session->userdata('logged_in');
$aLevel = $session_data['accessLevel'];
if($this->session->userdata('logged_in') && $aLevel == 'Admin')
{
$userResult = $this->user->populateUsers();
if($userResult)
{
$user_array = array();
foreach($userResult as $row)
{
$user_array = array(
'username' => $row->username,
'accessLevel' => $row->accessLevel,
'fullName' => $row->fullName
);
$data['accessLevel'] = $session_data['accessLevel'];
$data['username'] = $session_data['username'];
$data['fullName'] = $session_data['fullName'];
$data['userList'] = $user_array;
$this->load->view('admin_cp_view', $data);
}
return TRUE;
}
else
{
return FALSE;
}
}
else
{
redirect('home', 'refresh');
session_write_close();
}
}
And finally this is the section of the Admin_CP_View.php page that I want to display this information on:
<div class="well">
<h4>Manage users</h4>
<p>Select a user from the list to manage</p>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Access Level</th>
<th>Full Name</th>
</tr>
</thead>
<tbody>
<?php foreach ($userList->result_array() as $row)
{ ?>
<tr>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['accessLevel'];?></td>
<td><?php echo $row['fullName'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Please let me know where I am going wrong here, thanks!
Edit in your AdminCP.php
public function index()
{
$this->load->helper(array('form')); // for user creation
$session_data = $this->session->userdata('logged_in');
$aLevel = $session_data['accessLevel'];
if($this->session->userdata('logged_in') && $aLevel == 'Admin')
{
$userResult = $this->user->populateUsers();
if($userResult)
{
$this->load->view('admin_cp_view', $userResult);
}
return TRUE;
}
else
{
return FALSE;
}
}
else
{
redirect('home', 'refresh');
session_write_close();
}
}
Edit in your Admin_CP_View.php
<div class="well">
<h4>Manage users</h4>
<p>Select a user from the list to manage</p>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Access Level</th>
<th>Full Name</th>
</tr>
</thead>
<tbody>
<?php foreach ($userResult as $row)
{ ?>
<tr>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['accessLevel'];?></td>
<td><?php echo $row['fullName'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
This may helps you.. thanks!
You have several problems. First, is not so much a problem as it is worthless code. A controller need not return a value because the Codeigniter does not expect one and won't pay any attention to it anyway.
Second, I have removed these lines from index because they appear to be unused in the view.
$data['accessLevel'] = $session_data['accessLevel'];
$data['username'] = $session_data['username'];
$data['fullName'] = $session_data['fullName'];
Here is a revised index function for the controller with the logic to return TRUE or FALSE removed.
public function index()
{
$this->load->helper(array('form')); // for user creation
$session_data = $this->session->userdata('logged_in');
$aLevel = $session_data['accessLevel'];
if($this->session->userdata('logged_in') && $aLevel == 'Admin')
{
$data['userList'] = $this->user->populateUsers();
$this->load->view('admin_cp_view', $data);
}
else
{
session_write_close();
redirect('home', 'refresh');
}
}
I would code the model's populateUsers method differently. First of all don't blindly use Query Builder. It's a great tool in certain circumstances. But your sql is so simple there is no point in running a whole lot of Query Build code for such a super easy db request.
See the code comments for the "why?" on the other changes.
Class User extends CI_Model
{
function populateUsers()
{
$userquery = $this->db->query("Select usernam, accessLevel, fullName from users");
if($userquery->num_rows() > 0)
{
return $userquery->result_array();
}
//{
//else
//you do not need the else because if there were rows the method has returned
//and the next line will never be executed.
//On the other hand, if there are no rows then this line WILL execute
return array(); //Return empty array so that the view won't try to run foreach on a boolean
// }
}
Then in the view the code for your <table> is this
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>Access Level</th>
<th>Full Name</th>
</tr>
</thead>
<tbody>
<?php
foreach($userList as $row) { ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['accessLevel']; ?></td>
<td><?php echo $row['fullName']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
This will write one row to the table for each item in $userList.
I think you may not have correctly made the changes I suggested on my first attempt to answer. You probably got multiple page loads because you had a call to load->view inside of a foreach loop. Now only the table rows should repeat.
Change your user model to this:
Class User extends CI_Model
{
function populateUsers()
{
$this->db->select('username, accessLevel, fullName');
$this->db->from('users');
$userquery = $this->db->get();
if($userquery->num_rows() >= 1)
{
return $userquery->result();
}
else
{
return false;
}
}
}

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.

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

Categories