2017-07-07 27 views
0

所以,我一直试图让这个删除功能现在工作一段时间。删除功能总是选择相同的ID - 博客 - Codeigniter

在foreach的底部我有一个删除功能。该funtion本身做的工作,但它总是选择的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; 
} 

编辑的帖子ID: 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框架编写的。

+0

确保你得到了每个岗位不同的值,当你在您的视图中回显$ post ['id'] –

+0

PHPmyAdmin表中的ID是自动增量,因此每个值都有不同的值。 – frankjamesleo

+1

是的,我了解,但我不知道如何使用函数get_posts获取数据,所以我问你是否在此行中看到$ post ['id']的不同值 <?php echo form_open ( '/posts/delete/'.$post['id']); ?> 当你按f12 –

回答

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

+0

使用WHERE更新了代码并指定了查询选择posts.id而不是可能已经选择category.id作为编号的模糊语句 – Oluwaseye

0

你只返回1 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(); 
    } 
} 
+0

我试过了,ID仍然总是为1 – frankjamesleo

+0

@frankjamesleo我认为这就是您要做的 –

0

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

0

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

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

它实际上并不总是相同的帖子ID,它被设置为帖子类别的ID。我使用<?php foreach($ posts as $ post):?>,它工作正常。谢谢你的回答tho – frankjamesleo

+0

嗯... 我真的不知道这个接近,所以这就是为什么我要求花括号, 谢谢, – Rits