uzphpcoder
uzphpcoder

Reputation: 21

Codeigniter: how to do hit counter for each news

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);
    }

Here my views code:

            <?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; ?>

Here my model code:

                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;



    }

Upvotes: 1

Views: 3302

Answers (2)

Kunal
Kunal

Reputation: 604

Step 1: Call controller function onClick

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

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

now add table named as tbl_count and apply below query

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

Upvotes: 1

M Shahzad Khan
M Shahzad Khan

Reputation: 935

Your model function should be something like this

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

Upvotes: 0

Related Questions