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