
October 2nd, 2007
07:55 AM
Neverside Newbie
Status: Offline!
Help with Script
I am trying to search a flat-file database I am working on. The database is a .txt file with users votes (yes/no) one after the other on different lines. I have got really close to my goal but am stuck.
This is what I have so far:
<?php
$arr = file("pollresults.txt");
foreach ($arr as $key => $value)
{
echo "Value: $value $key<br />\n";
switch ($value)
{
case "yes":
echo "good";
break;
case "no":
echo "bad";
break;
}
}
?>
This is the pollresults.txt database (small, but doesn't need to be big for testing)
When I run the script I get:
My goal is to get:
Value: no 0
bad
Value: yes 1
good

October 2nd, 2007
11:20 AM
Neverside Newbie
Status: Offline!
I don't think I made myself clear enough...
With the main bit of code, I think the section of the script below is wrong:
switch ($value)
{
case "yes":
echo "good";
break;
case "no":
echo "bad";
break;
}
For some reason, the script says $value != yes or no; depending on the circumstances.
My goal is to get it to recognize whether the vote is yes or no so I can tally them and get percentages for a poll.
If someone can help me correct the scrip so it works, I would be very grateful.
*P.S. Sorry for the double post.
Last edited by firefly18, October 2nd, 2007 11:22 AM (Edited 2 times)

November 11th, 2007
11:09 PM
Neverside Newbie
Status: Offline!
Here we go. This code works perfectly and already tallies yes and no's up for you.
<?php
$file_name = "pollresults.txt";
$fp = fopen($file_name, "rb") or die("Couldn't open file");
$data = fread($fp, filesize($file_name));
while(!feof($fp))
{
$data .= fgets($fp, 1024);
}
fclose($fp);
$values = explode("\r\n", $data);
$yes = 0;
$no = 0;
foreach ($values as $key)
{
switch ($key)
{
case 'yes':
echo $key, " - Good <br>";
$yes++;
break;
case 'no':
echo $key, " - Bad <br>";
$no++;
break;
default:
break;
}
}
echo "-----------------STATS-------------<br>";
echo "Total \"yes\" == ".$yes."<br>";
echo "Total \"no\" == ".$no."<br>";
?>
___________________

Last edited by Glennmastermind, November 11th, 2007 11:24 PM (Edited 2 times)