我是 codeigniter 的新手。在我的 View 页面中,我在表中显示数据库中的数据,其中有两个用于更新和删除的 anchor 标记。我想通过id从数据库中删除特定行。
我的查看页面是
<?php foreach($query as $row){ ?>
<tr>
<td><?php echo $row->name ?></td>
<td><?php echo $row->testi ?></td>
<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
<td><a href="#ajax-modal2" data-id="delete[]" role="button" class="Delete" data-toggle="modal" onClick="confirm_delete()"><i class="icon-trash"></i></a></td>
</tr>
<?php } ?>
</table>
我的 Controller 页面是
function delete_row()
{
$this->load->model('mod1');
$this->mod1->row_delete();
redirect($_SERVER['HTTP_REFERER']);
}
我的模型页面是
function row_delete()
{
$this->db->where('id', $id);
$this->db->delete('testimonials');
}
我想通过捕获相应的 id 来删除该行。请不要严厉。谢谢
最佳答案
您在模型中使用了 $id
变量,但您是从任何地方提取它的。您需要将 $id
变量从 Controller 传递到模型。
Controller
让我们通过 row_delete()
方法的参数将 $id 传递给模型。
function delete_row()
{
$this->load->model('mod1');
// Pass the $id to the row_delete() method
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);
}
型号
将 $id 添加到模型方法参数中。
function row_delete($id)
{
$this->db->where('id', $id);
$this->db->delete('testimonials');
}
现在的问题是您从 Controller 传递了 $id
变量,但它没有在 Controller 中的任何位置声明。
关于php - 如何删除codeigniter中的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19700582/