/    Sign up×
Community /Pin to ProfileBookmark

server-side language that makes my page change depending on number of visitors

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

to post a comment
PHP

25 Comments(s)

Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — Really complicated no, a bit complicated. Yes, but not too bad ?

A database is one way of doing it but you can also use a file to store the number. A database would be the preferred way IMO.

First establish if your host has php. Copy the code below to a blank page and visit the URL through a browser
[code=php]<?php
phpinfo();
?>[/code]


You should see a whole page of information about PHP if you don't then you might not be on a PHP enabled server and you'l need to contact your webhost. To get an account with PHP. PHP is both apache and IIS so any host can provide it, some choose not to.

Let us know what you find. Confirm that the info page displays and one of the sections is titled MySQL (or MSSQL)
Copy linkTweet thisAlerts:
@stirxauthorSep 17.2008 — ahhhh thanks so much for your help!

it displays nothing!

I used dreamweaver and saved it as .html should it be .php?
Copy linkTweet thisAlerts:
@stirxauthorSep 17.2008 — yep it works when i change the end to .php

http://www.kirstyhole.com/Untitled-1.php
Copy linkTweet thisAlerts:
@Phill_PaffordSep 17.2008 — I would suggest looking into something lie a "Tag Cloud". The tag cloud uses clicks/hits on the links that are clicked to increase the font size of the links. Now you could just change the logic a little and instead of increasing the font size you could change the background color and instead of clicks to the links you can capture hits to the page.

Some Examples:

Tag Cloud link #1

#2

Some web Statistics for PHP

Web Statistics

Hit Counter

Hit Counter

Hope this gets you started in the right direction.

Also since your new to PHP you will need to install PHP on your hosting server, if your using your laptop or something you could use XAMPP
Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — yep it works when i change the end to .php

http://www.kirstyhole.com/Untitled-1.php[/QUOTE]

Doh, sorry should have added that. So you have the MySQL bit too?

You'll need the following CAPS info from your host.

Then again save with .php extension and visit in a URL and you should create the MySQL table with a single column. Copy any and all errors/messages here so we can read them.

