Jump to content

Auto Increment Data Storage


Go to solution Solved by rwhite35,

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/294967-auto-increment-data-storage/
Share on other sites

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'
  • Solution

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.