我想从数据库中获取值的ID,并使用它来删除项目。 继承我的代码:
此代码当前从数据库中获取一组记录,其中每个记录都有链接。我怎么能这样做?
<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中,其中polls是数据库的表名
class Admin_model
....
function delete($id)
{
$this->db->delete('polls', array('id' => $id));
}
我希望这很清楚。
答案 1 :(得分: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
}
请查看Codeigniter documentation