Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Urbana Ohio
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help setting up loop instead of 25 statements

    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...

    PHP Code:
    // 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
    Last edited by Inigoesdr; 02-19-2012 at 02:43 AM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,691
    Thanks
    25
    Thanked 656 Times in 655 Posts
    Run this query and see what you get:
    Code:
    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

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Urbana Ohio
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have I done correctly ? if so there seems to be a problem, page goes blank


    PHP Code:
    $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"); 
    Last edited by Inigoesdr; 02-19-2012 at 02:43 AM.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,691
    Thanks
    25
    Thanked 656 Times in 655 Posts
    Don't add this to you code. Test it by writing a small program to get and print.

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Quote Originally Posted by Dlan View Post
    Have I done correctly ? if so there seems to be a problem, page goes blank


    PHP Code:
    $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

  • #6
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Urbana Ohio
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Once I took a good look at what your wrote I saw my mistake, It works fine. Thank you so very much.

    Code:
     <?
     $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?

    Code:
    <?
    $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>";
     ?>

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,691
    Thanks
    25
    Thanked 656 Times in 655 Posts
    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:
    Code:
    <?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.

  • #8
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Urbana Ohio
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

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

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,691
    Thanks
    25
    Thanked 656 Times in 655 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •