nvidia Posted December 26, 2006 Share Posted December 26, 2006 Hi, i am trying to loop through my C2.txt file to find the number of occurrences of 'Mr.X',in my case, and displaying that in my php file page. I'v used the function 'file put contents' to successfully write my data values taken from my radio buttons and 'file get contents' to read the data in my while loop, unsucessful.[code]<?phpsession_start();$selection = $_POST['select'];// store location of file in variable$filename = "/home/eland/u1/kbccs/w1009048/public_html/Internet_Pg/C2/C2.txt"; function file_put_contents($filename,$selection) { $fp = fopen($filename,'a+'); if(!$fp) { return false; } else { $write = fwrite($fp, "$selection\r\n"); fclose($fp); return true; } }$put = file_put_contents($filename, $selection);$MrX = "Mr.X";while($contents = file_get_contents("C2.txt")) { $count = substr_count($put,"$MrX"); }?> [/code][code] <? echo $count ?> % chose Mr.X[/code]Everytime i press my submit button, it does not show the occurrences. Can somebody help please? Link to comment https://forums.phpfreaks.com/topic/31898-solved-looping-through-text-file-help/ Share on other sites More sharing options...
Shu Posted December 26, 2006 Share Posted December 26, 2006 i think u need to changewhile($contents = file_get_contents("C2.txt"))to while($contents = file_get_contents($filename)) Link to comment https://forums.phpfreaks.com/topic/31898-solved-looping-through-text-file-help/#findComment-148032 Share on other sites More sharing options...
craygo Posted December 26, 2006 Share Posted December 26, 2006 I may be wrong on this, but php already has a built in function called file_put_contents. I do not think you can name your function the same thing. Also there is no need to loop through the contents. You are storing it as a string not an array. you are also using the data you are storing to do the search instead of what is actually in the file[code]<?php$filename = "/home/eland/u1/kbccs/w1009048/public_html/Internet_Pg/C2/C2.txt";$contents = file_get_contents($filename);$MrX = "Mr.X";$count = substr_count($contents);echo $count;?>[/code]Ray Link to comment https://forums.phpfreaks.com/topic/31898-solved-looping-through-text-file-help/#findComment-148039 Share on other sites More sharing options...
nvidia Posted December 26, 2006 Author Share Posted December 26, 2006 [quote author=craygo link=topic=119968.msg491834#msg491834 date=1167164485]I may be wrong on this, but php already has a built in function called file_put_contents. I do not think you can name your function the same thing. Also there is no need to loop through the contents. You are storing it as a string not an array. you are also using the data you are storing to do the search instead of what is actually in the file[code]<?php$filename = "/home/eland/u1/kbccs/w1009048/public_html/Internet_Pg/C2/C2.txt";$contents = file_get_contents($filename);$MrX = "Mr.X";$count = substr_count($contents);echo $count;?>[/code]Ray[/quote]Thank you very much, with regards to your comment about using the loop, am i right in saying that since the function 'substr_count' counts the number of occurrences, there was no need to use a loop in the first place? As it does this for us. Again, thank you Link to comment https://forums.phpfreaks.com/topic/31898-solved-looping-through-text-file-help/#findComment-148052 Share on other sites More sharing options...
craygo Posted December 27, 2006 Share Posted December 27, 2006 the function file_get_content() returns everything in one long string, so yes there is not need for the loop. If you used file() then you would have to use a loop because each line would be an entry in an array.Ray Link to comment https://forums.phpfreaks.com/topic/31898-solved-looping-through-text-file-help/#findComment-148129 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.