There is a site where articles are output from the database in a loop. Something like:

<?php foreach($article as $item):?> <div class="nazvanie_statii"><?=$item['title'];></div> <div class="kol_prosmotrov"><?=$item['view'];></div> <div class="text_statii"><?=$item['text'];></div> <a href="<?=base_url();?>index.php/article/<?=$item['title_en'];?>"/> <?php endforeach;> 

There is also a jQuery function that counts the number of clicks on a block or link with the class .myObj and displays them in a block with the identifier- # counter.

 <script type="text/javascript" language="javascript"> $(document).ready(function() { $('#myObj').click(function() { $('#counter').html(+$('#counter').html()+1); }); }); </script> 

In general, this question arose - how to tie it to my link - <a href="<?=base_url();?>index.php/article/<?=$item['title_en'];?>"/> ?

After all, it is dynamic, each time is displayed on the title_en-title of the article in the transcription. Tell me, please, click on the idea. Thank you in advance.

No one has any ideas? Like the idea should not be difficult to implement ... Or is it generally impossible on the fly ... only by adding the number of clicks in the database and the subsequent output from the database in the <div class="kol_prosmotrov"><?=$item['view'];></div>

I realized that I was writing nonsense, because after reloading the page the counter will reset. Then it turns out only through?

  • What kind of people for limiting the number of input characters for questions?) Are you serious? Give me the opportunity to show the code, I'm trying to write myself) - semjaza

1 answer 1

  1. You need to create a label in which to store the number of clicks, according to your criteria and record the result.
  2. Block or link assign id , for example id="counter_<?=$item['id']?>" name="<?=$item['id']?>" , On which you will catch them.
  3. In javascript :

     $('[id^=counter_]')click(function() { var id = this.name; $.post("/ajx/update_couter", { id:id }).done(function(data) { $(this).html(+$(this).html()+1); }); }); 
  4. /ajx/update_couter on the server side, take the id your article and add +1 to the database.

  5. And in the foreach you output it or in some other way.