我在MySql数据库中有一个表 post_id,post_number,post_like
我在codeigniter中使用MVC从mysql表中获取所有数据并将其显示在页面上(此部分已完成)
我需要为每个 post_id 添加一个按钮,为mysql表中的 post_like 添加+1,对应于post_id。
因此,如果我点击post_id 7的+1按钮,post_like应仅为post_id 7增加。
这是我到目前为止所拥有的!
view_page.php
<?php foreach($members as $value): ?>
<tr>
<td><?php echo $value['post_id']; ?></td>
<td><?php echo $value['post_like']; ?></td>
<td> <a href="plusone/<?php echo $value['post_id']; ?>">Add +1</a></td>
</tr>
<?php endforeach; ?>
controller_page.php
<?php
function plusone($post_id){
$this->load->model('model_page');
$this->model_page->data_addition($post_id);
redirect('controller_page/viewdata');
}
?>
Model_page.php
<?php
function data_addition($post_id){
//trying to add 1 the post_like and update it in the db
$add_one_to_data = $post_like+ 1;
$data = array(
'post_like' => $add_one_to_data +1
);
$this->db->where('post_id', $post_id);
$this->db->update('post_tbl', $data);
}
?>
请不要嘲笑我在阵列上方添加的 $ add_one_to_data ,我不知道还能做什么
提前致谢
答案 0 :(得分:1)
你需要首先得到ifno:
function data_addition($post_id){
$post_like = $this->db->get_where('post_tbl', array('post_id' => $post_id))->row();
//trying to add 1 the post_like and update it in the db
$add_one_to_data = (int) $post_like->post_like + 1;
$data = array(
'post_like' => $add_one_to_data +1
);
$this->db->where('post_id', $post_id);
$this->db->update('post_tbl', $data);
}
答案 1 :(得分:1)
用此
替换您的data_addition
功能
function data_addition($post_id){
$this->db->set('post_like', 'post_like+1', FALSE);
$this->db->where('post_id', $post_id);
$this->db->update('post_tbl');
}