Quiz in PHP

Discussion in 'PHP' started by ducca, Oct 6, 2006.

  1. #1
    Hi All,
    I,ve downloaded a script to help me learn php, I, m a total novice at the moment but slowly trying to take it in.
    I,ve downloaded a quiz from the following url,,,

    http://www.widgetmonkey.com/

    my problem is the php files are all fine but I,m having problems with making them compile, I think its because the php_self is outdated,
    heres the first script,

    I'm using xammp/mysql/php on win xp and I,m running the scripts through dreamweaver, other example scripts work fine

    thanks in advance for any help:)

    
    <?php
    
    //copyright widgetmonkey.com 2001
    
    include("contentdb.php");
    
    $display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
    	
    if (!$submit) {
    
    
    	echo "<form method=post action=$PHP_SELF>";
    	echo "<table border=0>";
    
    	while ($row = mysql_fetch_array($display)) {
    
    	$id = $row["id"];
    	$question = $row["question"];
    	$opt1 = $row["opt1"];
    	$opt2 = $row["opt2"];
    	$opt3 = $row["opt3"];
    	$answer = $row["answer"];
    
    	echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
    	echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";
    
    	}
    
    	echo "</table>";
    	echo "<input type='submit' value='See how you did' name='submit'>";
    	echo "</form>";
    
    }
    
    elseif ($submit) 
    
    {
    
    	$score = 0;
    	$total = mysql_num_rows($display);
    		while ($result = mysql_fetch_array($display)) 
    		
    		
    		{
    		
    			$answer = $result["answer"];
    			$q = $result["q"];
    			
    		if ($$q == $answer) 
    		{
    		$score++; 
    		}
    	
    	}
    	
    	echo "<p align=center><b>You scored $score out of $total</b></p>";
    	echo "<p>";
    	
    	if   ($score == $total) {
    	echo "Congratulations! You got every question right!";
    	}
    	elseif ($score/$total < 0.34) {
    	echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
    	}
    	elseif ($score/$total > 0.67) {
    	echo "Well done! You certainly know your stuff.";
    	}
    	else {
    	echo "Not bad - but there were a few that caught you out!";
    	}
    
    echo "</p>";
    
    echo "<p>Here are the answers:";
    
    echo "<table border=0>";
    $display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
    while ($row = mysql_fetch_array($display)) {
    
    $question = $row["question"];
    $answer = $row["answer"];
    $q = $row["q"];
    
    echo "<tr><td><br>$question</td></tr>";
    
    if ($$q == $answer) 
    		{
    		echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
    		}
    elseif ($$q == "") {
    echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
    }
    else {
    echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>";
    }
    
    }
    echo "</table></p>";
    
    
    
    }
    
    ?>
    PHP:
     
    ducca, Oct 6, 2006 IP
  2. mitchandre

    mitchandre Peon

    Messages:
    313
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I've never compiled a php script in my life. What do you mean?
     
    mitchandre, Oct 6, 2006 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In PHP there's nothing to compile. $PHP_SELF can't get 'outdated' either. It just refers to 'the current page'.

    Why don't you just try it in a browser on http://localhost and see what the error is? Running it via Dreamweaver is asking for trouble IMO.
     
    T0PS3O, Oct 6, 2006 IP
  4. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ref; Compilig the script, I,ve been playing with java lol
    only been looking at php for a short while,
    thanks for your help
     
    ducca, Oct 6, 2006 IP
  5. faraday911

    faraday911 Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    First thing to do if you want to run php scripts on your local box is to download php interpreter off php.net, running a web server could also be useful if you plan to use Dreamweaver further on
     
    faraday911, Oct 7, 2006 IP
  6. coolsaint

    coolsaint Banned

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    coolsaint, Oct 7, 2006 IP