Image Hits Counter With Database Help

Started on the old forum in August 2008 · view the original in the archive · old links redirect here

2 replies · 754 views · started 18 years ago · 1 min read

2 participants
Phpnewb Newbie · 5 posts #1

hi i found this hits counter that uses a .txt file.CODE<?

// // 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);

?> 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 CODE<?

//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);

?> 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..

Phpnewb Newbie · 5 posts #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 fileCODE// 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);, so it writes the hits to the datbase instead.. CODE//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'"); so if thats right, this bit of the codeCODE// 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);

?> 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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close