Skip to main content
Inspiring
January 29, 2013
Answered

PHP/MySQL Page counter

  • January 29, 2013
  • 1 reply
  • 1172 views

I know, it's an often asked question, but I think I have a different requirement to most samples that I have found out there on the net, so I thought I'd ask for some advice here.

I am looking to create a page counter that is stored within the MySQL database as part of the record that has been viewed. and I am not quite sure as to how to go about this.

I know I need a column in my table that stores the amount of times a record has been viewed, but I can't figure out how I should firstly trigger how a record has been viewed, and then I can't figure out how to increment it by one each time it has been viewed.

I don't really want to have a separate table for the counter as I intend to use the info elsewhere where it is pulling the whole record info onto a page.

Any thoughts/tuts/examples/samples are welcome.

Many thanks.

This topic has been closed for replies.
Correct answer bregent

Just found this on tinternet...

mysql_query("UPDATE counter SET counter = counter + 1");

I assume 'counter' is the name of the column with the DB table, and that as soon as the page is loaded it will automatically update the appropriate record. Is that right?

Thanks.


Yes, that is correct.

1 reply

Inspiring
January 31, 2013

Doesn't anybody have any idea of how I might attack this?

How do I submit to the database that a record has been viewed and increment it by 1?

If this was a form, I am sure I could do it as I am able to insert and update records within a DB, but as there is no form involved in this, I have no idea how to attack it and make it count from 0 to 1 to 2 and so on...

Thanks

Participating Frequently
February 1, 2013

If someone is viewing a record then there would be a related SELECT statement. Right after the SELECT statement, just issue an UPDATE statement against the same record that increments the field by 1.

Inspiring
February 1, 2013

OK. I understand the concept behind that, and I can see it would work for me.

How do I trigger the update statement though? I'm only aware of being able to update a record by clicking a form button.


Just found this on tinternet...

mysql_query("UPDATE counter SET counter = counter + 1");

I assume 'counter' is the name of the column with the DB table, and that as soon as the page is loaded it will automatically update the appropriate record. Is that right?

Thanks.