p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: counter in php


Message #1 by "Jamal" <new_mojam@y...> on Tue, 8 Oct 2002 20:40:54
Hi,
can anyone please help me on how to use this counter code on php file 
please. I am stupid so make your explanation easy to understand.
<?php
$dfile = "count_data.txt";
if(file_exists($dfile)) {
        $inf = fopen($dfile, "r");
        $count = fread($inf, 10);
        $count = (int)$count;
        $count++;
        fclose($inf);
        $outf = fopen($dfile, "w");
        fputs($outf, $count);
        fclose($outf);  }
else { echo "File, $dfile, not found!"; }  ?>
<center>
You are the
<?
echo $count;
$rem = $count % 10;
if (($rem >= 1) && ($rem <= 3)) {
        if((($count % 100) >= 11) && (($count % 100) <= 13)) {
                echo "th "; }
        else {
                switch ($rem) {  
                        case (1):  { echo "st ";  break;}
                        case (2):  { echo "nd ";  break;}
                        case (3):  { echo "rd ";  break;}
                        default :  { break; }             
                }
        }
}
else { echo "th "; }   ?>
visitor since --insert date here-- .
</center>

thanks,
jamal
Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 8 Oct 2002 12:53:09 -0700
Okay, Let's go line by line.  Most of the lines are self explanitory, like
"if(file_exists($dfile))", so I'll skip those.


>         $inf = fopen($dfile, "r");

If the file exists, we open it for reading.  $inf is a handle to the file,
which gives other file manipulation functions a means to access the file.

>         $count = fread($inf, 10);

Up to the first 10 bytes of the file are read, and that result is stored into
the variable $count.  fread() returns a string, because files are read as text.

>         $count = (int)$count;

This forces PHP to convert the string value to it's numerical representation.
For example, if the file contained the character '5', $count would contain the
string "5".  This conversion would result in $count being set the integer value
of 5.

>         $count++;

increment count by one.

>         fclose($inf);

close the counter file, since we're not reading it anymore.

>         $outf = fopen($dfile, "w");

Reopen the file, this time for writing.  Opening the file with 'w' as the
second parameter will delete the contents of the file, so that anything you
write will be the only contents of the file.

>         fputs($outf, $count);

Write the current value of $count back into the file.  fputs() expects a string
as it's second argument, so the integer value is converted behind the scenes.
In our previous example, the 5 would've been incremented to 6, and this 6 would
be converted to the string "6".

>         fclose($outf);  }

Close the file since we're done writing.


> $rem = $count % 10;
> if (($rem >= 1) && ($rem <= 3)) {
>         if((($count % 100) >= 11) && (($count % 100) <= 13)) {
>                 echo "th "; }
>         else {
>                 switch ($rem) {
>                         case (1):  { echo "st ";  break;}
>                         case (2):  { echo "nd ";  break;}
>                         case (3):  { echo "rd ";  break;}
>                         default :  { break; }
>                 }
>         }
> }
> else { echo "th "; }   ?>


All this stuff above just determines whether or not the value of $count ends in
1, 2, or 3, but is NOT a 13, 14, or 15, since any of these numbers will have a
different suffix than anything else.

To illustrate:

1st, 2nd, 3rd, 4th, 5th, ...(all with 'th'), 11th, 12th, 13th, ...(still with
'th') 100th, 101st, 102nd, 103rd, 104th, ...


There are several flaws in the code in that not enough error checking is done.
We assume that we can open the file for writing if we'd successfully opened it
for reading, which is not always the case.  But those are details for another
day, since getting the gist of things is more important.  Security, error
checking, and other general robustness concerns should come after you're more
familiar.


Take care,

Nik


  Return to Index