Welcome Guest, Not a member yet? Register   Sign In
Need help with my delete button
#1

im trying to delete a post 

here is my code 


controler 

Code:
<?php

namespace App\Controllers;

use App\Models\NewsModel;
use CodeIgniter\Controller;

class News extends Controller
{

public function index()
{
  $model = new NewsModel();

  $data = [
   'news' => $model->getNews(),
   'title' => 'News archive',
  ];

  echo view('templates/header', $data);
  echo view('news/overview', $data);
  echo view('templates/footer', $data);
}

public function view($slug = null)
{
  $model = new NewsModel();

  $data['news'] = $model->getNews($slug);

  if (empty($data['news'])) {
   throw new \CodeIgniter\Exceptions\PageNotFoundException('Cannot find the news item: ' . $slug);
  }

  $data['title'] = $data['news']['title'];

  echo view('templates/header', $data);
  echo view('news/view', $data);
  echo view('templates/footer', $data);
}
public function create()
{
  $model = new NewsModel();

  if ($this->request->getMethod() === 'post' && $this->validate([
   'title' => 'required|min_length[3]|max_length[255]',
   'body' => 'required',
  ])) {
   $model->save([
    'title' => $this->request->getPost('title'),
    'slug' => url_title($this->request->getPost('title'), '-', true),
    'body' => $this->request->getPost('body'),
   ]);

   echo view('news/success');

  } else {
   echo view('templates/header', ['title' => 'Create a news item']);
   echo view('news/create');
   echo view('templates/footer');
  }
}
public function delete()
{
  $model = new NewsModel();

  $model->where('id', )->delete();

  return view('news/delete');

}
}


model

PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
NewsModel extends Model
{
 protected 
$table 'news';

 protected 
$allowedFields = ['title''slug''body'];

 public function 
getNews($slug false)
 {
  if ($slug === false) {
   return $this->findAll();
  }

  return $this->asArray()
   ->where(['slug' => $slug])
   ->first();
 }



Code:
<h2><?=esc($news['title'])?></h2>
<p><?=esc($news['body'])?></p>



<a href="/news/delete" class="btn btn-danger">Delete</a>
if you look at my controler you can see i pass in 

Code:
  $model->where('id',1 )->delete();
it deletes the post with id 1 but how can i make it so i is dynamic and it can delete the post i am on for example if im at post 3 it should delete post 3 not post 1 

i have tried passing in id

Code:
public function delete($id)
{
  $model = new NewsModel();

  $model->where('id',$id )->delete();

  return view('news/delete');

}
but it say that the variable id is not defined and i canot seem to understand what is wrong i am learning and is a bit of a noob.

sorry for my bad English
Reply
#2

Hi
The code you have written is basically the start of a CRUD type project.
CRUD stands for Create, Read (view), Update and Delete.
It is a standard layout for many applications.
Lookup "CodeIgniter 4 CRUD" on the internet and you will find complete examples.

Your controller delete($id) function has an $id parameter but when you call this delete fron your view
you do not pass the id.
<a href="/news/delete" class="btn btn-danger">Delete</a>
should be
<a href="/news/delete/$id" class="btn btn-danger">Delete</a>
but I don't think you have passed any ids to the view.
So check out the internet examples as mentioned above.
Reply
#3

(03-31-2021, 05:34 AM)[email protected] Wrote: Hi
The code you have written is basically the start of a CRUD type project.
CRUD stands for Create, Read (view), Update and Delete.
It is a standard layout for many applications.
Lookup "CodeIgniter 4 CRUD" on the internet and you will find complete examples.

Your controller delete($id) function has an $id parameter but when you call this delete fron your view
you do not pass the id.
<a href="/news/delete" class="btn btn-danger">Delete</a>
should be
<a href="/news/delete/$id" class="btn btn-danger">Delete</a>
but I don't think you have passed any ids to the view.
So check out the internet examples as mentioned above.


okay thank you i have tried checking on other peoples projects  but i stil havent manage to get it right i must be doing something wrong!
Reply
#4

You can use the ID of the news item. I am assuming the ID field here is simply "id".

PHP Code:
// your view


<h2><?= esc($news['title']) ?></h2>
<p><?= esc($news['body']) ?></p>

<a href="<?= '/news/delete/' esc($news['id']) ?>">Delete</a>


// in your controller's delete method
public function delete($id)
{
    $model = new NewsModel();
    $model->where('id', (int) $id)->delete();

    return view('news/delete');


A little comment if you don't mind. I'm also assuming this delete button is only within the reach of the admins. If you release this to unprivileged users, all posts are at risk to be deleted.
Reply
#5

paulbalandanThank you so much this is what worked for me becuase i did figure out if you echo news['id Wrote:it gives the id but i did not know how to to get it in my controller THANKS pid='385678' dateline='1617202077']
could you please explain to me what i did wrong


and also thank you for the comment you made i will keep that in mind 


 
 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB