2012-10-04 76 views
0

我想從數據庫獲取值的ID並使用它刪除項目。 繼承人我的代碼:從數據庫獲取ID並將其寫入URI

此代碼當前從數據庫中獲取一組記錄,其中每個記錄都有鏈接。我怎樣才能做到這一點?

<h2>List of all polls in this site:</h2> 
<?php echo '<table>';?> 
<table border="1"> 
    <tr> 
    <th>Title</th> 
    <th>Text</th>  

    <th colspan="2">Action</th>   
    </tr> 
<?php 
foreach($polls as $polls_item){ 
    echo "<tr>"; 
    echo "<td>". $polls_item['title'] ."</td>"; 
    echo "<td>". $polls_item['text'] ."</td>"; 
    echo "<td>". anchor('user/vote/'.$polls_item->id,'Vote') ."</td>"; 
    echo "<td>". anchor('admin/del/'.$polls_item->id,'Delete') ."</td>";   
    echo "</tr>"; 
} 
?> 

<?php echo anchor('admin/del/1','Delete'); ?> 

</table> 
+0

告訴我你得到了什麼當你懸停在刪除鏈接.. –

回答

0

我會假設你有一個管理控制器,而你在這,你要把所有的民意調查開始加載admin_model

class Admin 
.... 
function del($id = null) 
{ 
    $this->admin_model->delete($id); 
} 

然後在你的admin_model其中民調是表名您的數據庫

class Admin_model 
.... 
function delete($id) 
{ 
$this->db->delete('polls', array('id' => $id)); 
} 

我希望這已經夠清楚了。

+0

謝謝我在我的管理和模型類,它只是當我運行它,ID是未定義的。我不知道如何使用這個ID獲得ID? –

+0

這條線在這裏:$ polls_item-> id .......我如何得到id? –

+0

你的意思是你的$ polls_item-> id給出了undefined或者它在del($ id)函數中是未定義的? –

0

您的 「管理員」 控制器function del

function del($item_to_delete) 
{ 
    $this->your_model->delete_item($item_to_delete); 
} 

在你的模型:

function delete_item($id) 
{ 
    $this->db->delete('mytable', array('id' => $id)); 
    // some successful message to return 
} 

請隨便看看笨documentation

+0

嘿,謝謝..我有這樣的編碼位只是不知道如何將它連接到每個項目的刪除鏈接.. –

相關問題