Delete Multiple DB rows and Images with Same title

55
May 30, 2021, at 7:10 PM

I am trying to delete multiple rows based on 'slug' column using Codeigniter. Form DB all rows are deleted but the problem is with Images. From the root directory Only the first image is deleted. The the other rows images are not deleting.

Here is my Controller:

public function deletePost() {
    $slug = $this->input->post('type');
    $query = $this->db->query("Select * from posts_images where slug='$slug'");
    $img = $query->row_array();
    $imgPath = $img['image_path'];
    if (is_file($imgPath)) {
        foreach ($query as $deletRow) { // This foreach is not working
            unlink($imgPath.$deletRow);
        }
        $this->db->where('slug', $slug)->delete('posts_images');
        $this->session->set_flashdata('success', 'Posts Deleted Successfully');
        redirect('dashboard/Cproducts/showData', 'refresh');
    }
     else {
        $this->db->where('slug', $slug)->delete('posts_images');
        $this->session->set_flashdata('success', 'Posts Deleted Successfully');
        redirect('dashboard/Cposts/showData', 'refresh');
    }
}

Here is the DB screenshot:

In the above screenshot, the second-row Image is not deleting. I have applied the foreach before deleting the rows from DB, but the foreach is not working. Any help will be much appreciated.

Thanks

Answer 1

Call the "where" method before the "delete" method.

Try this:

$this->db->where('slug', $slug)->delete('posts_images');
READ ALSO
Click count using JavaScript for many buttons with default value '0'

Click count using JavaScript for many buttons with default value '0'

I want to use a click count function in JavaScript to measure the number of clicks on a button in otreeThe data should be stored in the database using a hidden input field

89
Django login error, LOGIN_REDIRECT_URL is not working

Django login error, LOGIN_REDIRECT_URL is not working

When I click the login button the next page shows HTTP ERROR 405

96
Notifiy my RecyclerView that data has changed

Notifiy my RecyclerView that data has changed

I am developing an shop app in android and got a question about transfering data between two adapterclassesThis is the scenario:

94
Add animation to React Slider

Add animation to React Slider

I have a very simple react sliderThere's an option to navigation between the slides

76