Codeigniter:如何为每个新闻点击计数器

时间:2016-05-14 03:27:57

标签: php codeigniter

我正在开发网站,宣传二手车在codeigniter中出售。我想通过控制器中的方法为每个新闻做点击计数器。当用户点击新闻时,会打开此新闻的模态,并且应该在数据库中的hitCounter列中添加1。我尝试了很多代码和方法来做到这一点,但它没有用。如果有人请建议在这种情况下做什么。提前谢谢!

这是我的控制器的方法代码:

                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">&times;</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;



    }

2 个答案:

答案 0 :(得分:0)

您的模型功能应该是这样的

public function set_counter($id)
{
   if($id>0)
   {
      $sql = "UPDATE avto SET hitCounter = hitCounter + 1 WHERE id = ".$id ;
      $this->db->query($sql);
   }
}

答案 1 :(得分:0)

步骤1:调用控制器功能onClick

<button type="button" class="btn btn-success" 
 onclick="<?php echo base_url()?>controller/function">Click Here</button>

步骤2:在Controller中定义各自的功能并添加更新查询..

  

你必须更新表中的计数因为实时计数器不能存储在会话中,因为有多个用户正在使用网站

现在添加名为tbl_count的表并在查询

下面应用
$qry = "UPDATE `tbl_count` SET `hits`=`hits` +1 WHERE `news_id` = '".$news_id."'";