counter in MySQL db

Busy's picture

He has: 6,151 posts

Joined: May 2001

is it possible to have a MySQL database count how many hits (and store them in the db) a certain ID is displayed ?

say I have a database of image details, id, name, width, height, description, alt, group .... and on the page would display the description to link to the rest of the details (image itself with width, height ...). I only want the count to happen when the link/image has been viewed, not the actually description/listing of the image.

would it be best to use two tables?

mairving's picture

They have: 2,256 posts

Joined: Feb 2001

If each page had a unique page_id, you could do it. It would be even easier to count the clicks if someone clicked on a thumbnail image. I have seen this code in gallery, a web based photo display. If you do store it in a database, the database would get to be pretty big after awhile, so that you would probably have to have a script to roll the data into another table.

Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states

Busy's picture

He has: 6,151 posts

Joined: May 2001

Thanks, think I may have to rethink some of the options I want, getting way to involved and messy

They have: 13 posts

Joined: Apr 2002

I don't think it is that hard, but it depends on your whole system. Rather than link directly to the image in a URL, you need to link to a "count and display" script that increments a counter in the database for the image, and THEN display the image. So your link looks like

Visit my portal project -temporarily at http://www.bobcosta.com/bcportal

They have: 157 posts

Joined: Mar 2002

I don't know if this will help but I'll give it a stab.

When a surfer clicks on the thumbnail of an image for instance to get the details, a variable is passed such as http://*.com?detail=view&image_id=image_id to tell the script to parse this block of code to show this information from the database.

Put in the code something like:
if(isset($detail)):
$sql="update thetable set count+1 where image_id='$image_id'";
mysql_query($sql);
endif;

The sql may be wrong but you get the idea. This is if there was a field in the image data called count. Now your count for clicks to view details are incremented.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

SQL:
UPDATE table_name SET field1=field1+1 WHERE field2='key'

They have: 157 posts

Joined: Mar 2002

Thanks Mark.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.