php - Ajax and Site Performance -


i learning php , ajax while creating plugin wordpress, managed finish up. counts number of clicks , impressions banner on sites gets. u place banners through plugin etc...anyway finished adding number of impressions each banner gets. works without problems. did on own , not tutorial wondering right way it:

$(window).load(function() {      $("a[count]").each(function(){         var id = $(this).attr("count");         var data = {             action: 'impressions_count',             postid: id         };         $.post(myajax.ajaxurl,              data,             function(response) {                 console.log( response);         });     }) }); 

and here 1 part of code updates db

function impressions_count_callback() {         global $wpdb;         $post_id = $_post['postid'];         //print_r($post_id);         $post_id = mysql_real_escape_string($post_id);         $wpdb->query("update ". $wpdb->prefix ."cb_ads_manager set impressions = impressions+1 id = '$post_id'");     } 

as u can see sending ajax request each baner on site, if have 5 baners thats 5 requests. make difference if sent in 1 request, have foreach loop in db query again 5 db queries unless possible in 1 go.

p.s. if working within wp, believe more of php/ajax question thats why asked here.

you make 1 ajax request , 1 query db :

$(window).load(function() {     var ids = new array();     $("a[count]").each(function(){         ids.push($(this).attr("count"));     };     if(ids.length > 0) {        var data = {           action: 'impressions_count',           postids: json.stringify(ids);        };        $.post(myajax.ajaxurl, data,            function(response) {              console.log( response);        });     }   });  

and in php

function impressions_count_callback() {         global $wpdb;         $post_id = json_decode($_post['postid']);         //print_r($post_id);         $post_id = implode(',', $post_id);         $post_id = mysql_real_escape_string($post_id);         $wpdb->query("update ". $wpdb->prefix ."cb_ads_manager set impressions = impressions+1 id in ($post_id)"); } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -