hi,
after asking a question in a different forum, i have established that i need to use a server-side language to do what i want to do on my webpage.
ill be honest – im a graphic design student with knowledge of html only and dreamweaver. I need to do this for one project only too so I need to know if its worth it or whether i need to teach myself a whole new script, in which case Ill do it the (not quite so effective) easy way!
I am creating a single page which needs to fade a little every time a visitor comes to the site. I want it to fade to grey over 1000 hits. I now know that I’d need to have a database somewhere holding stastistics about traffic, which is queried in order to determine the opacity value of the page. But I have no idea how to do this. at all! is it REALLY complicated??
any help would be great!
thanks, Kirsty
[code=php]<?php
phpinfo();
?>[/code]
yep it works when i change the end to .php
http://www.kirstyhole.com/Untitled-1.php [/QUOTE]
[code=php]<?php
// Make a MySQL Connection
mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error());
mysql_select_db("DATABASENAME") or die(mysql_error());
// Create a MySQL table in the selected database
mysql_query("CREATE TABLE counter(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(pageid),
countvalue int(11)")
or die(mysql_error());
echo "Table Created!";
?>
Are you on hosting with a control panel? You might be able to create the table from there. Use something like this. Let us know if you change anything from the suggestion or if there is a problem.
Table name - counter
column 1 - pageid autoincrementid primary key (and unique if option exists)
column 2 - countvalue int(11)
[/code]
[code=php]
<?php
if(!file_exists('counter.dat')) {
touch('counter.dat');
}
$counter = file_get_contents('counter.dat');
if(!is_numeric($counter)) {
$counter = 1;
} else {
$counter++;
}
file_put_contents('counter.dat', $counter);
?>
[/code]
[code=php]
<?php echo $counter; ?>
[/code]
[code=php]
<?php
//The functions can go anywhere on the page so I've put them first for clarity of code
function increment_counter($filename) {
//this function is called every page load. It will increment the value in the counter file one.
$file_handle = fopen($filename,'r+');//open file for reading and writing r+
$num = fread($file_handle, filesize($filename));//file read to return the number to a variable
if($num<1000)$num++;//shout cut to increment a numerical value by one if <1000
fwrite($file_handle,$num);//write new number to file
fclose($fp);//close file to free resource
}
function generate_color($filename,$hex){
//this function will lighten the rgb value of a provided hex value start color by the same percentage of 1000 in the num file
$num=file_get_contents($filename);//get the value in the text file
//convert hex to RGB dec
$r=hexdec(substr($hex,0,2)); echo $r;
$g=hexdec(substr($hex,2,2)); echo $g;
$b=hexdec(substr($hex,4,2)); echo $b;
//The number in the text file will be 1000 or less so find the percentage and lighten (increase) all 3 rgb values by that.
$percent=$num/1000*100;
//calculate new rgb
$r_difference=255-$r;
$r_increaseamount=$r_difference/100*$percent;
$new_r=$r+$r_increaseamount;
$g_difference=255-$g;
$g_increaseamount=$g_difference/100*$percent;
$new_g=$g+$g_increaseamount;
$b_difference=255-$b;
$b_increaseamount=$b_difference/100*$percent;
$new_b=$b+$b_increaseamount;
//convert back to hex
$r_hex=dechex($new_r);
$g_hex=dechex($new_g);
$b_hex=dechex($new_b);
$new_hex=$r_hex.$g_hex.$b_hex;
return $new_hex;
}
//You know about absolute and relative links, well paths to files work in the same way, so set the path to the textfile, if you want it anywhere other than in the same directory as the counting script.
$text_file = "num.txt";//the location of the counter
$start_color='ffcc11';
?>
<div style="background-color:#<?php echo $start_color?>">Start color</div>
<?
$new_color=generate_color($text_file,$start_color);
?>
<div style="background-color:#<?php echo $new_color?>"> lighter by percentage</div>
[/code]
[code=php]background-color: #<?php echo generate_color($text_file,$start_color); ?>[/code]
[code=php]<?php
increment_counter($text_file);
?>[/code]
[code=php]<?
function increment_counter($filename) {
//this function is called every page load. It will increment the value in the counter file one.
$file_handle = fopen($filename,'r+');//open file for reading and writing r+
$num = fread($file_handle, filesize($filename));//file read to return the number to a variable
if($num<1000)$num++;//shout cut to increment a numerical value by one if <1000
fwrite($file_handle,$num);//write new number to file
fclose($fp);//close file to free resource
}
function generate_color($filename,$hex){
//this function will lighten the rgb value of a provided hex value start color by the same percentage of 1000 in the num file
$num=file_get_contents($filename);//get the value in the text file
//convert hex to RGB dec
$r=hexdec(substr($hex,0,2));
$g=hexdec(substr($hex,2,2));
$b=hexdec(substr($hex,4,2));
//The number in the text file will be 1000 or less so find the percentage and lighten (increase) all 3 rgb values by that.
$percent=$num/1000*100;
//calculate new rgb
$r_difference=255-$r;
$r_increaseamount=$r_difference/100*$percent;
$new_r=$r+$r_increaseamount;
$g_difference=255-$g;
$g_increaseamount=$g_difference/100*$percent;
$new_g=$g+$g_increaseamount;
$b_difference=255-$b;
$b_increaseamount=$b_difference/100*$percent;
$new_b=$b+$b_increaseamount;
//convert back to hex
$r_hex=dechex($new_r);
$g_hex=dechex($new_g);
$b_hex=dechex($new_b);
$new_hex=$r_hex.$g_hex.$b_hex;
return $new_hex;
}
//You know about absolute and relative links, well paths to files work in the same way, so set the path to the textfile, if you want it anywhere other than in the same directory as the counting script.
$text_file = "num.txt";//the location of the counter
increment_counter($text_file); //Edit: call this function before the other one so it creates the file.
$start_color='ffcc11';
?>
<div style="background-color:#<?php echo $start_color?>">Start color</div>
<?
$new_color=generate_color($text_file,$start_color);
?>
<div style="background-color:#<?php echo $new_color?>"> lighter by percentage</div>
[/code]
$file_handle = fopen($num.txt,'r+');
$num = fread($file_handle, filesize($num.txt));
if($num<1000)$num++;
fwrite($file_handle,$num);
fclose($fp);
$num=file_get_contents($num.txt);
$r=hexdec(substr($hex,0,2));
$g=hexdec(substr($hex,2,2));
$b=hexdec(substr($hex,4,2));
$percent=$num/1000*100;
$r_difference=255-$r;
$r_increaseamount=$r_difference/100*$percent;
$new_r=$r+$r_increaseamount;
$g_difference=255-$g;
$g_increaseamount=$g_difference/100*$percent;
$new_g=$g+$g_increaseamount;
$b_difference=255-$b;
$b_increaseamount=$b_difference/100*$percent;
$new_b=$b+$b_increaseamount;
$r_hex=dechex($new_r);
$g_hex=dechex($new_g);
$b_hex=dechex($new_b);
$new_hex=$r_hex.$g_hex.$b_hex;
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>What's mine is yours and what's yours is mine</title>
<style type="text/css">
<!--
body {
margin-left: 50px;
background-color: #FFFFFF;
}
body,td,th {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #000000;
line-height: 0.3;
}
a:link {
color: #000000;
}
a:visited {
color: #333333;
}
a:hover {
color: #000000;
}
a:active {
color: #333333;
}
.style1 {
color: #16B200
}
.style2 {
color: #009933
}
.style3 {
color: #00CC00
}
.style4 {
color: #16b200
}
-->
</style></head>
<body>
<?
function increment_counter($num.txt) {
$file_handle = fopen($num.txt,'r+');
$num = fread($file_handle, filesize($num.txt));
if($num<1000)$num++;
fwrite($file_handle,$num);
fclose($fp);
}
function generate_color($num.txt,$hex){
$num=file_get_contents($num.txt);
$r=hexdec(substr($hex,0,2));
$g=hexdec(substr($hex,2,2));
$b=hexdec(substr($hex,4,2));
$percent=$num/1000*100;
$r_difference=255-$r;
$r_increaseamount=$r_difference/100*$percent;
$new_r=$r+$r_increaseamount;
$g_difference=255-$g;
$g_increaseamount=$g_difference/100*$percent;
$new_g=$g+$g_increaseamount;
$b_difference=255-$b;
$b_increaseamount=$b_difference/100*$percent;
$new_b=$b+$b_increaseamount;
$r_hex=dechex($new_r);
$g_hex=dechex($new_g);
$b_hex=dechex($new_b);
$new_hex=$r_hex.$g_hex.$b_hex;
return $new_hex;
}
increment_counter($num.txt);
$text_file = "num.txt";
$start_color='000000';
// -----> Did you forget to close the PHP tag?
?>
<p> </p>
<p> </p>
<p> </p>
<p align="left"> </p>
<p><img src="wyimtext.jpg" width="692" height="160" /></p>
<p> </p>
<p><img src="thisrepresents.jpg" width="692" height="100" /></p>
<p class="style4"> </p>
<p class="style4"> </p>
<p class="style4">The nature of the communication design industry</p>
<p class="style1"> today is so competitive that designers fall back</p>
<p class="style1"> onto stylistic trends that guarantee success, for</p>
<p class="style1"> fear of failing to impress the client if they dare </p>
<p class="style1">to produce something new and innovative.</p>
<p> </p>
<p class="style2">The internet can be a very useful tool for providing</p>
<p class="style2">inspiration and attracting interest to your work.</p>
<p class="style2"> However when it is misused and ideas are borrowed, </p>
<p class="style2">these ideas become worn out and overused, leading</p>
<p class="style2">to design that is all very 'run-of-the-mill'.</p>
<p> </p>
<p> </p>
<p><a href="mailto:[email protected]">Email</a></p>
</body>
</html>
[/code]
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>What's mine is yours and what's yours is mine</title>
<style type="text/css">
<!--
body {
margin-left: 50px;
background-color: #FFFFFF;
}
body,td,th {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #000000;
line-height: 0.3;
}
a:link {
color: #000000;
}
a:visited {
color: #333333;
}
a:hover {
color: #000000;
}
a:active {
color: #333333;
}
.style1 {
color: #16B200
}
.style2 {
color: #009933
}
.style3 {
color: #00CC00
}
.style4 {
color: #16b200
}
-->
</style></head>
<body>
<?
function increment_counter($num.".txt") {
$file_handle = fopen($num.".txt",'r+');
$num = fread($file_handle, filesize($num.".txt"));
if($num<1000)$num++;
fwrite($file_handle,$num);
fclose($fp);
}
function generate_color($num.".txt",$hex){
$num=file_get_contents($num.".txt");
$r=hexdec(substr($hex,0,2));
$g=hexdec(substr($hex,2,2));
$b=hexdec(substr($hex,4,2));
$percent=$num/1000*100;
$r_difference=255-$r;
$r_increaseamount=$r_difference/100*$percent;
$new_r=$r+$r_increaseamount;
$g_difference=255-$g;
$g_increaseamount=$g_difference/100*$percent;
$new_g=$g+$g_increaseamount;
$b_difference=255-$b;
$b_increaseamount=$b_difference/100*$percent;
$new_b=$b+$b_increaseamount;
$r_hex=dechex($new_r);
$g_hex=dechex($new_g);
$b_hex=dechex($new_b);
$new_hex=$r_hex.$g_hex.$b_hex;
return $new_hex;
}
increment_counter($num.".txt");
$text_file = "num.txt";
$start_color='000000';
?>
<p> </p>
<p> </p>
<p> </p>
<p align="left"> </p>
<p><img src="wyimtext.jpg" width="692" height="160" /></p>
<p> </p>
<p><img src="thisrepresents.jpg" width="692" height="100" /></p>
<p class="style4"> </p>
<p class="style4"> </p>
<p class="style4">The nature of the communication design industry</p>
<p class="style1"> today is so competitive that designers fall back</p>
<p class="style1"> onto stylistic trends that guarantee success, for</p>
<p class="style1"> fear of failing to impress the client if they dare </p>
<p class="style1">to produce something new and innovative.</p>
<p> </p>
<p class="style2">The internet can be a very useful tool for providing</p>
<p class="style2">inspiration and attracting interest to your work.</p>
<p class="style2"> However when it is misused and ideas are borrowed, </p>
<p class="style2">these ideas become worn out and overused, leading</p>
<p class="style2">to design that is all very 'run-of-the-mill'.</p>
<p> </p>
<p> </p>
<p><a href="mailto:[email protected]">Email</a></p>
</body>
</html>
[/code]
[code=php]
function my_function($foo){
echo $foo;
}[/code]
[code=php]my_function('hello');[/code]
[code=php]
$bar='hello';
my_function($bar);
[/code]
[code=php]
$bar='Goodbye ';
$var1='cruel ';
$var2='world ';
my_function($bar);
my_function($var1);
my_function($var2);
[/code]
Goodbye cruel world
[code=php]
$bar='Goodbye ';
$var1='cruel ';
$var2='world ';
my_function($bar.$var1.$var2);
//or
my_function($bar.'cruel '.$var2);[/code]
[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>What's mine is yours and what's yours is mine</title>
<style type="text/css">
<!--
body {
margin-left: 50px;
background-color: #FFFFFF;
}
body,td,th {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #000000;
line-height: 0.3;
}
a:link {
color: #000000;
}
a:visited {
color: #333333;
}
a:hover {
color: #000000;
}
a:active {
color: #333333;
}
.style4 {color: #16b200}
.style5 {color: #000000}
-->
</style></head>
<body>
<?php
$text_file = "num.txt";
function increment_counter($file) {
$fp = fopen($file,rw);
$count = fgets($fp,9999);
fclose($fp);
$fp = fopen($file,w);
$count += 1;
print "$count ";
fputs($fp, $count);
fclose($fp);
}
function generate_color($filename,$hex){
//this function will lighten the rgb value of a provided hex value start color by the same percentage of 1000 in the num file
$num=file_get_contents($filename);//get the value in the text file
//convert hex to RGB dec
$r=hexdec(substr($hex,0,2));
$g=hexdec(substr($hex,2,2));
$b=hexdec(substr($hex,4,2));
//The number in the text file will be 1000 or less so find the percentage and lighten (increase) all 3 rgb values by that.
$percent=$num/100*100;
//calculate new rgb
$r_difference=255-$r;
$r_increaseamount=$r_difference/100*$percent;
$new_r=$r+$r_increaseamount;
$g_difference=255-$g;
$g_increaseamount=$g_difference/100*$percent;
$new_g=$g+$g_increaseamount;
$b_difference=255-$b;
$b_increaseamount=$b_difference/100*$percent;
$new_b=$b+$b_increaseamount;
//convert back to hex
$r_hex=dechex($new_r);
$g_hex=dechex($new_g);
$b_hex=dechex($new_b);
$new_hex=$r_hex.$g_hex.$b_hex;
return $new_hex;
}
$start_color='#000000';
$new_color=generate_color($text_file,$start_color);
?>
<div style="font-color:#<?php echo $new_color?>">
<p> </p>
<p> </p>
<p> </p>
<p align="left"> </p>
<p><img src="wyimtext.jpg" width="692" height="160" /></p>
<p> </p>
<p><img src="thisrepresents.jpg" width="692" height="100" /></p>
<p class="style4"> </p>
<p class="style4"> </p>
<p class="style5">The nature of the communication design industry</p>
<p class="style5"> today is so competitive that designers fall back</p>
<p class="style5"> onto stylistic trends that guarantee success, for</p>
<p class="style5"> fear of failing to impress the client if they dare </p>
<p class="style5">to produce something new and innovative.</p>
<p class="style5"> </p>
<p class="style5">The internet can be a very useful tool for providing</p>
<p class="style5">inspiration and attracting interest to your work.</p>
<p class="style5"> However when it is misused and ideas are borrowed, </p>
<p class="style5">these ideas become worn out and overused, leading</p>
<p class="style5">to design that is all very 'run-of-the-mill'.</p>
<p> </p>
<p>
<? echo 'people have viewed this page.' . increment_counter($text_file);
?>
</p>
<p> </p>
<p><a href="mailto:[email protected]">Email</a></p>
</div>
</body>
</html>[/code]
[code=php]$percent=$num/100*100;
//should be
$percent=$num/1000*100;[/code]
[code=php]$start_color='#000000'
//should be
$start_color='000000'[/code]
[code=html]<div style="font-color:#<?php echo $new_color?>">
should be
<div style="color:#<?php echo $new_color?>">
[/code]
[code=php]$start_color='000000';
$start_color2='61B601';
$new_color=generate_color($text_file,$start_color);
$new_color2=generate_color($text_file,$start_color2);
?>
</p>
<p> </p>
<div style="color:#<?php echo $new_color?>". #<?php echo $new_color2?>>
[/code]
[code=php]<?php
//this creates a jpeg from a jpeg
function calculate_percent($filename){
$num=file_get_contents($filename);//get the value in the text file
$percent=$num/1000*100;
return $percent;
}
function masked_image($filename,$imgfile,$hex){
//calculate percentage
$percent=calculate_percent($filename);
$file_details=getimagesize($imgfile);
$width=$file_details[0];
$height=$file_details[1];
//convert hex to RGB dec
$r=hexdec(substr($hex,0,2));
$g=hexdec(substr($hex,2,2));
$b=hexdec(substr($hex,4,2));
//Create mask image
$stamp = imagecreatetruecolor($width, $height);
$maskcolor = imagecolorallocate($stamp, $r, $g, $b);
imagefilledrectangle($stamp, 0,0, $width, $height, $maskcolor);
$im = imagecreatefromjpeg($imgfile);
// Merge mask with original
imagecopymerge($im, $stamp, 0, 0, 0, 0,$width, $height, $percent);
// Save the image to file and free memory
imagepng($im);
imagedestroy($im);
}
$my_image= 'bar_thumb.jpg';//This function only handles jpgs, It could handle other image types but not as it is.
$text_file = "num.txt";
$bgcolor='ffffff';//no '#'.
masked_image($text_file,$my_image,$bgcolor);
?>[/code]
0.1.9 — BETA 6.2