...

View Full Version : need help setting up loop instead of 25 statements



Dlan
02-17-2012, 08:54 PM
On my site I have a quiz with 25 questions, the responses are saved in a DB

I would like to show what % of people had answers correct for each question.
I came up with this and it seems to work fine except I have to enter it 25 times one for each question. quest1,quest2 ect. THere must be a way to do this in some kind of loop but after two days of trying I've given up. Most of what I've done has been taken code from searches and made it work on my site. Must admit I'm lost on this one. If some one could at least get me pointed in the right direction. Thanks in advance, Don...



// get total number of times test taken
$result = mysql_query("SELECT COUNT(0) FROM testresults");
$row = mysql_fetch_array($result) or die(mysql_error());
$TotalnumberTest = $row[0];

// get number of correct answers from each column
$result = mysql_query("SELECT COUNT(0) AS quest1 FROM testresults where quest1 = 'C' ") or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$TotalCorectAnswQ1 = $row[quest1];

//Percent of anwsers correct
$PercentCorrQ1 = sprintf( "%d %%", ( $TotalCorectAnswQ1 / $TotalnumberTest) * 100 );

//Display persent
$PercentCorrQ1 = $PercentCorrQ1 . " Had this question correct!";

echo $PercentCorrQ1;

sunfighter
02-18-2012, 04:47 PM
Run this query and see what you get:


select sum(IF(quest1 = "C", 1,0)), sum(IF(quest2 = "C", 1,0)), sum(IF(quest3 = "C", 1,0)), sum(IF(quest4 = "C", 1,0)) from testresults

Dlan
02-19-2012, 12:53 AM
Have I done correctly ? if so there seems to be a problem, page goes blank



$result = mysql_query("select sum(IF(quest1 = "C", 1,0)), sum(IF(quest2 = "C", 1,0)), sum(IF(quest3 = "C", 1,0)), sum(IF(quest4 = "C", 1,0)) from testresults");

sunfighter
02-19-2012, 04:05 AM
Don't add this to you code. Test it by writing a small program to get and print.

Nightfire
02-19-2012, 02:53 PM
Have I done correctly ? if so there seems to be a problem, page goes blank



$result = mysql_query("select sum(IF(quest1 = "C", 1,0)), sum(IF(quest2 = "C", 1,0)), sum(IF(quest3 = "C", 1,0)), sum(IF(quest4 = "C", 1,0)) from testresults");

You're not escaping the quotes, unless the forum has stripped the slashes

Dlan
02-19-2012, 09:55 PM
Once I took a good look at what your wrote I saw my mistake, It works fine. Thank you so very much.


<?
$sql = "select sum(IF(quest1 = \"C\", 1,0)), sum(IF(quest2 = \"C\", 1,0)), sum(IF(quest3 = \"C\", 1,0)), sum(IF(quest4 = \"C\", 1,0)) from testresults";
$result = mysql_query($sql) or die(mysql_error());
$TotalCorectAnsw = mysql_fetch_array( $result );


echo "<table border='1'>";
echo "<tr> <th>Percent Correct</th></tr>";
echo "<tr><td>";
echo "Q1=".$PercentCorrQ1 = sprintf( "%d %%", ( $TotalCorectAnsw[0] / $TotalnumberTest) * 100 );
echo "</td></tr>";
echo "<tr><td>";
echo "Q2=".$PercentCorrQ2 = sprintf( "%d %%", ( $TotalCorectAnsw[1] / $TotalnumberTest) * 100 );
echo "</td></tr>";
echo "<tr><td>";
echo "Q3=".$PercentCorrQ3 = sprintf( "%d %%", ( $TotalCorectAnsw[2] / $TotalnumberTest) * 100 );
echo "</td></tr>";
echo "<tr><td>";
echo "Q4=".$PercentCorrQ4 = sprintf( "%d %%", ( $TotalCorectAnsw[3] / $TotalnumberTest) * 100 );
echo "</td></tr>";
echo "</table>";
?>

So then I tried to take it one step farther. but this only gives results in $PercentCorrQ[0] not [1] [2]. If I could, can I ask for your help one last time?


<?
$result = mysql_query("SELECT COUNT(0) FROM testresults");
$row = mysql_fetch_array($result) or die(mysql_error());
$TotalnumberTest = $row[0];

