Delete from the database using CodeIgniter - php

I want to make one delete function, but I do not know what steps did wrong?
This is the file model:
function delete()
{
$this->db->delete('ns_categories', array('cat_id' => $cat_id));
}
Controller:
function delete()
{
$this->cat_model->delete();
$cat_id=$this->uri->segment(3);
if($cat_id->delete()) return json_encode(array("success" => true));
}
View:
<td class="centeralign"><a class="deleterow" href="<?php echo anchor('admin/categories/delete' .<?php echo $row['cat_id']; ?>"><span class="icon-trash"></span></a></td>
Please help me.

You haven't done anything right, no offense. It's a mess.
View:
You missed a forward slash here:
href="<?php echo 'admin/categories/delete' .<?php echo $row['cat_id']; ?>"
^^^
...but you don't use anchor() this way anyways, it generates an entire link. And you have nested two <?php echos... What a mess. Do it like this:
<a href="<?php echo base_url('admin/categories/delete/'.$row['cat_id']; ?>">
Model:
You haven't defined $cat_id in the model's delete function. Also, don't return json from a model. Do it like this:
function delete($cat_id = null)
{
return $this->db->delete('ns_categories', array('cat_id' => $cat_id));
}
Controller:
You're calling delete as a method of $cat_id which makes no sense, as the variable contains a number - not an object you can call a method on. You also have to echo/print the json, and you should set json headers. Do it like this:
function delete($cat_id = null) {
$status = $this->cat_model->delete($cat_id);
header('Content-type: application/json');
echo json_encode(array("success" => $status));
}
You should use POST method to delete things, otherwise people can delete things accidentally or do stupid tricks like <img src="delete/item/1">

Remove extra echo from link:
<td class="centeralign">
<a class="deleterow" href="<?php echo anchor('admin/categories/delete' . $row['cat_id']); ?>">
<span class="icon-trash"></span>
</a>
</td>

In your Controller delete function you have got the category id after calling the delete function of model why? And in your model function from where you are getting $cat_id ?? it should be like this
function Category_delete(){
$cat_id=$this->uri->segment(3);
if($this->cat_model->delete($cat_id)) return json_encode(array("success" => true));
}
In your view you have opened the <?php again and also you are using anchor function in the href anchor function generates the complete <a> tag see reference for helpers CI Helpers
<td class="centeralign"><a class="deleterow" href="<?php echo site_url('admin/categories/Category_delete/'.$row['cat_id']; ?>">
<span class="icon-trash"></span></a></td>
And you should rename the functions because delete may be a keyword so use ordinary names for the functions

Related

How to pass argument for function in controller from view

I have function in controller with one argument and now I want to pass value for it from view through url.
courses.php
public function index($id){
print_r();
exit;
}
coursesview.php
<a class="jscroll-next" href="<?php echo base_url(); ?>/courses/index?page=<?php echo $nextPage; ?>&id=<?php echo $parentId ?>">next page</a>
How it possible?
From the user guide,
Simply put the parameter in an array and pass it to the view by using the second parameter of load->view().
Afterwards, in the view, the variable will be available as the key.
In this example: $id or $anothervar.
courses.php
public function index($id){
$data = array('id' => $id, 'anothervar' => 'yeahi');
$this->load->view('coursesview', $data);
// exit;
}
coursesview.php
echo $id; // the ID
echo $anothervar; // "yeahi"
You are going to use minimum arguments in URL, here is my suggestions
1.<a class="jscroll-next" href="<?php echo base_url('courses/$nextPage/$parentId/'); ?>">next page</a> //controller followed by arguments - if you call index method
(OR)
2.<a class="jscroll-next" href="<?php echo base_url('courses/methodname/$nextPage/$parentId/'); ?>">next page</a> //controller,method name and followed by arguments
How to get those arguments in controller
Example:
$nextPage = $this->uri->segment(3);
$parentId = $this->uri->segment(4);

How to add ID (number) to Codeigniter URL

I like to preface this question by apologizing for being noob.
For codeigniter URL, format goes:
example.com/class/function/ID
If I were to have the urls below
website.com/books/chapter/1
website.com/books/chapter/2
I know how to create class named "books" under which I would create public function "chapter", so...
public funtion chapter() {
$this->load->view("content");
}
How would I add the ID's 1 and 2, assuming the only difference between the two website is I would have:
1.png within my "content" for website.com/books/chapter/1
and
2.png within my "content" for website.com/books/chapter/2, with 2.png replacing where 1.png was supposed to be.
Thanks!
Since you are using this URL: website.com/books/chapter/2, you are already passing the id after the last slash. Therefore your controller method should receive that ID as a parameter like this:
Controller:
public function chapter($id) {
$data["image_id"] = $id;
$this->load->view("content", $data);
}
View:
<img src="<?= $image_id ?>.png">
Controller:
public function chapter() {
$data["image_id"] = $this->uri->segment(3); //since your ID(number) is in third segment so 3
$this->load->view("content", $data);
}
View:
<img src="<?= $image_id ?>.png">
for more info on URI class
https://ellislab.com/codeigniter/user-guide/libraries/uri.html
The ids are just the method parameters. You can get it by passing the parameter in your method.
In the controller:
public function chapter($id) {
$data["image_id"] = $id;
$this->load->view("your_view", $data);
}
In your view:
<img src="<?php echo $image_id;?>.png"/>

