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