p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Another counter in PHP life


Message #1 by "Jamal" <new_mojam@y...> on Thu, 10 Oct 2002 22:07:08
Hi Guys,
I get an error in the code below. Please can you help me to fix the error??
<?php 

$filename = "count.dat"       //name of the file where the data is kept 
$message1 = "There have been";        // first message section   \/ 
$message2 = " visitors to this page."; // second message section 
[$message1 $count $message2] 
$bold = "1"; // if 1, the numbers are bold, if 0 nothing is bold 
$font = "verdana"; // The font name 
$fontsize = "2"; // The size of the font 
$textcolor = "#005500"; //The color of the $message1 and $message2 (HEX) 
$numbercolor = "#550000"; //The colour of the numbers (HEX) 
$numberglow = "0"; //If 1, the numbers glow, if 0, they don't. 
$glowcolor = "#0000FF"; //The colour of the glow 
$invisible = "0" //If 1, the counter will not show (it will still count!) 



$fp = fopen($filename,"r+"); 
flock($fp,1); 
$count = fgets($fp,6); 
$count += 1; 
rewind($fp); 
fputs($fp,$count); 
flock($fp,3); 
fclose($fp); 
if ($invisible == "1"){ 
//counter does nothing because 'invisible' is on  (1) 
} elseif ($invisible == "0"){ 
if ($bold == "1"){ 
    $bold1 = "<b>"; 
    $bold2 = "</b>"; 
} elseif ($bold == "0"){ 
    $bold1 = ""; 
    $bold2 = ""; 
} 
if ($numberglow == "1"){ 
    $glow1 = "<table STYLE=width:100%;filter:GLOW
(color=$glowcolor,strength=#+9)>"; 
    $glow2 = "</table>"; 
} elseif ($numberglow == "0"){ 
    $glow1 = ""; 
    $glow2 = ""; 
} 
print "<font face='$font' size='$fontsize' color='$textcolor'> $message1 
</font><font face='$font' size='$fontsize' color='$numbercolor'>$glow1
$bold1$count$bold2$glow2</font><font face='$font' size='$fontsize' 
color='$textcolor'> $message2</font>"; 
 } 
 ?> 



Thanks 
Jamal
Message #2 by "Nikolai Devereaux" <yomama@u...> on Thu, 10 Oct 2002 15:18:33 -0700
Wanna tell us what the error you're getting is?  I have a feeling it's due to a
missing semicolons on the $filename = "count.dat" and $invisible = "0" lines.


Here's a few unasked for comments, btw -- you are enclosing all your integer
values in quotes, making them strings.  I don't really understand why you're
doing this... why not leave them as integers?

Actually, why not create them as boolean (true or false) values, since that's
really what you're using them for?

Let me rewrite your code to show you how much more readable it can be when
using better picked variable names and/or values:


<?php

$filename    = 'count.dat';               //name of the file where the data is
kept
$message1    = 'There have been ';         // first message section
$message2    = ' visitors to this page.'; // second message section

$bold        = true;        // if 1, the numbers are bold, if 0 nothing is bold
$font        = 'Verdana';   // The font name
$fontsize    = 2;           // The size of the font
$textcolor   = '#005500';   //The color of the $message1 and $message2 (HEX)
$numbercolor = '#550000';   //The colour of the numbers (HEX)
$numberglow  = false;       //If 1, the numbers glow, if 0, they don't.
$glowcolor   = '#0000FF';   //The colour of the glow
$invisible   = false;       //If 1, the counter will not show (it will still
count!)


if(!file_exists($filename))
{
    $fp = fopen($filename, 'w');
    $count = 1;
}
else
{
    $fp = fopen($filename, 'r+');
    $count = 1 + fgets($fp);
}

if($fp)
{
    flock($fp, LOCK_EX);
    rewind($fp);
    fputs($fp, $count);
    flock($fp, LOCK_UN);
    fclose($fp);
}
else
{
    exit('Could not open counter file for reading or writing!');
}

if (! $invisible)
{
    $bold_b = ($bold)? '<b>'  : '';   // begin tag
    $bold_e = ($bold)? '</b>' : '';   // end tag

    $glowstyle = "width:100%;filter:GLOW(color={$glowcolor},strength=#+9)";
    $glow_b = ($glow)? "<table STYLE=\"{$glowstyle}\">" : '';
    $glow_e = ($glow)? '</table>' : '';

    print "<font face=\"{$font}\" size=\"{$fontsize}\"
                 color=\"{$textcolor}\">"
        . $message1
        . "</font>"
        . "<font face=\"{$font}\" size=\"{$fontsize}\"
                 color=\"{$numbercolor}\">"
        . $glow_b . $bold_b . $count . $bold_e . $glow_e
        . "</font><font face=\"{$font}\" size=\"{$fontsize}\"
                        color=\"{$textcolor}\">"
        . $message2
        . "</font>";
}
?>



The new mode you're using for fopen() does _NOT_ attempt to create the file if
it does not exist.  For this reason, you should check to see if the file exists
or not.  If it doesn't, attempt to open the file in a writing mode that will
create the file for you.

Since reading from the file and writing to the file are really two separate
tasks, I separated them.  The first if/else block determines how we should open
the file.  If the file doesn't exist yet, then we initalize $count to 1, and
set up $fp so that we can write $count to the file.  Otherwise, we read in the
old value and increment it before storing it into $count.

The next if/else block tests to see if we have a valid $fp for writing.  If so,
we lock the file around the write operation, write the value of $count to the
file, and close it.

Since our flag variables ($invisible, $glow, and $bold) are now booleans, we
don't need to compare them to anything to generate a boolean value.  They
already ARE a boolean value.  Now the code also reads a lot more like english:

if(! $invisible)   ==>
   if not invisible, then...


$bold_b = ($bold)? '<b>' : '';  =>
   bold?  yes -- bold_b is '<b>'.  no -- bold_b is empty ('').


etc.

hope this makes sense!

Nik





  Return to Index