0
votes

So, I have been trying to get this delete function to work now for a while.

At the bottom of the foreach I have a delete function. The funtion itself does work, however it always selects the post id of 1.

View

<div><?php foreach($posts as $post) : ?>
<hr>
<h3><?php echo $post['title']; ?></h3>
<div class="row">
    <div class="col-md-3">
        <img class="post-thumb" src="<?php echo site_url(); ?>posts/image/<?php echo $post['post_image']; ?>">
    </div>
    <div class="col-md-9">
        <small class="post-date">Posted on: <?php echo $post['created_at']; ?> in <strong><?php echo $post['name']; ?></strong></small><br>
    <?php echo word_limiter($post['body'], 60); ?>
    <br><br>
    <p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p>
    </div>
</div>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
    <input type="submit" value="Delete" class="btn btn-danger">
</form>

Controller

public function posts($offset = 0){


  // Pagination Config  
  $config['base_url'] = base_url() . 'admins/posts/';
  $config['total_rows'] = $this->db->count_all('posts');
  $config['per_page'] = 10;
  $config['uri_segment'] = 3;
  $config['attributes'] = array('class' => 'pagination-link');

  // Init Pagination
  $this->pagination->initialize($config);

  $data['title'] = 'Latest Posts';

  $data['posts'] = $this->post_model->get_posts(FALSE, $config['per_page'], $offset);



    $this->load->view('templates/header');
    $this->load->view('admins/posts', $data);
    $this->load->view('templates/footer');
}     

Model

public function delete_post($id){
  $image_file_name = $this->db->select('post_image')->get_where('posts', array('id' => $id))->row()->post_image;
  $cwd = getcwd(); // save the current working directory
  $image_file_path = $cwd."\\assets\\images\\posts\\";
  chdir($image_file_path);
  unlink($image_file_name);
  chdir($cwd); // Restore the previous working directory
  $this->db->where('id', $id);
  $this->db->delete('posts');
  return true;
}

EDIT: get_posts in model

public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
  if($limit){
    $this->db->limit($limit, $offset);
  }
  if($slug === FALSE){
    $this->db->order_by('posts.id', 'DESC');
    $this->db->join('categories', 'categories.id = posts.category_id');
    $query = $this->db->get('posts');
    return $query->result_array();
  }

  $query = $this->db->get_where('posts', array('slug' => $slug));
  return $query->row_array();
}

The function does run and I get a confirmation message, so the only thing I am really confused about is the Id. The whole thing is written using the CodeIgniter Framework.

4
make sure you got a different value for each post when you echo out $post['id'] in your view - Rildo Gomez
The ID in the PHPmyAdmin table is an Auto-Increment, thus there is a different value for each. - frankjamesleo
yes, I understand that but I have no idea how you get your data with the function get_posts, so I'm asking you if you see a different value for $post['id'] in this line <?php echo form_open('/posts/delete/'.$post['id']); ?> when you press f12 - Rildo Gomez
what is result of print_r($data); in controller before calling view - Jigar Shah
It always shows the value 1 @Rildo Gomez - frankjamesleo

4 Answers

0
votes

Check your get_post model properly. From what you have, it looks like your query will get it's id from the category table.

Try this instead

 public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
      if($limit){
        $this->db->limit($limit, $offset);
      }
      if($slug === FALSE){
        $this->db->select('posts.id AS id, posts.slug, posts.body, posts.created_at');
        $this->db->from('posts, categories');
        $this->db->where('categories.id = posts.category_id');
        $query = $this->db->get();      
        return $query->result_array();
      }

      $query = $this->db->get_where('posts', array('slug' => $slug));
      return $query->result_array();
    }

Updating your join to LEFT JOIN or simple use WHERE

0
votes

you are returning only 1 line with row_array() function so you should replace for result_array():

public function get_posts($slug = FALSE, $limit = FALSE, $offset = FALSE){
  if($limit){
    $this->db->limit($limit, $offset);
  }

  if($slug === FALSE){
    $this->db->order_by('posts.id', 'DESC');
    $this->db->join('categories', 'categories.id = posts.category_id');
    $query = $this->db->get('posts');
    return $query->result_array();
  }else{
    $query = $this->db->get_where('posts', array('slug' => $slug));
    return $query->result_array();
  }
}
0
votes

Thanks to everyone, your answers helped me a lot and I learned a few new things about PHP. I tried implementing your Ideas and they solved the area of the problem I was asking about. In the end I decided to use a simple mysqli query to get the data I needed from the database. mysqli_query($con, "SELECT id,category_id,user_id,title,body,created_at FROM posts Order By id DESC");

0
votes

May be i am wrong but i think its a foreach syntax issue, because if you getting same id in all the rows, you need to write like,

<?php foreach ($posts as $post) : ?>
instead of,
<?php foreach ($posts as $post) { ?>
//your code goes here
<?php } ?>