Display $query->result(); - php

I'm having some trouble displaying the results of my query using a foreach loop in codeigniter. Heres my controller:
function viewall()
{
$this->load->model('all');
$data['query'] = $this->all->viewall();
$this->load->view('all', $data);
}
Entire Model File:
<?php
class All extends CI_Model
{
function insert_into_db()
{
$data = array('Error' => $this->input->post('f1'),
'Solution' => $this->input->post('f2')
);
$this->db->insert('Errors', $data);
}
function viewall()
{
$query = $this->db->select("Error, Solution")->from("Errors")->get();
return $query->result();
}
}
My view (where I think the problem is)
<table class="table table-striped">
<thead>
<tr>
<td><h3>Error</h3></td>
<td><h3>Solution</h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($query->result_array() as $entry) ?>
<tr>
<td><?php echo $entry->Error; ?></td>
<td><?php echo $entry->Solution;?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>

Controller:
function viewall()
{
$this->load->model('all');
$data['results'] = $this->all->viewall();
$this->load->view('all', $data);
}
Model:
function viewall()
{
$query = $this->db->select("Error, Solution")->from("Errors")->get();
return $query->result();
}
View:
<table class="table table-striped">
<thead>
<tr>
<td><h3>Error</h3></td>
<td><h3>Solution</h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($results as $entry): ?>
<tr>
<td><?php echo $entry->Error; ?></td>
<td><?php echo $entry->Solution;?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Related

Cannot display data to table from database

