Page 1 of 1

Stats

Posted: Wed Jul 23, 2003 10:17 am
by ozyvent
I have a plain text file, with the results of a survey I'm conducting and I want to create a graphical stats file. I have made the stats page easily, just updating the "results.txt" file manually with what numbers etc. and then the stats page being written with the data in that file.

In the "entries.txt" file from which entries are added from a form. Each new entry appears on a separate line in the following format:
date|host|name|email|state|answer1|answer2|answer3|textanswer1|answer4|text answer2
date|host|name|email|state|answer1|answer2|answer3|textanswer1|answer4|text answer2

Each "answerX" is multi-choice.
Answer1 = for or against
Answer2 = yes or no
Answer3 = yes or no
Answer4 = yes or no

I want a script to be able to count how many
e.g. fors and againsts appear in the answer1 area, and so on for each "answerX" field" (dont worry about the textanswerX's, they're not part of the graphical stats) Then once it's counted the number of answers for each option. I want it to write to a plain text file each one in the form of
$for = "X";
$ag = "X";
$yes1 = "X";
$no1 = "X";
and so on.

I want percentages as well, but once each figure it's added up I can make it do the percentages easily. and create the stats file from the results written to the results.txt file from this process.

Im working with explode() which works well. It's just exploding each line and calculating the number of answers that I'm stuck with.

Thanks in advance. If you can help, or need more info, please ask. :)

...

Posted: Wed Jul 23, 2003 1:55 pm
by kettle_drum
Ok, you should just be able to:

1) Open the text file using fopen().
2) Get a line of data at each time using fget().
3) Split each line of data using explode() with the | char.
4) Have a series of if statments that check the array that explode has made to see what the result is.
5) Count the results.

That should be all you need to do. You may also wish to keep a counter for the number of survey's so you know what to divide by for the percentage.

Oh, also since each question only has 2 answers if would be easier to use '1' and '0' for either yes/no and for/against.

Posted: Wed Jul 23, 2003 9:29 pm
by ozyvent
Opening the file works a treat. It just gets stuck at the fget() bit and throws up an error.

Code: Select all

<?php

// First, open the entries file
@ $fp = fopen($entries."a") 

// Now get each line from entries file
while (!feof($fp)) {
	
	$line = fgets($fp, 4096);
	explode("|", $line);

}
?>
Brings up the error:
Parse error: parse error, unexpected T_WHILE in /home/virtual/site3/fst/var/www/html/venturers/survey/results1.php on line 8

...

Posted: Wed Jul 23, 2003 11:17 pm
by kettle_drum
You need a ; after the $fp = fopen();

Code: Select all

<?php
$fp = fopen($entries."a");
?>

Stuck

Posted: Thu Jul 24, 2003 7:41 am
by ozyvent

Code: Select all

<?php

$entries = "entries.txt";

// Check if file exists
if (file_exists($entries)) {

	$fp = fopen($entries,"a");


} else {
	echo "<i>" . $entries . "</i> does <b>not</b> exist on this server.<br>";
}


while (!feof($fp)) { 
    
   $response = fgets($fp, 4096); 
   explode("|", $response); 

}

// Here we'll add up total for and total against

if ($response[6] = 'for')
	$for = $for+1;
if ($response[6] = 'against')
	$ag = $ag+1;
$total = $for + $ag;

// Then Calculate Percentages
$for_percent = ($for/$total) * 100;
$ag_percent = ($ag/$total) * 100;

// Now echo the results
echo "<b>For =</b> " . $for . "<br>\n";
echo "<b>Percent =</b> " . $for_percent . "<br>\n";
echo "<b>Against =</b> " . $ag . "<br>\n";
echo "<b>Percent =</b> " . $ag_percent . "<br>\n";
echo "<b>Total =</b> " . $total . "<br>\n";


// Now close the entries file
fclose($fp);

?>
OK... this is what i got so far... and im stuck... it checks if the file exists fine, and opening it... and im still stuck with the fget/explode bit.

Theres no errors so... I'm not sure whats wrong with it.

Posted: Thu Jul 24, 2003 7:45 am
by qartis
Replace:

Code: Select all

explode("|", $response);
With:

Code: Select all

$response = explode("|", $response);

Posted: Thu Jul 24, 2003 7:53 am
by qartis
I just realized, you're only getting one recordset from the file.

Instead of

Code: Select all

while (!feof($fp)) {
   
   $response = fgets($fp, 4096);
   explode("|", $response);

}

// Here we'll add up total for and total against

if ($response[6] = 'for')
   $for = $for+1;
if ($response[6] = 'against')
   $ag = $ag+1;
$total = $for + $ag;
Put this:

Code: Select all

while (!feof($fp)) {
   $response = fgets($fp, 4096);
   $votes[] = explode("|", $response);
}

foreach ($votes as $array){
if ($array[6] == "for"){
$for++;
} elseif ($array[6] == "against"){
$ag++;
}
}
$total = $for+$ag;

Posted: Thu Jul 24, 2003 11:31 am
by ozyvent
ok... i feel pretty stupid here... so please bare with me. Basically I learn from being told straight out exaclty what to do... So far you've helped heaps. My knowledge of PHP is limited so yeah... i dont get it whats wrong, how to fix it etc. With you assistance i'll eventually get it.

2 questions:
1) it displays nothing for the echo $for and $ag... and therefore 0s in the percent lines, and i get division by zero errors.
2) how would i get it to output each variable it creates to a separate line?
e.g. $for = "X";
$ag = "Y";
$for_percent = "A";
$ag _percent = "B";
and so on.

Thanks

Posted: Thu Jul 24, 2003 8:32 pm
by qartis

Code: Select all

<?php
$entries = "entries.txt";

if (!file_exists($entries)) {
   die ("<i>" . $entries . "</i> does <b>not</b> exist on this server.<br>");
}

$lines = file($entries);

foreach ($lines as $line){
	$votes[] = explode("|",$line);
}

foreach ($votes as $array){
	if ($array[5] == "for"){
		$for++;
	} elseif ($array[5] == "against"){
		$ag++;
	}
}

$total = $for+$ag;
$for_percent = ($for/$total) * 100;
$ag_percent = ($ag/$total) * 100;

echo "<b>For: </b> {$for} ({$for_percent}%)<br>\n";
echo "<b>Against: </b> {$ag} ({$ag_percent}%)<br>\n";
echo "<b>Total Votes: </b> ".$total."<br>\n";
?>

Thank you

Posted: Sun Jul 27, 2003 4:49 am
by ozyvent
What you gave me worked well, and from it I figured out what I had to do for the rest. Thanks!! :D :D