nvidia Posted December 23, 2006 Share Posted December 23, 2006 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 "; Quote Link to comment https://forums.phpfreaks.com/topic/31702-php-checking-contents-of-file/ Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 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 ;) Quote Link to comment https://forums.phpfreaks.com/topic/31702-php-checking-contents-of-file/#findComment-146930 Share on other sites More sharing options...
nvidia Posted December 23, 2006 Author Share Posted December 23, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/31702-php-checking-contents-of-file/#findComment-146971 Share on other sites More sharing options...
Jessica Posted December 23, 2006 Share Posted December 23, 2006 Yes. Try r+ instead of a+. Quote Link to comment https://forums.phpfreaks.com/topic/31702-php-checking-contents-of-file/#findComment-146972 Share on other sites More sharing options...
nvidia Posted December 23, 2006 Author Share Posted December 23, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/31702-php-checking-contents-of-file/#findComment-146977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.