[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 based on this tutorial

http://www.tizag.com/mysqlTutorial/mysqltables.php

Tizag is an excellent site for mini tutorials.
Copy linkTweet thisAlerts:
@stirxauthorSep 17.2008 — I thought i had mysql because it appeared in that php info page but according to my host admin page, i dont have it on my package. does that mean i cant do this? could i make my own text file that holds hits as given by phil pafford above and use some kind of script that queries that instead? ?
Copy linkTweet thisAlerts:
@kattenSep 17.2008 — Talk about overkill with a database for such a simple matter

[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]


Then just write the following were you want to output the contents!
[code=php]
<?php echo $counter; ?>
[/code]
Copy linkTweet thisAlerts:
@stirxauthorSep 17.2008 — is that a hit counter though or will it make my page opacity change as people visit?

thanks!

kirsty
Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — I needed a very similar function for a project I'm working on so it's you lucky day ?.

The file writing bits aren't checked as I get my start color and percentage value in a different way. Just post up any errors, someone will be able to fix them.

YOu should be able to copy this straight and save as a PHP file.
[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]


In you CSS or body tag you can set the color like this
[code=php]background-color: #<?php echo generate_color($text_file,$start_color); ?>[/code]

place this code and the function increment_counter() on the page you want to count
[code=php]<?php

increment_counter($text_file);
?>[/code]


Any start color can be provided. Don't add the # to the function add that when you echo out the value to CSS/HTML.

katten- Using SQL isn't overkill. SQL has built in file locking and request queuing that neither your or my solution account for. Also you're opening/closing twice when you only need to open read increment and close. If you open, read and close and another connection opens, reads and closes, then you open write, close and they do the same they over write you and you're not counted. With SQL you just send an increase statement to the column and all requests are queued without conflicts. You can account for it in file code but in SQL its much easier with the only difficulty being creating the table, once that's done it's a single line of PHP to increment the counter and a single to retrieve it.
Copy linkTweet thisAlerts:
@stirxauthorSep 17.2008 — oh thankyou so much ?

so which bits of the code so i need to adjust? which values do i need to change?

do i need to create a file for it to save bits in or will it do that?

so essentially if all the text was in red, and i told it to lighten the red it would? or if i had an image would it lighten that?

i think i might find this too difficult! im trying to understand!

thanks so much though ?
Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — The file should create itself. If it doesn't it's permission issue but try it first and see if it works.

Copy this to a file and view it through a browser.
[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]


You should see 2 divs the second one lighter than the first. There are 2 example of how to call it in that chunk of code.

see how it's called here

background-color: #<?php echo generate_color($text_file,$start_color); ?>

This would be an extract from a style sheet. You could set the value in it directly.

background-color: #<?php echo generate_color('num.txt','FF0000'); ?>

And that would work just fine too. It's better to use the variable $whatever for the file so you don't have to update the file name in lots of places if you ever need to change it or reuse the code.

If the value in the file is 0 then you get the same ff0000 output as it had to lighten it by 0%. if the value is 500 then you get a 50% lighten. 1000 or 100% is white.
Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — So add the functions to the page, the top is normal but anywhere is OK.

Next add the path to the text file. If it doesn't exist PHP will try to create it.

Call the increment function

Call the generate_color function. as many time as you like with an color you want. Each call can have a different color. All will lighted by the same amount.

There are probably errors here as it's been done in a hurry. Kind of busy today, but post any issues and me or someone will help.
Copy linkTweet thisAlerts:
@stirxauthorSep 17.2008 — ok so if i write my text in the second div then it should change everytime a new visitor comes to the site? ahh you're a lifesaver! thankyou!
Copy linkTweet thisAlerts:
@SyCoSep 17.2008 — You're welcome ?

BTW, You don't[I] have [/I]to add it to the second DIV you can add this as an attribute to any CSS class or HTML tag where you would have just added a hex color.
Copy linkTweet thisAlerts:
@stirxauthorSep 24.2008 — hi again!

im having so many problems with this, i dont understand it at all and i think ive messed it all up!! its not making the text file so i made one and uploaded it too to my site, and now this error is coming up:

Parse error: syntax error, unexpected '.', expecting ')' in /homepages/31/d256435368/htdocs/Wmiy/index2.php on line 40

goto www.whatsmineisyourswhatsyoursismine.co.uk to see it, ive rewritten the coding as follows... i dont know if anyone can help but im about to give up now! i just dont get it ? booo hooooooo!!!!

<!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>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p align="left">&nbsp;</p>

<p><img src="wyimtext.jpg" width="692" height="160" /></p>

<p>&nbsp;</p>

<p><img src="thisrepresents.jpg" width="692" height="100" /></p>

<p class="style4">&nbsp;</p>

<p class="style4">&nbsp;</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>&nbsp;</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>&nbsp;</p>

<p>&nbsp;</p>

<p><a href="mailto:[email protected]">Email</a></p>

</body>

</html>
Copy linkTweet thisAlerts:
@Phill_PaffordSep 24.2008 — Did you forget to close the PHP tag?

[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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="left">&nbsp;</p>
<p><img src="wyimtext.jpg" width="692" height="160" /></p>
<p>&nbsp;</p>
<p><img src="thisrepresents.jpg" width="692" height="100" /></p>
<p class="style4">&nbsp;</p>
<p class="style4">&nbsp;</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>&nbsp;</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>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="mailto:[email protected]">Email</a></p>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@stirxauthorSep 24.2008 — yep i did, still the same problem though ?

is there another kind of code i can use to just make the whole page go white - i.e. the opacity of the page fade to 0% im wondering?
Copy linkTweet thisAlerts:
@Phill_PaffordSep 24.2008 — After taking another look I noticed you have $num.txt I think you need $num.".txt"

[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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="left">&nbsp;</p>
<p><img src="wyimtext.jpg" width="692" height="160" /></p>
<p>&nbsp;</p>
<p><img src="thisrepresents.jpg" width="692" height="100" /></p>
<p class="style4">&nbsp;</p>
<p class="style4">&nbsp;</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>&nbsp;</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>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="mailto:[email protected]">Email</a></p>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@stirxauthorSep 24.2008 — pk, so phill, i've started using your method of a simple hit counter first and now i need to manipulate the coding given by syco to make it look at the value given as the hit count and change the opacity of the page depending on this. i think. :S
Copy linkTweet thisAlerts:
@SyCoSep 24.2008 — Don't give up you're so close, ?

When you post snippets of code and an error referring to a line number please let us know which line that is. You'll get faster responses if people don't have to plough through code looking for what is likely a simple error for a experienced coder. Also please post code using the [ php ] code here [ /php ] tags (without spaces) to get the syntax highlighting. Click 'go advanced' if using the quick reply for the tag buttons, or just add them manually.

So there's a problem with how you're calling the and using the function. Read up on PHP functions if you want to understand it fully. Functions are a little weird at first, it tricky understanding how the values pass into them.

Here's a simple function that is passed a value that is converted to the variable $foo. The function will always refer to it as $foo

[code=php]
function my_function($foo){
echo $foo;
}[/code]


The function is called like this
[code=php]my_function('hello');[/code]

Calling the function will now echo what is being passed into it. A value could also be passed in to it in this way.

[code=php]
$bar='hello';
my_function($bar);
[/code]

Here the only difference is the value was stored into a variable first. So when the function is called any variable name can be used or in this example a string value (hello) can be directly used. But inside the function the name doesn't change. The reason this is useful is it doesn't matter what is passed into the function. The function will treat all values the same as it only has one variable to refer to. So all of these will work.

[code=php]
$bar='Goodbye ';
$var1='cruel ';
$var2='world ';

my_function($bar);
my_function($var1);
my_function($var2);
[/code]

will echo
Goodbye cruel world

You can even use a combination of string values and variables with a process called concatenation, a fancy word for joining together. In PHP we concatenate with a period (aka a full stop). So the above example could be called like this.

[code=php]
$bar='Goodbye ';
$var1='cruel ';
$var2='world ';

my_function($bar.$var1.$var2);

//or

my_function($bar.'cruel '.$var2);[/code]


Both will echo the same message. The function will treat whatever is passed into it simply as the variable to process. In the example function my_function(), it's the value to echo.

So in the function generate_color() I called the value in the function being passed $filename and that how it's refereed to within the function. When you call it you could use the full path a string, a relative path as a variable or any combination. The thing to remember is you're passing the value to the function and the function takes that value and uses it so don't change the function variable name, it needs to know that so it can use it. But when you call the function you use the real value of what the file name is.
Copy linkTweet thisAlerts:
@stirxauthorSep 24.2008 — thanks so much for all your help ?

ive got this far:

[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>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="left">&nbsp;</p>
<p><img src="wyimtext.jpg" width="692" height="160" /></p>
<p>&nbsp;</p>
<p><img src="thisrepresents.jpg" width="692" height="100" /></p>
<p class="style4">&nbsp;</p>
<p class="style4">&nbsp;</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">&nbsp;</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>&nbsp;</p>
<p>



<? echo 'people have viewed this page.' . increment_counter($text_file);



?>


</p>
<p>&nbsp;</p>
<p><a href="mailto:[email protected]">Email</a></p>
</div>

</body>
</html>[/code]


how ever its now showing the hits but not calling it properly still i dont think. i need just black & green to disappear! grrrrrrrr

you can view it at www.whatsmineisyourswhatsyoursismine.co.uk/index2.php
Copy linkTweet thisAlerts:
@SyCoSep 24.2008 — At a quick glance I see 3 things wrong.

1.

You've changed the value in the percentage calculation.
[code=php]$percent=$num/100*100;
//should be
$percent=$num/1000*100;[/code]


2.

$start_color should not have a # (as the notes in the earlier post said) so
[code=php]$start_color='#000000'
//should be
$start_color='000000'[/code]

It still works by chance as 0 and 00 have the same dec/hex value. But any other color not starting in 00 would not work correctly.

3.

And (forehead slap for this one)
[code=html]<div style="font-color:#<?php echo $new_color?>">
should be
<div style="color:#<?php echo $new_color?>">
[/code]


I think that should get you there.
Copy linkTweet thisAlerts:
@stirxauthorSep 24.2008 — THANKYOU!!!

ok so the black fades and ive written this to try and make the green fade too but its not working, can someone please let me know what ive done wrong??

[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>&nbsp; </p>
<div style="color:#<?php echo $new_color?>". #<?php echo $new_color2?>>
[/code]
Copy linkTweet thisAlerts:
@stirxauthorSep 25.2008 — yay! i managed it! both the green and the black change opacity. is there a way i can adjust the code so that a jpeg's opacity can be taken down by the same increement as the colour of the text is adjusted? so that if i had an image on the page, when there's been 1000 visitors, all the page disappears?

also, the email link doesnt seem to want to change colour!

thanks!

Kirsty
Copy linkTweet thisAlerts:
@SyCoSep 25.2008 — Congratulations, see I told you you were close ?

There are a couple of ways to do this you could create 1000 images of varying translucency and call each one 1.png, 2.png etc and call each one based on its filename. But honestly that would be nuts. There's a much cooler way of doing it using PHP and GD. Creating a fading image image can be done in a similar way to the text, calculate a percentage first so we know how light it needs to be. There a bunch of PHP functions under the banner GD than can manipulate images. It's really useful for things like watermarking images on the fly. So with GD we can create a mask the same color as the page bg (which must be a solid color behind the image). If we change how translucent the mask is it creates the effect of making the image fade into the bg. The mask and the image are reassembled into a single jpeg file and show in the browser. It wont actually be transparent it'll just appear transparent. The advantage of that is PNG are not very well supported by all browsers so you'll have no compatibility issues.

This shold copy and paste. Just change the values in the function call. Which you now know how to do.

[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]
×

Success!

Help @stirx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...