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>
Related
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);
}
So I've successfully made the methods in controller so when someone logs in my website and it's the admin ( value 1 in the column admin from db, and the normal users have the value 0), to redirect him to a page who only he can see. Now, I want to display all the users that are not the admin, for the admin, but I don't know how to make it work honestly.
Model:
public function display_users() {
$query = $this->db->query('SELECT id, fname, email, username, password FROM register WHERE admin = 0');
return $query->result();
}
also tried this one in the model:
$this->db->select('id', 'fname', 'lname', 'email', 'username', 'password')
->where('admin', 0)
->get('register')
->result_array();
Controller:
public function admin() {
$isLogged = $this->session->userdata('email');
if ($isLogged) {
$check_admin = $this->signup_model->admin();
if ($check_admin == 1) {
$this->signup_model->display_users();
$this->load->view('navbar');
$this->load->view('header');
$this->load->view('admin');
$this->load->view('footer');
} else {
redirect('users/login');
}
}
}
View:
<table class="usersTable">
<thead>
<tr class="column">
<th class="cell">ID</th>
<th class="cell">First Name</th>
<th class="cell">Last Name</th>
<th class="cell">Email</th>
<th class="cell">Username</th>
<th class="cell">Password</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td class="cell"><?php echo $row->id; ?> </td>
<td class="cell"><?php echo $row->fname; ?> </td>
<td class="cell"><?php echo $row->lname; ?> </td>
<td class="cell"><?php echo $row->email; ?> </td>
<td class="cell"><?php echo $row->username; ?> </td>
<td class="cell"><?php echo $row->password; ?> </td>
</tr>
<?php }} ?>
</tbody>
Do not use DB functions in your view. You need to set a variable in your controller and send it to your view, something like this :
Model
public function display_users() {
$query = $this->db->query('SELECT id, fname, email, username, password FROM register WHERE admin = 0');
return $query->result_array();
}
Controller
public function admin() {
$return = array();
$isLogged = $this->session->userdata('email');
if ($isLogged) {
$check_admin = $this->signup_model->admin();
if ($check_admin == 1) {
$return['users'] = $this->signup_model->display_users();
$this->load->view('navbar');
$this->load->view('header');
$this->load->view('admin', $return);
$this->load->view('footer');
} else {
redirect('users/login');
}
}
}
View :
<table class="usersTable">
<thead>
<tr class="column">
<th class="cell">ID</th>
<th class="cell">First Name</th>
<th class="cell">Last Name</th>
<th class="cell">Email</th>
<th class="cell">Username</th>
<th class="cell">Password</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user) { ?>
<tr>
<td class="cell"><?php echo $user['id']; ?> </td>
<td class="cell"><?php echo $user['fname']; ?> </td>
<td class="cell"><?php echo $user['lname']; ?> </td>
<td class="cell"><?php echo $user['email']; ?> </td>
<td class="cell"><?php echo $user['username']; ?> </td>
<td class="cell"><?php echo $user['password']; ?> </td>
</tr>
<?php } ?>
</tbody>
Try this:
// Get the role id of logged in user in some variable like $roleId
if($roleId == 1) // Visible only for admin
{
$resultSet = $this->db->query("SELECT id, fname, email, username, password FROM register WHERE role != '1'");
// It will give you the user list other than admin role.
return $resultSet;
}
Controller:
if ($check_admin == 1) {
$result = $this->signup_model->display_users();
// pass this variable on the view so that you can use it there.
$this->load->view('navbar');
$this->load->view('header');
$this->load->view('admin', $result); // like that
$this->load->view('footer');
} else {
redirect('users/login');
}
I have to print something like No emails found in my view.
My Controller looks:
public function viewEmailMessage()
{
if(($this->session->userdata('username')!=""))
{
$data =array();
$data['email']=$this->adminModel->viewEmailMessageModel();
$this->load->view('admin/viewEmailMessages',$data);
}
else
{
$this->load->view('admin/login');
}
}
My Model looks:
public function viewEmailMessageModel()
{
$this->db->select("*");
$this->db->from('message');
$this->db->order_by("id", "desc");
$query = $this->db->get();
return $result=$query->result();
}
My View looks:
<h4>View Emails</h4>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Message</th>
<th>options</th>
</tr>
</thead>
<tbody>
<?php
foreach ($email as $row)
{
?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->message;?></td>
<td>
<i class="fa fa-trash fa-lg "></i>
</td>
</tr>
<?php }
?>
</tbody>
</table>
How to modify my Controller, model & view. For I have to print something like "No Emails Found" in the same view when the message table is empty.
In view try this
<h4>View Emails</h4>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Message</th>
<th>options</th>
</tr>
</thead>
<tbody>
<?php
if(count($email) > 0 ){
foreach ($email as $row)
{
?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->message;?></td>
<td>
<i class="fa fa-trash fa-lg "></i>
</td>
</tr>
<?php }
} else {
echo "msg here";
}
?>
</tbody>
</table>
<h4>View Emails</h4>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Message</th>
<th>options</th>
</tr>
</thead>
<tbody>
<?php
if($email and other condition){
foreach ($email as $row)
{
?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->message;?></td>
<td>
<i class="fa fa-trash fa-lg "></i>
</td>
</tr>
<?php }
}
else{
echo your message;
}
?>
</tbody>
</table>
use count() function
and not able to use if(!empty($email)) for array variable use count()
if(count($email)==0)
{
echo "NO email found";
}
else
{
foreach($email as $row)
{
.........
}
}
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.
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>