删除功能总是选择相同的ID - 博客 - Codeigniter

时间:2017-07-07 15:37:42

标签: php codeigniter

所以,我一直试图让这个删除功能暂时工作。

在foreach的底部,我有一个删除功能。功能本身确实有效,但它总是选择帖子ID为1。

查看

<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>

控制器

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');
}     

模型

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;
}

编辑: 模型中的get_posts

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();
}

该功能确实运行,我收到一条确认消息,所以我真的很困惑的是Id。 整个过程都是使用CodeIgniter Framework编写的。

4 个答案:

答案 0 :(得分:0)

你只返回一行row_array()函数,所以你应该替换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();
  }
}

答案 1 :(得分:0)

感谢大家,您的回答对我帮助很大,我学到了一些关于PHP的新知识。我尝试实施你的想法,他们解决了我所问的问题领域。最后,我决定使用一个简单的mysqli查询来从数据库中获取所需的数据。 mysqli_query($con, "SELECT id,category_id,user_id,title,body,created_at FROM posts Order By id DESC");

答案 2 :(得分:0)

正确检查您的get_post模型。根据您的情况,您的查询看起来会从类别表中获取它的ID。

试试这个

 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();
    }

将您的联接更新为LEFT JOIN或简单使用WHERE

答案 3 :(得分:0)

可能是我错了,但我认为这是一个foreach语法问题, 因为如果你在所有的同一个id 行, 你需要写得像,

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