I want to add a link in a table which download and delete my uploaded data in codeigniter. It's my viewer and it only shows the file name and date from the database which I had uploaded
foreach($query as $row)
{
echo "<tr>";
echo "<td><h2>". $row->filename ."</h2></td>";
echo "<td><h2>". $row->date ."</h2></td>";
echo "<td><h2>".$row->filelink."</h2></td>";
//now how can i make download and delete from my database
//my database contains filename,fileid,filelink
echo "</tr>";
}
My model is
function showfile()
{
$this->db->select('filename, filelink, date');
$query = $this->db->get('upload_file');
return $query->result();
}
My controller
function index()
{
$this->load->model('showuploadmodel');
$data['query']=$this->showuploadmodel->showfile();
$this->load->view('showupload_view',$data);
}
I want to add a delete link and download link and delete only a specific row from database and download a file for which a Id is passed. How can I pass field from database into download helper?
pass field to controller that calls download helper and not to downloadhelper itself
try this
foreach($query as $row)
{
echo "<tr>";
echo "<td><h2>". $row->filename ."</h2></td>";
echo "<td><h2>". $row->date ."</h2></td>";
echo "<td><h2>".$row->filelink."</h2></td>";
echo "<td><h2><a href='".site_url('path/to/controllers/download/'.$row->id)."'>Download</a></h2></td>";
echo "<td><h2><a href='".site_url('path/to/controllers/delete/'.$row->id)."'>Delete</a></h2></td>";
echo "</tr>";
}
and in controller add related function
function download($id){
//$id is that particular Id
//do stuff with download helper
}
function delete($id){
//$id is that particular Id
//delete code
}
Related
I am trying to add students in the database but the the students are not been added to database.
the code for the controller is as follows:
public function add_student(){
$this->load->model('Stud_Model');
$data=array(
'roll_no'=>$this->input->post('roll_no'),
'name'=>$this->input->post('name'),
'role'=>$this->input->post('role'),
);
$this->Stud_Model->insert($data);
$query=$this->db->get("stud");
$data['records']=$query->result();
$this->load->view('Stud_view',$data);
}
the code for the model is as follows:
public function insert($data){
$this->db->insert('stud',$data);
if($this->db->insert('stud',$data)){
return true;
}
}
the code for the view is as follows:
<?php
echo form_open('Stud_controller/add_student');
echo form_label('Roll No.:');
echo form_input(array('id'=>'roll_no','name'=>'roll_no','role'=>'roll_no'));
echo "<br/>";
echo form_label('name:');
echo form_input(array('id'=>'name','name'=>'name','role'=>'name'));
echo "<br/>";
echo form_label('Role:');
echo form_input(array('id'=>'roll_no','name'=>'roll_no','role'=>'role'));
echo "<br/>";
echo form_submit(array('id'=>'submit','value'=>'Add'));
echo form_close();
?>
for model
public function insert($data)
{
return $this->db->insert('stud',$data);
}
for retrive data in model
public function GetAllStudent()
{
$this->db->select('*');
$this->db->from('stud');
$query = $this->db->get();
return $query->result();
}
for retrive data in controller
$data['all_std_info'] = $this->Stud_Model->GetAllStudent();
The order of the views change wen i make a call to a database. In this case i make a Formulari.php that is a Controller.
Formulari.php
public function resum(){
**$this->load->view('header');**
$query = $this->db->query("SELECT * FROM tarifes");
# code...
foreach ($query->result_array() as $row) {
echo $row['operador'];
echo $row['minutatge'];
echo $row['permanencia'];
echo $row['dades'];
echo $row['preu'];
echo '</br>';
}
$this->load->view('resum_taula');
$this->load->view('footer');
}
When I see this controller the first i can see is the table that returns me. But que first view i want to see is the title.
Thanks a lot!
Controller
$data['formular_data'] = $this->your_model->getData();
$this->load->view('resum_taula', $data);
$this->load->view('footer');
Model
function getData() {
$query = $this->db->get('tarifes');
return $query->result_array();
}
View
<?php print_r($formular_data'); ?> // form $data['formular_data'];
i am trying to access a function in model, in my view in codeigniter and its not working.
can somebody please tell me where is the problem.
MODEL:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
VIEW:
<select id="special" >
<?php echo $this->dashboard_ext_model->gettemplates(); ?>
</select>
Change this:
echo "<pre>"; print_r($result);exit;
To this:
echo "<pre>"; print_r($result); echo "</pre>";
Basically remove the exit. Exit aborts the script.
You should never have a good reason to call the model from the view. Model data should be passed to the controller, to then pass to the view.
Since this is MVC (Model View Controller) you shouldn't call a model from a view.
You should be calling the Model inside the controller then passing to the view as an argument.
Model:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
Controller:
function gettemplates(){
//$this->dashboard_ext_model = $this->load->model('dashboard_ext_model');
//uncomment this if you didn't load the model
$data['templates'] = $this->dashboard_ext_model->gettemplates();
$this->view->load('page_name', $data);
}
View:
<select id="special" >
<?php echo $templates ?>
</select>
MVC may look stupid at first but it really helps in large size projects.
MVC DESIGN: http://i.stack.imgur.com/Beh3a.png
I'm trying to compare the libraries of books of two users. I am displaying another user's library, and I would like for there to be a link to "Ping" under the book if the current user also has that book in his library.
Here is the relevant controller code:
function other_user_library()
{
$this->load->model('user_library_model');
$this->load->model('user_information_model');
$data['other_user_library']=$this->user_library_model->other_user_library($username);
$data['my_book']=$this->user_library_model->compare_libraries();
$this->load->view('other_user_library', $data);
}
Here is the model:
function other_user_library($username)
{
$this->load->database();
$this->db->select('*');
//rest of query to display all of other user's books
$query= $this->db->get();
return $query->result();
}
function compare_libraries()
{
$this->load->database();
$this->db->select('BOOK.isbn');
//rest of query to return the current user's list of isbns in his library
$query= $this->db->get();
return $query->result();
}
And here's the relevant View code:
<?php foreach ($other_user_library as $row) :?>
<?php if($row->isbn)
{
foreach ($my_book as $thing):
if($thing->isbn ==$row->isbn)
{
$ping = TRUE;
if($ping)
{
echo anchor('Ping');
}
}
else
{
echo "somethingelse";
}
endforeach;
}?>
<?php endforeach;?>
Now, as it stands, for a user who has 8 books, one of which matches the book in the other user's library, this will display "somethingelse" 7 times and then the "Ping" link once. I am wondering if there is a way just to go through the 8 isbns, see that ONE of them matches, and then just display the "Ping" link. I have tried just removing the else{echo "somethingelse";} line. And then it displays just the "Ping" for the matching books. However, I would like to be able to put something else under those books that do not match, as opposed to just leaving them blank. Thank you!
I can't follow your entire code. But I've made changes to where I see need for obvious modifications. Give it a try.
<?php foreach ($other_user_library as $row) : ?>
<?php
$ping = FALSE; //set ping here
if ($row->isbn) {
foreach ($my_book as $thing):
if ($thing->isbn == $row->isbn) {
$ping = true;
break; //prevents the continuous loop after a match incase you have thousands of rows
}
endforeach;
if ($ping) { //Do the echo after this point to have just one echo. You could still move out of the parent foreach loop as long as $ping is defined first.
echo anchor('Ping');
} else {
echo "somethingelse";
}
}
?>
<?php endforeach; ?>
In one of my new codeigniter project, one of my colleague wrote a helper method array_to_object so that he can call the variables in views as $row->field instead of $row['field'].
I thought codeigniter default behaviour allows us to retrieve data from database in $row->field (as object). Can anyone pls enlight me data flow in codeigniter?
Codeigniter supports both an array and an object oriented style to retrieve data from DB so both of these styles are equal(from the user guide):
oop style
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
array style
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['email'];
}
here is the user guide:
http://codeigniter.com/user_guide/database/examples.html