2017-07-07 23 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