Support

Account

Home Forums Feature Requests PHP Ajax Click Counter With ACF

Unread

PHP Ajax Click Counter With ACF

  • hello there in this point where im stuck I know its possible but because of my experience I don’t know how im gonna handle it 🙂 so im adding this future request Ibelive the idea some how important in one point – I mean how many times people click dynamically created specific custom field based links
    right now im using bit.ly I generate every link with that web site so I can track the button activities.. ( I need only click but bitly gives me more than I bargain )
    lets get start it
    Source : Documentation and demos
    what I like to do but I cant do 🙂
    1)
    A) I need dynamically create a name for the .TXT file.
    B) create and save that file to the X location. because 2)
    2) if you checked the demo and examples script works ID based so manually you have to add that ID to the your link also in the counter.txt file
    like Data-id||times_clicked
    link||0
    So I need automate this script to work for me but issue is “EXPERINCE” I don’t have 🙂
    just in case let me copy paste required 3 files here if > one day > some how > some one > with some experience fixed that issue > me my self and rest of the internet can use this useful information – or future with ACF

    Index.php

     
    <style>
    html, body { margin:0; padding:0; font:16px/1.75 Verdana, Arial, Helvetica, sans-serif }
    .page-content { padding:1em; max-width:64em; margin:auto }
    
    .click-count { color:green; font-weight:bold }
    </style>
    <div class="page-content">
    
    <?php 
    // my custom field For ID $uniqueid = get_field('fordigitid');
    // my custom field For Link 1 $linkname_1 = get_field('link1'); 
    // my custom field For Link 2 $linkname_2 = get_field('link2'); 
    // my custom field For Link 3 $linkname_3 = get_field('link3'); 
    	
    $clickcount = explode("\n", file_get_contents('counter.txt')); // this .txt name must be dynamic 
    foreach($clickcount as $line){
    	$tmp = explode('||', $line);
    	$count[trim($tmp[0])] = trim($tmp[1]);
    	}
    
    ?>
    
    <button class="click-trigger" data-click-id="click-001">Click Me</button> <!-- data-click-id have to be $uniqueid+$linkname_1 -->
    Clicked <span id="click-001" class="click-count"><?php echo $count['click-001'];?></span> times.  <!-- Same Here - $uniqueid+$linkname_1 -->
    <br/><br/>
    
    <button class="click-trigger" data-click-id="click-002">Click Me</button>  <!-- data-click-id have to be $uniqueid+$linkname_2 -->
    Clicked <span id="click-002" class="click-count"><?php echo $count['click-002'];?></span> times. <!-- Same Here - $uniqueid+$linkname_2 -->
    <br/><br/>
    
    <button class="click-trigger" data-click-id="click-003">Click Me</button>   <!-- data-click-id have to be $uniqueid+$linkname_3 -->
    Clicked <span id="click-003" class="click-count"><?php echo $count['click-003'];?></span> times. <!-- Same Here - $uniqueid+$linkname_2 -->
    <br/><br/>
    
    </div><!-- closing ".page-content" -->
    
    <script>
    var clicks = document.querySelectorAll('.click-trigger'); // IE8
    for(var i = 0; i < clicks.length; i++){
    	clicks[i].onclick = function(){
    		var id = this.getAttribute('data-click-id');
    		var post = 'id='+id; // post string
    		var req = new XMLHttpRequest();
    		req.open('POST', 'counter.php', true); 
    		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    		req.onreadystatechange = function(){
    			if (req.readyState != 4 || req.status != 200) return; 
    			document.getElementById(id).innerHTML = req.responseText;
    			};
    		req.send(post);
    		}
    	}
    </script>
    

    Counter.php

    
    <?php 
    /* so this is the point !!! I have to automate that */
    /* Notes - add items manually to 'counter.txt' 
               one item per line, in this format;
               id||0
               e.g. click-005||0
               e.g. my-id||0
               e.g. download-01||0
    */
    
    $file = 'counter.txt'; // path to text file that stores counts - Also here this TXT file name must be dynamic so I cant post every single post click stats individually 
    $fh = fopen($file, 'r+');
    $id = $_REQUEST['id']; // posted from page
    $lines = '';
    while(!feof($fh)){
    	$line = explode('||', fgets($fh));
    	$item = trim($line[0]);
    	$num = trim($line[1]);
    	if(!empty($item)){
    		if($item == $id){
    			$num++; // increment count by 1
    			echo $num;
    			}
    		$lines .= "$item||$num\r\n";
    		}
    	} 
    file_put_contents($file, $lines);
    fclose($fh);
    
    ?>	
    

    counter.txt

    
    click-001||36
    click-002||123
    click-003||513
    

    How To show in the post or page

    
    <span id="click-001"><?php echo $count['click-001'];?></span>
    

    I think we all set now thank you for reading and helping me to solve this puzzle 🙂

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.