Results 1 to 3 of 3

Thread: Addimg Images to a database + Hit counter

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    143

    Addimg Images to a database + Hit counter

    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!

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Addimg Images to a database + Hit counter

    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.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Addimg Images to a database + Hit counter

    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

    PHP Code:
    // 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.
    Last edited by penagate; Apr 8th, 2006 at 10:34 AM. Reason: Stupid scrolling bugs in F

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width