Jump to content

[SOLVED] Looping through text file, help


nvidia

Recommended Posts

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]
<?php
session_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

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 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

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.