PDA

Click to See Complete Forum and Search --> : Addimg Images to a database + Hit counter


abstrait
Apr 8th, 2006, 02:59 AM
I want to add images to a database and create a hit counter for each image. Is it possible to even record how many times one image has been viewed? Please give me ideas on how to complete this task. Thanks!

john tindell
Apr 8th, 2006, 06:55 AM
How are you storing the image? Are you storing the actual image of are you storing a link to the image (eg /path/to/image.png)?

Well which ever way you choose to do it all you need to do is add an extra column to your table which holds the number of times the image has been viewed.

penagate
Apr 8th, 2006, 10:28 AM
You need to be viewing the image on a PHP page, or link to a PHP script that outputs the image just like a static image. Then you can incerement your hit counter in that script.

If you are using a database you can do something like this to increment the hit count

// once connected to the DB, obviously

// $_GET['id'] holds the image ID in the database

$count = @mysql_result(@mysql_query(
'select distinct `hit_count` from `images` where '.
'`image_id` = '.(int)$_GET['id']
), 0);

if (!mysql_errno()) {
// the image exists, update the hit count:
mysql_query(
'update `images` set `hit_count` = '.$count++.
' where `image_id` = '.(int)$_GET['id']
);
}
else {
// throw an error, or whatever
}


Note that the @ symbol prepended to a statement suppresses error messages for that statement. It's useful if you have an alternate method of checking for errors, such as mysql_errno() in this case (to handle invalid image IDs), but you should use proper error handling methods otherwise.