This is the kind of error that I'm facing:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: fetch_data
Filename: pages/instructors.php
Line Number: 126"
Here's my instructor.php line of codes:
<tr>
<th>ID</th>
<th>Last Name</th>
<th>First Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if($fetch_data) {
foreach ($fetch_data->result() as $row) {
?>
<tr>
<td><?php echo $row->ins_id; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3">No data found</td>
</tr>
<?php
}
?>
</tbody>
</table>
my Controller:
class Add_instructor extends CI_Controller{
public function instructor(){
$this->load->model('instructor_model');
$data["fetch_data"] = $this->instructor_model->fetch_data();
$this->load->vars($data);
$this->load->view('templates/header');
$this->load->view('pages/add_instructor', $data);
$this->load->view('templates/footer');
}
}
And my model:
class instructor_model extends CI_Model {
function fetch_data(){
$query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
return $query;
}
}
Change your controller code as below. It will work.
Controller code
class Add_instructor extends CI_Controller{
public function instructor(){
$this->load->model('instructor_model');
$data["fetch_data"] = $this->instructor_model->fetch_data();
$this->load->view('templates/header');
$this->load->view('pages/instructor', $data);
$this->load->view('templates/footer');
}
}
Model Code:-
class instructor_model extends CI_Model {
function fetch_data(){
$query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
return $query->result();
}
}
View Code:-
<tr>
<th>ID</th>
<th>Last Name</th>
<th>First Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if($fetch_data) {
foreach($fetch_data as $row) {
?>
<tr>
<td><?php echo $row->ins_id; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3">No data found</td>
</tr>
<?php
}
?>
</tbody>
</table>

Display data in table in specific format

i want to display data in specific format that i have attached. Every object,Title and Coordinates added by instructor should be displayed in front of instructor in one row . This is what i want . this is how i am currently getting data
Code for both view and controller is here
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>Instructor</th>
<th>Title</th>
<th>Coordinates</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php $i=1; foreach ($res as $value) { ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $value['first_name'] . " " . $value['last_name']; ?></td>
<td><?php echo $value['description']; ?></td>
<td><?php echo $value['coordinates']; ?></td>
<td>
<?php if($edit_delete['edit']) {?>
<i class="fa fa-eye"></i>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
public function index()
{
$this->user_model->check_permissions('Objects/index');
$data['edit_delete']=$this->user_model->checkEditDelete('Objects/index');
$rl_id= $this->rol_id;
$str = implode('-',$rl_id);
if($str==2)
{
$data['menu']=$this->load_model->menu_inst();
}else
{
$data['menu']=$this->load_model->menu();
}
$data['base_url'] = base_url();
$data['userInfo'] = $this->userInfo;
$rl_id= $this->rol_id;
$super = implode('-',$rl_id);
if($super==1)
{
$this->db->select('object.id, object.description, object.coordinates, admin.first_name, admin.last_name');
$this->db->from('object');
$this->db->join('admin', 'admin.id = object.instructor_id');
$this->db->where('admin.is_delete',0);
$this->db->where('admin.role_id',2);
$this->db->where('object.is_delete',0);
$data['res'] = $this->db->get()->result_array();
}
else
{
$in_id= $this->insret_id;
$insrtuctur = implode('-',$in_id);
$data['data'] = $this->db->where('is_delete',0)->where('instructor_id',$insrtuctur)->get('object')->result_array();
}
$data['page'] = "objects/objects";
$this->load->view('Template/main', $data);
}

I am not able to get data from model in codeigniter

Model
//returns an array having different arrays inside it with keys id ,name and thumbnail taken from the database
function m_get_thumbnails($category){
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food');
if($query->num_rows()>0)
{
$count =0;
$arr = array();
foreach ($query->result() as $row)
{
$arr[$count++] = array(
'id'=>$row->id,
'name'=>$row->name,
'thumbnail'=>$row->thumbnail
);
}
return $arr;
}
}
function in controller
After that i place the result in an array and pass the array to my view.
function index_food()
{
$data['cafe'] = $this->mlocus->m_get_thumbnails('cafe');
$this->load->view('food.php',$data);
}
View
<script>alert(<?php $cafe[1]; ?>);</script>
//this alert is coming blank.......
Controller:
function index_food()
{
$data['cafe'] = $this->mlocus->m_get_thumbnails('cafe');
$this->load->view('food',$data);
}
In Model
function m_get_thumbnails($category)
{
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food');
$result = $query->result_array();
return $result;
}
In your View
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Thumbnail</td>
</tr>
<?php
if(!empty($cafe)) {
foreach($cafe as $c){
?>
<tr>
<td> <?php echo $c['id']?> </td>
<td> <?php echo $c['name']?> </td>
<td><img src="<?php echo $c['thumbnail']?>" /></td>
</tr>
<?php } } else { ?>
<tr><td colspan="3">No record found.</td></tr>
<?php } ?>
</table>
Simplify your function in Model
function m_get_thumbnails($category)
{
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food')->result_array();
return $query;
}
In your View
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Thumbnail</td>
</tr>
<?php for($i=0;$i<count($cafe);$i++){?>
<tr>
<td> <?php echo $cafe[$i]['id']?> </td>
<td> <?php echo $cafe[$i]['name']?> </td>
<td> <?php echo $cafe[$i]['thumbnail']?> </td> <!-- Use <img> if you have a url -->
</tr>
<?php }?>
</table>

Download file helper

I wanna download my file that i have upload to my web
Im just saving the name of the file to database
My controller
public function index()
{
$data['file'] = $this->home_model->file();
$this->load->view('file', $data);
}
public function download()
{
$id_file = $this->uri->segment(3);
$data = file_get_contents("/upload/kepsek/".$id_file);
force_download('$id_file', $data);
}
My model
function file()
{
$query=$this->db->query("SELECT * FROM silabus");
return $query->result();
}
My view
<table class="table table-bordered text-center" id="table-user">
<thead>
<tr class="info">
<th>Download</th>
<th>Pelajaran</th>
</tr>
</thead>
<tbody>
<?php
foreach($file as $data){
?>
<tr>
<td width="50%">Download</td>
<td width="50%"><?php echo $data->pelajaran;?></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
When i click download nothing happend
Its there any way to make it work?
try this
$data = file_get_contents("upload/kepsek/".$id_file);
and also check the "upload/kepsek/".$id_file file exist or not
<table class="table table-bordered text-center" id="table-user">
<thead>
<tr class="info">
<th>Download</th>
<th>Pelajaran</th>
</tr>
</thead>
<tbody>
<?php
foreach($file as $data){
?>
<tr>
<td width="50%">Download</td>
<td width="50%"><?php echo $data->pelajaran;?></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
Then in Download Controller
public function index()
{
$data['file'] = $this->home_model->get_file();
$this->load->view('file', $data);
}
public function download($id)
{
$this->load->helper('file');
$this->load->helper('download');
$data = file_get_contents("./upload/kepsek/".$id); //check file exist
$name = my_file.php//your file type
force_download($data, $name);
}
In model
function get_file()
{
$query= $this->db->query("SELECT * FROM silabus");
$result = $query->result_array();
return $result;
}

CodeIgniter loads data from the controller but not from the view

Here's the issue : CodeIgniter loads data from the controller but not from the view
When I go the the source of the page and click the form action link its load data in the source code but not in the html view.
--view
<table class="table hovered">
<thead>
<tr>
<th class="text-left">User ID</th>
<th class="text-left">Type</th>
<th class="text-left">Name</th>
<th class="text-left">Email</th>
<th class="text-left">Phone</th>
</tr>
</thead>
<tbody>
<?php echo form_open('admin/manage_customer') ?><!-- start of the form -->
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr>
<td class="right"><?php echo $row->cus_id; ?></td>
<td class="right">Admin</td>
<td class="right"><?php echo $row->cus_name; ?></td>
<td class="right"><?php echo $row->cus_email; ?></td>
<td class="right"><?php echo $row->cus_phone; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php echo form_close(); ?><!-- END of the form -->
</tbody>
<tfoot></tfoot>
</table>
---controller
function inbox()
{
$data = array();
if($query = $this->mod_contactus->get_records())
{
$data['records'] = $query;
}
$this->load->view('admin/admin_messages',$data);
}
---Model
<?php
class Mod_contactus extends CI_Model //Users (model name) share all the class and functions CodeIgniter models have
{
function get_records()
{
$query = $this->db->get('tbl_contactus');
return $query->result();
}
function add_record($data)
{
$this->db->insert('tbl_contactus', $data);
return;
}
function update_record()
{
}
}
i would go with hussain in this. first check if the necessary data is returned by the model or not. Only then you should check the html structure. But even if there is a problem with structure, something should be displayed. So, check the flow of data to the view first.

Categories