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='1'");
//extract count from database table
$hits_count = mysql_query("SELECT hits FROM counter WHERE id='1'");
//
// 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..
//
// 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='1'");
//extract count from database table
$hits_count = mysql_query("SELECT hits FROM counter WHERE id='1'");
//
// 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..
Comment