Codeigniter:如何对每个新闻进行反击 [英] Codeigniter: how to do hit counter for each news

查看:63
本文介绍了Codeigniter:如何对每个新闻进行反击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,在codeigniter中做广告出售二手车。我想按控制器中的方法为每个新闻做计数器。当用户单击新闻时,此新闻的模式将打开,并且应在数据库的hitCounter列中添加1。我尝试了许多代码和方法来执行此操作,但是它不起作用。如果有人请建议在这种情况下该怎么办。

I'm developing website which advertises used cars for sell in codeigniter. I want to do hit counter for each news by method in controller. When user clicks to news the modal of this news opens and it should add 1 to my hitCounter column in database. I tried many codes and ways to do this but it is not working. If someone please can suggest what to do in this case. Thanks in advance!

此处是我控制器的方法代码:

Here my controller's method code:

                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;



    }


推荐答案

步骤1:调用控制器函数onClick

Step 1: Call controller function onClick

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

步骤2:在Controller中定义相应的函数并添加更新查询。

Step 2: In Controller define respective function and add update query..


您必须更新表中的计数,因为由于有多个用户正在使用网站,实时计数器无法存储在会话中

you have to update count in table because in real-time counter can't be stored in session as there are multiple users are using website

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

now add table named as tbl_count and apply below query

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

这篇关于Codeigniter:如何对每个新闻进行反击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