Jump to content

PHP CHECKING CONTENTS OF FILE


nvidia

Recommended Posts

Hi, i have a form whereby users select a value via radio buttons, once they press the submit button the values are stored into a text file. After reading and writing to my C2.txt file, how do count how many of a particular entry, for example "Mr.X" has been stored in my text file?
form1.html file:

Mr.X <input type="radio" name="select" value="Mr.X " CHECKED> 

handler1.php file // this is what i've done so far but not working properly

$Mrx = "Mr.x";
$counter = 0;

  while( $contents = fread( $fp,filesize($filename) ))
  {
    if($contents == $Mrx)
      {
          $counter = $counter+1;
      }
  }
echo $counter.  "% chose Mr.x ";

Link to comment
https://forums.phpfreaks.com/topic/31702-php-checking-contents-of-file/
Share on other sites

Right now you are checking if the entire string $contents is equal to the string $Mrx. You want to find if $Mrx is IN the string $contents and how many times, right?

[code]$counter = substr_count($contents, $Mrx);[/code]
That gives you a place to start. Go from there ;)
Hi, sorry about this but i'm still having the same problem regarding counting the number of entries.
[code]$fp = fopen($filename,'a+'); // a+  open for reading + writing
                              // place file pointer at EOF
$write = fwrite($fp, "$selection\r\n");
$Mrx = "Mr.X";
$counter = 0;

while( $contents = fread( $fp,filesize($filename) ))
  {
      $counter = substr_count("$contents", "$Mrx");
  }
echo $counter.  "% chose Mr.x ";

[/code]
I understand what you are saying regarding checking variable $Mrx is IN the file, but for some reason, it still outputs 0. Also as a checker, i'm trying to ouput the variable $contents so that i know it has something to compare against, but it outputs nothing. Could this be the problem?
Sorry again, after replacing a+ with r+, after reading the second value, the first value gets replaced. Therefore i only c the most recent value. I beilve having a+ is the only way for reading multiple values in my case. Although i still don't understand why it does not output the number of ocurrences of a particular entry, Mr.X, in this case. It only ouputs 0. Am i right in saying that that the while loop condition is a still a good logical checker? The reason why i'm asking this is because i think this is the way it would work for me. Assistance would be very greatful as before :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.