$counter = 0;
$result = mysql_query("select sum(IF(quest1 = 'C', 1,0)), sum(IF(quest2 = 'C', 1,0)), sum(IF(quest3 = 'C', 1,0)), sum(IF(quest4 = 'C', 1,0)) from testresults") or die(mysql_error());
while ( $TotalCorectAnsw = mysql_fetch_array( $result ))
{
$PercentCorrQ[$counter] = sprintf( "%d %%", ( $TotalCorectAnsw[$counter] / $TotalnumberTest) * 100 );
$counter++;
};

echo "<table border='1'>";
echo "<tr> <th>Percent Correct</th></tr>";
echo "<tr><td>";
echo "Q1=". $PercentCorrQ[0];
echo "</td></tr>";
echo "<tr><td>";
echo "Q2=". $PercentCorrQ[1];
echo "</td></tr>";
echo "<tr><td>";
echo "Q3=".$PercentCorrQ[2];
echo "</td></tr>";
echo "<tr><td>";
echo "Q4=".$PercentCorrQ[3];
echo "</td></tr>";
echo "</table>";
?>

sunfighter
02-20-2012, 02:02 AM
The number one thing I can do for you Dlan is to tell you to STOP using <? to start your php sections. It's a shorthand code that most all of the internet does not use. Newer php modules have it shut off in the ini file and it will not be recognized. That may give you a bunch of stuff to update. Instead use the proper <?php as your opening tag.

Don't know where you got count(0), I can't find it anywhere, but it seems to work. I use count(*) and suggest that you do also just because I can't find anything on it.

sprintf() function writes a formatted string to a variable. I don't think we need it and I don't ever use it.

You have a while loop to make a number of strings and then insert them into a table one at a time. Since we're trying to get 25 entrees into a table it maybe better to do that loop while writing to the table. Here's What I have:


<?php

$sql = "select count(*), sum(IF(quest1 = 'C', 1,0)), sum(IF(quest2 = 'C', 1,0)), sum(IF(quest3 = 'C', 1,0)), sum(IF(quest4 = 'C', 1,0)) from testresults";
$result = mysql_query($sql) or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Percent Correct</th></tr>";
while($answ = mysql_fetch_row($result ))
{
for($i = 1; $i < $answ[0]-1; $i++)
{
echo "<tr><td>";
echo "Q".$i." = ".$answ[$i]/$answ[0];
echo "</td></tr>";
}
}
echo "</table>";
?>

$answ[0] is the total number of records in the table and so should equal the number of people that gave an answer. Once you check this you can add the
sum(IF(quest1 = 'C', 1,0)),
for the rest of the questions to the query and the code should work for you.

Dlan
02-20-2012, 04:36 AM
Thank You all for your help on this. I had to make a couple of small changes to get the results I was looking for, but without your help I never would have got it. BTW now that I know I will definitely up date my pages to include <?php.
Now that I'm retired I have the time to learn this stuff.
This quiz will be part of an on-line learning tool for all that play the card game "euchre"
Thanks again
Don...
OhioEuchre.com


<?php
$result = mysql_query("SELECT COUNT(0) FROM testresults");
$row = mysql_fetch_array($result) or die(mysql_error());
$TotalnumberTest = $row[0];

$sql = "select count(*), sum(IF(quest1 = 'C', 1,0)), sum(IF(quest2 = 'C', 1,0)), sum(IF(quest3 = 'C', 1,0)), sum(IF(quest4 = 'C', 1,0)) from testresults";
$result = mysql_query($sql) or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Percent Correct</th></tr>";
while($answ = mysql_fetch_row($result ))
{
for($i = 1; $i < 26; $i++)
{
echo "<tr><td>";
echo "Q".$i."=". sprintf( "%d %%", ( $answ[$i] / $TotalnumberTest)* 100) ;
echo "</td></tr>";
}
}
echo "</table>";
?>

sunfighter
02-20-2012, 06:11 AM
I'm an old euchre player myself. We hid when we had nothing to do and played when I worked at Sinclair Refinery. Do you play on line Don? I'd love to see this up and running when you finish it. Send me a line through the form's email.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum