0

How to scale PHP voting system for multiple posts?

I have a working PHP Ajax voting system that writes the likes from a blog post into a .txt file, and I want to scale it for multiple posts and record the likes from each of those.

I have tried to change the "onclick" value, but it seems that the script I am using limits me. I have done extensive research on Stackoverflow and other platforms, and tried many implementations. Please, can you assist with my below code, and point me to the right direction?

HTML

<span id="like"><a href="javascript:" name="vote"
value="0" onclick="getVote(this.value)">Like</a></span>

JAVASCRIPT

function getVote(int){
    if(window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest()
    }else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
   xmlhttp.onreadystatechange=function({
        if(this.readyState==4&&this.status==200{
            document.getElementById("like").innerHTML=this.responseText
        }
   };
    xmlhttp.open("GET","vote.php?vote="+int,true);
    xmlhttp.send()
}

PHP

<?php 
$vote=$_REQUEST['vote'];
$filename="votes.txt";
$content=file($filename);
$array=explode("-",$content[0]);
$yes=$array[0];
if($vote==0){
    $yes=$yes+1;
}
$insertvote=$yes;
$fp=fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

Fiddle

NDi
  • 184
  • 1
  • 2
  • 17
  • if you want it to scale, stop using a text file, start using a database. –  Aug 16 '19 at 00:34
  • I appreciate your comment tim. I am in the process of educating myself regarding databases, but at the same time i wish to see if it can be done with an alternative way. – NDi Aug 16 '19 at 00:42
  • PLEASE format that – seven_seas Aug 16 '19 at 01:45

1 Answers1

1

There are 2 Problems here:
1. you cannot name your vaiable int this is a reserved keyword so rename it to x
2. you're js-fiddle is set to onload. this will wrap your javascript in a function. In your fiddle go to the Javascript + no library (pure js) selector then select No-wrap bottom of head in the Load Type options Reserved Javascript-keywords

Now: about scaling.
If you want to scale this and not use a database... you can either have a voting file for every post. then ud just pass the post name to the ajax call and your php script would open the file, read the number, increment it by 1 and close it. OR you could store them in a Single file and parse the content when u need it. so i.E ud have a file that consists of <postid>:<votes>,..... and parse this file using explode. However the question of how to scale this is in general Too broad

seven_seas
  • 703
  • 4
  • 21
  • I appreciated your proposals! I changed the variable and the fiddle. Regarding scaling, I honestly do not know (yet) how to code for including the and parse this file, and I am not expecting from you to do that. But, what I would love is to point me somewhere were I can learn. Thank you @seven_seas . – NDi Aug 17 '19 at 01:51
  • Update: It is done. With some essential code changes IT CAN BE scaled massively for infinite of posts, plus using a .txt file will help with performance. It is incredible. – NDi Aug 18 '19 at 23:20