BuildMyWeb Posted February 28, 2015 Share Posted February 28, 2015 let's say i have two buttons on a webpage and i want to count how many times each is clicked by viewers over a term of a year. obviously i can update a database but what are some simpler solutions? more of a creative-thinking exercise than anything i suppose. ------------------------------------------- what about a simple file_put_contents(); that reads the current value in the file, increments it, and then overwrites the current val with the new? would the value be read as a type (int) and not text? ------------------------------------------- what else could we do? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 28, 2015 Share Posted February 28, 2015 That single line on its own will not do what you say it does. You need to read the content into variable, increment it put the content back. It will be text but php's automatic type conversion will allow you to process it as int. What's wrong with a db table? All you need then is UPDATE mycounts SET count=count+1 WHERE button = 'A' Quote Link to comment Share on other sites More sharing options...
BuildMyWeb Posted February 28, 2015 Author Share Posted February 28, 2015 i understand that about the file writing barand. nothing wrong with doing it in a db. like i said, this was more a creative exercise for me. just wondering whatother creative solutions one might come up with. Quote Link to comment Share on other sites More sharing options...
Solution rwhite35 Posted February 28, 2015 Solution Share Posted February 28, 2015 Since the web is "stateless", you really only have two choices. database or flat file. I prefer writing data to XML(flat file), specially when the public has the ability to add comments... Usually for blog post and other long form text. And save discrete data to a database. That being said, your XML could store the count as Barand suggest (retrieve/tally/rewrite) or as new element in the XML. The XML might then look like: //pseudo XML code <tally> <count id="1"> <click>1</click> <button>red</button> <date>2015-02-05</date> </count> <count id="2"> <click>1</click> <button>blue</button> <date>2015-02-10</date> </count> //continues on... <count id="15"> <click>1</click> <button>red</button> <date>2015-02-28</date> </count> </tally> It would be a simple matter of tallying the ticks from each <count><click> element. That's an alternative approach since this is a thought experiment. Quote Link to comment Share on other sites More sharing options...
BuildMyWeb Posted February 28, 2015 Author Share Posted February 28, 2015 thx all Quote Link to comment Share on other sites More sharing options...
kicken Posted March 1, 2015 Share Posted March 1, 2015 Note that if you use a file approach you will need to take care to ensure you lock the file properly before reading/updating the content. Not doing this could result in a corrupt file due to multiple people hitting it at the same time. If you use a database this problem is taken care of for you, provided to do it right and use a single UPDATE like Barand demonstrated rather than a SELECT first then update later. There are plenty of ways you could track a counter. What you choose to use would depend mostly on what you need and whether you're already using a particular tech. If you're already using a database for your site, then using that for your counter as well would make sense. If you're not using database already, then a file of some type would work fine. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.