Image Hits Counter With Database Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Image Hits Counter With Database Help

    hi i found this hits counter that uses a .txt file.<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?

    //
    // SET VARIABLES
    //

    // filename of hits count
    $count_filename = "counter.txt";

    // minimum number of digits to display
    $min_digits = 0; // set to 0 to display hits count as is

    // location of digit images
    $digits_location = "images/";

    // dimensions of digit images in pixels
    $digit_width = 25;
    $digit_height = 25;


    //
    // INCREMENT HITS COUNT
    //

    // open count file for reading only
    $count_file = fopen($count_filename, "r");

    // get current hits count
    $hits_count = fgets($count_file, filesize($count_filename) + 1);

    // increment hits count by one
    $hits_count++;

    // close count file
    fclose($count_file);

    // open count file for writing only
    $count_file = fopen($count_filename, "w");

    // write new hits count to count file
    fwrite($count_file, $hits_count);

    // close count file
    fclose($count_file);


    //
    // OUTPUT HITS COUNT AS A JPEG IMAGE
    //

    // get number of digits in hits count
    $no_digits = strlen($hits_count);

    // use a minimum number of digits to display hits count if necessary
    if ($no_digits < $min_digits) {
    // get number of zeroes to append to hits count
    $no_zeroes = $min_digits - $no_digits;

    // append zeroes to hits count
    for ($i = 0; $i < $no_zeroes; $i++) {
    $hits_count = "0" . $hits_count;
    }

    // get new number of digits in hits count
    $no_digits = $min_digits;
    }

    // send headers for JPEG image
    header("Content-type: image/jpeg");

    // create hits count image
    $count_image = imagecreate($digit_width * $no_digits, $digit_height);

    // add digit images to hits count image
    for ($i = 0; $i < $no_digits; $i++) {
    // get digit in this part of hits count
    $digit = substr($hits_count, $i, 1);

    // get image for this digit
    $digit_image = imagecreatefromjpeg($digits_location . $digit . ".jpg");

    // get x-coordinate for placing this digit in count image
    $x = $digit_width * $i;

    // place digit image within count image
    imagecopymerge($count_image, $digit_image, $x, 0, 0, 0, $digit_width, $digit_height, 100);
    }

    // output hits count image
    imagejpeg($count_image);

    ?></div> i had a look at the code n thought it would be pretty easy (even tho im only a beginner lol) to convert it to use mysql database instead.. but it isnt workin, this is what iv done <div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'><?


    //Set database to counter

    $db="db-name";

    //connect to server and database


    $link = mysql_connect("localhost", "user", "pw");

    if (! $link) die("Cannot connect to MySQL");

    mysql_select_db($db , $link) or die("Cannot open $db: ".mysql_error());


    //
    // SET VARIABLES
    //


    // minimum number of digits to display
    $min_digits = 0; // set to 0 to display hits count as is

    // location of digit images
    $digits_location = "images/";

    // dimensions of digit images in pixels
    $digit_width = 25;
    $digit_height = 25;


    //
    // INCREMENT HITS COUNT
    //

    //Increment counter

    mysql_query("UPDATE counter SET hits=hits+1 WHERE id=&#39;1&#39;");

    //extract count from database table

    $hits_count = mysql_query("SELECT hits FROM counter WHERE id=&#39;1&#39;");




    //
    // OUTPUT HITS COUNT AS A JPEG IMAGE
    //

    // get number of digits in hits count
    $no_digits = strlen($hits_count[0]);

    // use a minimum number of digits to display hits count if necessary
    if ($no_digits < $min_digits) {
    // get number of zeroes to append to hits count
    $no_zeroes = $min_digits - $no_digits;

    // append zeroes to hits count
    for ($i = 0; $i < $no_zeroes; $i++) {
    $hits_count[0] = "0" . $hits_count[0];
    }

    // get new number of digits in hits count
    $no_digits = $min_digits;
    }

    // send headers for JPEG image
    header("Content-type: image/jpeg");

    // create hits count image
    $count_image = imagecreate($digit_width * $no_digits, $digit_height);

    // add digit images to hits count image
    for ($i = 0; $i < $no_digits; $i++) {
    // get digit in this part of hits count
    $digit = substr($hits_count[0], $i, 1);

    // get image for this digit
    $digit_image = imagecreatefromjpeg($digits_location . $digit . ".jpg");

    // get x-coordinate for placing this digit in count image
    $x = $digit_width * $i;

    // place digit image within count image
    imagecopymerge($count_image, $digit_image, $x, 0, 0, 0, $digit_width, $digit_height, 100);
    }

    // output hits count image
    imagejpeg($count_image);

    ?></div> it just shows a broken image.. can any1 help please? iv created the table in the database with id & hits and iv added id as 1 and hits as 0..

    #2
    mention image location 1st here
    Code:
    $digits_location = "images/";
    its really unnecessary to use both flat file and sql db at the same time,this both are complementary to one another. I used only mysql db in my hitcounter script

    Comment


      #3
      im not trying to use both.. i just want to use mysql, the 1st code above usin the txt file works so it cant be anything to do with the digits location "images/" cuz it shows a jpeg image using the images in the images/ folder (0-9).. i kept that bit of code the same in the 2nd code above.. i changed the code that writes the hits incrementation (if thats a word) to the txt file<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>// INCREMENT HITS COUNT
      //

      // open count file for reading only
      $count_file = fopen($count_filename, "r");

      // get current hits count
      $hits_count = fgets($count_file, filesize($count_filename) + 1);

      // increment hits count by one
      $hits_count++;

      // close count file
      fclose($count_file);

      // open count file for writing only
      $count_file = fopen($count_filename, "w");

      // write new hits count to count file
      fwrite($count_file, $hits_count);

      // close count file
      fclose($count_file);</div>, so it writes the hits to the datbase instead.. <div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>//Increment counter

      mysql_query("UPDATE counter SET hits=hits+1 WHERE id=&#39;1&#39;");

      //extract count from database table

      $hits_count = mysql_query("SELECT hits FROM counter WHERE id=&#39;1&#39;");</div> so if thats right, this bit of the code<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>// get number of digits in hits count
      $no_digits = strlen($hits_count[0]);

      // use a minimum number of digits to display hits count if necessary
      if ($no_digits < $min_digits) {
      // get number of zeroes to append to hits count
      $no_zeroes = $min_digits - $no_digits;

      // append zeroes to hits count
      for ($i = 0; $i < $no_zeroes; $i++) {
      $hits_count[0] = "0" . $hits_count[0];
      }

      // get new number of digits in hits count
      $no_digits = $min_digits;
      }

      // send headers for JPEG image
      header("Content-type: image/jpeg");

      // create hits count image
      $count_image = imagecreate($digit_width * $no_digits, $digit_height);

      // add digit images to hits count image
      for ($i = 0; $i < $no_digits; $i++) {
      // get digit in this part of hits count
      $digit = substr($hits_count[0], $i, 1);

      // get image for this digit
      $digit_image = imagecreatefromjpeg($digits_location . $digit . ".jpg");

      // get x-coordinate for placing this digit in count image
      $x = $digit_width * $i;

      // place digit image within count image
      imagecopymerge($count_image, $digit_image, $x, 0, 0, 0, $digit_width, $digit_height, 100);
      }

      // output hits count image
      imagejpeg($count_image);

      ?></div> should in theory output the image using the number (of hits) thats stored in the database, in the same way as it does when it outputs the image using the number thats writen in the .txt file?? but it doesnt

      Comment

      Working...
      X