CodeIgniter URL issue

I am having difficulty getting the correct URL when I call a method to load a view.
Heres my controller:
public function post() {
$title = $this->input->post('title');
$data = $this->p_Model->post($title);
$this->qs($data);
}
public function qs($id){
$title = $this->s_Model->getTitle($id);
$result = $title->result();
$this->load->view('q_View', array('results' => $result));
}
Heres my view:(note this view is not the view which gets loaded from the qs function, but one which calls the qs function)
<html>
<body>
<table>
<?php
if (isset($qs)) {
foreach ($qs as $row) {
$id = $row->qID;
echo '<a href="'.site_url('myController/qs/'.$id).'">';
echo $row->title;
echo "<br>";
}
}
?>
</table>
</body>
</html>
So in my controller I have two functions, the qs function works separately by itself so can be called in the view and give the following url myController/qs/1 however when I use the post function I get a url like this myController/post so my question is how can I get my url to be like the first example?
Instead of using the line:
$this->qs($data);
You can use a redirect:
redirect('/mainController/qs/'.$data);
That should work in the same way that you have used in your view
Try base_url and also you can use current_url() returns the full URL (including segments) of the page being currently viewed.
echo '<a href="'.base_url('myController/qs/'.$id).'">';

where to process mysql queries in codeigniter?

Where should we process mysql queries in CodeIgniter application?
For example in a simple project we do like this :
for controller:
class Blog extends CI_Controller {
function posts(){
$data['query'] = $this->blog_model->index_posts();
$this->load->view('blog_view', $data);
}
}
and in view :
<?php
while ($post = mysql_fetch_object($query)):
?>
<div>
<p><?= $post->body; ?></p>
</div>
<?php endwhile; ?>
But, if we want to do something with body of post before print where should it be done?
For example, I want to write a function that formats the body of post and pass the body to it before doing echo.
Where should it be placed according to CoeIgniter's structure and recommended practices? (best option)
in the controller? (if so , how to use it)
in the view?
write a helper?
other approaches ?
Here's what is recommended:
Controller:
function posts() {
$this->load->model("blog_model");
$data['rows'] = $this->blog_model->index_posts();
$this->load->view("blog_view", $data);
}
Model: (blog_model.php)
function index_posts() {
$this->load->database();
$query = $this->db->get('your_table');
$return = array();
foreach ($query->result_array() as $line) {
$line['body'] = ... do something with the body....
$return[] = $line;
}
return $return;
}
View: (blog_view.php)
<?php foreach ($rows as $line): ?>
<div>
<p><?php echo $line['column']; ?></p>
</div>
<?php endforeach; ?>
Basically what happens is your model returns a multidimensional array that is passed the view and processed using a foreach() loop.
Good luck!
If you want to reuse that function create a helper. If you want this function only once put it in your controller and call from that controller.
Models are just for accessing database or maybe in few other cases, but mostly just for accessing things in database or editing, deleting etc. and sending the result to controller for further processing.
In your case I would stick with helper.
E.g. you will create a file top_mega_best_functions.php and put it inside helpers folder.
Than you write ther e.g. something like
function red_text($input) {
echo '<span style="color: red;">';
echo $input;
echo '</span>';
}
Then load the helper in your autoloader file or load before using.
And use in your view or controller like
$blablabla = "This text will be red";
red_text($blablabla);

how do i pass a view variable to the controller in codeigniter?

My View :-
<html>
<?= link_tag(base_url().'css/simple.css'); ?>
<body>
<?php $this->load->helper('form'); ?>
<?php $this->load->view('commentform'); ?>
<?php $id=$this->uri->segment(3);?>
<?php echo $id;?>
</body>
</html>
i would like to use the variable $id in my controller.I'm using codeigniter by the way, and am a beginner. I would appreciate any help on this.
you should not call the $id from the View, you should get it at the controller level and pass it to the View.
as Bulk said. you URL will be something like that
www.mysite.com/thecontrollername/thefunction/id
for example your controller if home and there is a show_id function in it and your view is call show_id_view.php.
you will have your url like this: www.mysite.com/home/show_id/id
your function in home will read the id"
in your home controller:
function show_id(){
$id=$this->uri->segment(3);
$view_data['id'] = $id;
$this->load->view('show_id_view',$view_data);
}
in the view (show_id_view):
<?php echo $id ?>
nothing else..
hope this helps.
Well, ideally you wouldn't do it this way. You should assign the variable first in the controller and pass it to the view if you need to use it there.
$data['id'] = $this->uri->segment(3);
$this->load->view('view_file', $data);
$id would then be available in your view as well.
in my view i add link
<li><a href="<?php echo base_url()?>link/show_id/mantap"> coba </li>
in my link.php controler i add function show_id
function show_id(){
$id=$this->uri->segment(3);
$data['coba'] = $id;
$this->mobile->view('**daftarmember_form**',$data);
in my next view daftarmember_form.html
)
<?php echo $id;?>
they print mantap,

Categories