Jump to content

PHP reading a file through a while loop?


nvidia

Recommended Posts

Hi, i have a file which i have written values into which is from a form whereby people select values, which is then submitted and written to my C2.txt file. The values selected are of type raidio buttons and one of them is already checked. I'm am trying to read my C2.txt file using a while loop and counting how many times aparticular string, 'Mr.X', in my case exist. The problem i'm having is that, after selecting Mr.X and pressing submit, my second page, handler4.php, does not load up. Therefore i'm assuming that my while loop is the problem.
[code]
<form method="post" action="handler4.php">
Mr.X <input type="radio" name="select" value="Mr.X " CHECKED> <br />
Tom Cruis <input type="radio" name="select" value="Tom Cruis " ><br />
  <input type=submit  value="Press me!"  />
  </form> 
[/code]

handler4.php file
[code]
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);
$count = 0;
  while($contents = file_get_contents("C2.txt"))
  {
      $count = substr_count($put, 'Mr.X');

  }
?>

<? echo $count ?>% chose Mr.X

[/code]

Please note, having 'a+' is the only way i have found that will allow me to FULLY write each individual value on a separte line. As i said, the problem i believe is the while loop because everytime i press submit, my php file does not load up, perhaps it is in a infinite loop. Can anybody suggest any slight modificataions to the condition so that when i press submit, the number of occurrences of Mr.X is shown?? :D
Link to comment
https://forums.phpfreaks.com/topic/31782-php-reading-a-file-through-a-while-loop/
Share on other sites

Your problem is, first, you're creating an infinite loop by using [code]while($contents = file_get_contents("C2.txt"))[/code] This should always return true and therefore will keep repeating. I don't think you need this for counting anyways.
Second, you're using substr_count() on the wrong variable ($put) which is defined as [code]$put = file_put_contents($filename, $selection);[/code]

[code=php:0]<?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);
$contents = file_get_contents("C2.txt");
$count = substr_count($contents, 'Mr.X');

?>

<?php echo $count ?> people chose Mr.X[/code]


One last suggestion, if you have a MySQL database available and know how to use MySQL, it would be a better choice for poll counts than reading and writing to a file. This would also be better for getting a percent of the people who chose the same option. :)

Good luck :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.