我正在开发宣传二手车在codeigniter出售的网站。我想通过控制器中的方法为每个新闻做点击。当用户点击新闻时,这个消息的模式打开,它应该添加1到我的数据库中的hitCounter列。我尝试了很多代码和方法来做到这一点,但它不工作。如果有人请在这种情况下建议做什么。提前致谢!Codeigniter:如何做每个新闻的计数器
这里我控制器的方法代码:
public function clickCounter($id)
{
$this->news_model->set_counter($id);
}
这里我的看法代码:
<?php foreach ($news as $news_item): ?>
<div class="font-icon-list col-lg-3 col-md-3 col-sm-4 col-xs-12" style="padding: 15px;" onclick="document.getElementById('<?php echo $news_item['id']; ?>').style.display='block'; ">
<div class="font-icon-detail"><img src="<?php echo base_url(); echo "images/thumb/images/"; echo $news_item['image_path']; ?>" alt="" style=" max-width: 220px; height:100%;">
<input type="text" value="<?php echo $news_item['location']; echo " - "; echo $news_item['price']; ?>">
<input type="text" value="<?php echo $news_item['telno']; ?>">
</div>
</div>
<div id="<?php echo $news_item['id']; ?>" class="w3-modal">
<div class="w3-modal-content w3-animate-zoom" >
<header class="w3-container">
<span onclick="document.getElementById('<?php echo $news_item['id']; ?>').style.display='none'"
class="w3-closebtn">×</span>
</header>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12" style="background-color: white; padding:0;">
<table class="table table-hover table-striped" style="white-space:wrap; overflow:hidden;">
<tr>
<td colspan="2" class="text-center"><?php echo $news_item['title']; ?></td>
</tr> <tr>
<td>Сана</td><td><?php echo $news_item['date']; ?></td>
</tr>
<tr >
<td>Нархи</td><td><?php echo $news_item['price']; ?></td>
</tr>
<tr>
<td>Исми ва шарифи</td><td><?php echo $news_item['name']; ?></td>
</tr>
<tr>
<td>Телефон рақам</td><td><?php echo $news_item['telno']; ?></td>
</tr>
<tr>
<td colspan="2" class="text-center">Қўшимча маълумот:</td>
</tr>
<tr>
<td class="text-justify" colspan="2"><?php echo $news_item['info']; ?></td>
</tr>
</table>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
这里我的模型代码:
public function set_counter($id)
{
$sql = "SELECT * FROM avto WHERE id = ?" ;
$query = $this->db->query($sql, $id);
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$data['hitCounter'] = $row['hitCounter'];
}
$numHits= $data['hitCounter'];
$numHits = $numHits + 1;
$data = array(
'hitCounter' => $numHits,
);
$this->db->where('id', $id);
$this->db->update('avto', $data);
}
return false;
}