Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP/MySQL Page counter

Enthusiast ,
Jan 29, 2013 Jan 29, 2013

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.

TOPICS
Server side applications
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 05, 2013 Feb 05, 2013

Yes, that is correct.

Translate
Enthusiast ,
Jan 31, 2013 Jan 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 31, 2013 Jan 31, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 01, 2013 Feb 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 01, 2013 Feb 01, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 05, 2013 Feb 05, 2013
LATEST

Yes, that is correct.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines