Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

70 users online



Help with Script

Help with Script

Currently viewing this thread: 1 (0 members and 1 guests)


firefly18

firefly18

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:

Code:

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

Code:

no
yes

When I run the script I get:

Code:

Value: no 0
Value: yes 1

My goal is to get:

Code:

Value: no 0
bad
Value: yes 1
good

firefly18

firefly18

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:

Code:

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)

Glennmastermind

Glennmastermind

Neverside Newbie
Status: Offline!

Here we go. This code works perfectly and already tallies yes and no's up for you.

Code:

<?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>";

?> 

___________________

http://audiotracker.bamboozled.org/remote/song.php/DF14

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

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0075 seconds.