Jump to content

doctor-Eggman

Members
  • Posts

    43
  • Joined

  • Last visited

    Never

Posts posted by doctor-Eggman

  1. I am making this quiz following a tutorial I got online. it works by having the correct answer as the first part of the array then has a code to shuffle them other wise every time the correct answer will be the first one you can select. I have applied the code and done everything as it says but it isnt shuffling. Every time the correct answer appears first.

     

    Here is the questions and answers array and the code to shuffle it. Can any one give me a hand and help me get it to work?

     

    The questions and answers

    <?php 
    $questions = array('FTP','AJAX','RSS','XSS','PHP','W3C','XML','YUI','HTML','CGI','SSL','SQL','HTTP','CSS','SOAP','WAI','SSI','JSON','XSLT','WCAG'); 
    $answers = array( 
    array(0 =>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'), 
    array(0 => 'Asynchronous JavaScript and XML','All JavaScript and XML','Alternative Java and XML','Actual JavaScript and XML'), 
    array(0 => 'Really Simple Syndication','Really Simple Scripting','Ready-Styled Scripting','Really Stupid Syndication'), 
    array(0 => 'Cross-site Scripting','Cross-site Security','Cleverly Structured Scripting','eXtremely Safe and Secure'), 
    array(0 => 'PHP: Hypertext Preprocessor','Post Hypertext Processor','Practical HTML Processing','Process HTML Prettily'), 
    array(0 => 'World Wide Web Consortium','World Wide Web Committee','World Wide Web Creatives','Wakefield Willy Wavers Club'), 
    array(0 => 'eXtensible Markup Language','eXtendable Markup Language','Crossover Markup Language','eXtreme Markup Language'), 
    array(0 => 'Yahoo User Interface','Yahoo\'s Useful Idea','Yahoo Utility Interface','Yahoo User Interaction'), 
    array(0 => 'Hypertext Markup Language','Human Markup Language','Helpful Markup Language','Hypertext Memory Language'), 
    array(0 => 'Common Gateway Interface','Common or Garden Interaction','Computer\'s Graphical Intelligence','Common Graphical Interface'), 
    array(0 => 'Secure Sockets Layer','Server Security Layer','Server Security Level','Secret Socket Layer'), 
    array(0 => 'Structured Query Language','Stupid Query Language','Secure Query Language','Strict Query Language'), 
    array(0 => 'Hypertext Transfer Protocol','Hypertext Traffic Protocol','HTML Traffic Transfer Protocol','HTML Through Traffic Protocol'), 
    array(0 => 'Cascading Style Sheets','Custom Style Sheets','Clientside Style Sheets','Calculated Style Sheets'), 
    array(0 => 'Simple Object Access Protocol','Structured Object Access Protocol','Simple, Objective And Private','Simply Obvious Access Principle'), 
    array(0 => 'Web Accessibility Initiative','World Wide Accessibility Intiative','World Wide Accessibility Incorporation','Web Accessibility and Inclusion'), 
    array(0 => 'Server-Side Include','Server-Side Intelligence','Scripted Server Include','Secure Server Include'), 
    array(0 => 'JavaScript Object Notation','JQuery-Scripting Object Notation','Just Simple Object Notation','JavaScript Over the Net'), 
    array(0 => 'eXtensible Stylesheet Language Transformation','eXpandable Stylesheet Language Transfer','eXtensible Stylesheet Language Transfer','eXtendable Stylesheet Language Transformation'), 
    array(0 => 'Web Content Accessibility Guidelines','Wakefield Community Action Group','Web Criteria And Guidelines','World-wide Common Access Group') 
    ); 
    ?>

     

    function shuffle_assoc(&$array) { 
        $keys = array_rand($array, count($array)); 
        foreach($keys as $key) 
            $new[$key] = $array[$key]; 
            $array = $new; 
            return true; 
        } 

     

    Also there was this. I dont know if it related.

    <?php 
    $pattern = ' '; 
    $replace = '_'; 
    shuffle_assoc($answers[$num]); 
    foreach ($answers[$num] as $answer) { 
        $answer2 = str_replace($pattern,$replace,$answer); 
        echo "<li><input type=\"radio\" id=\"$answer2\" value=\"$answer2\" name=\"answers\" />\n"; 
        echo "<label for=\"$answer2\">$answer</label></li>\n"; 
    } 
    ?>

     

    Any help anyone could give me would be amazing. Thanks!

     

     

  2. Oooops that wasnt the code I changed at all. Thats the way it was sorry

    function showLeaders($file,$limit,$group = null) { 
    $leaders = array();
    
    
    	$xml = simplexml_load_file($file);
    	foreach($xml->user as $user) {
    		$name = (string)$user->name;
    		$percent = (string)$user->score;
    		$leaders[$name] = $percent;
    	}
    
    
    	arsort($leaders,SORT_NUMERIC);
    
    
    	$counter = 1;
    
    
    	$output = "<ul class=\"leaders\">\n";
    
    
    	foreach ($leaders as $key => $percent) {
    		// Check that $counter is less than $limit.
    		if ($counter <= $limit) {
    			if ($key == $_SESSION['user']) {
    				$output .= "<li><strong>$key:</strong> $percent%</li>\n";
    			} else {
    				$output .= "<li>$key: $percent%</li>\n";
    			}
    
    			if ($group) {
    				// Use the modulus operator(%) to create new sub-list.
    				if($counter % $group == 0) {
    					$output .= "</ul>\n<ul class=\"leaders\">\n";
    				}
    			}
    		}
    
    	$counter++;
    	}
    
    	$output .= "</ul>\n";
    
    
    echo $output;
    }

     

    Sorry about that. But yeah its still doing it.

  3. function showLeaders($file,$limit,$group = null) {
        $leaders = array();
       
    
        $xml = simplexml_load_file($file);
        foreach($xml->user as $user) {
            $name = (string)$user->name;
            $score = (string)$user->score;
            $leaders[$name] = $score;
        }
           
    
        arsort($leaders,SORT_NUMERIC);
        
        
        $counter = 1;
        
        
        $output = "<ul class=\"leaders\">\n";
        
    
        foreach ($leaders as $key => $value) {
           
            if ($counter <= $limit) {
                if ($key == $_SESSION['user']) {
                    $output .= "<li><strong>$key:</strong> $value/20</li>\n";
                } else {
                    $output .= "<li>$key: $value/20</li>\n";
                }
           
                if ($group) {
                    // Use the modulus operator(%) to create new sub-list.
                    if($counter % $group == 0) {
                        $output .= "</ul>\n<ul class=\"leaders\">\n";
                    }
                }
            }
        
        $counter++;
        }
        
        $output .= "</ul>\n";
        
        
        echo $output;
    }

     

    That is the code I changed.

  4. I changed all you said to the percent variable and it worked...sort of. It is now having a really really odd problem. When I do the test and complete it. I get my score as normal. For example I will so the test and it will say "your percentage was : 95%" thats great....then when it shows the leaderboard the score will show i got say 70% or something. One time i tried it and got 100% and then the leaderboard showed that i got 60%.

     

    So it is showing a percent...just the wrong one. Why on earth would it be doing this? Any idea why its giving me a seemingly random percent instead of the one i got.

  5. oh that wasnt the maths from it, I did that awful maths in my head on the spot as an example haha.

     

    I finally got it displaying a percentage. unfortunately my leaderboard still shows it as something/20. Any clue how I can make the leaderboard show the percentage?

  6. Ok I did get some code that shows a percentage. It shows it after the score out of 20 though. So it goes like " you scored 4/20 your percentage is 40" I am definatly moving in the right direction.

    What I am looking for now is for the mark out of 20 to not be shown at all and only the percentage is shown. Also the score out of 20 goes to a leaderboard what I want is the percentage to go to the leaderboard instead.

    I know I am a pain in the ass but Any help and I would be exstatic.

     

    I will provide the code here.

     

    This is the code in the test part that I belive will send it to the leaderboard and the one that shows the results.

    <?php } else {
    $file = "leaders.xml";
    $xml = simplexml_load_file($file);
    $user = $xml->addChild('user');
    $uname = $user->addChild('name',$_SESSION['user']);
    $uscore = $user->addChild('score',$_SESSION['score']);
    $xml->asXML("leaders.xml");
    
    echo "<h2 id=\"score\">{$_SESSION['user']}, your final score is:</h2>\n
    <h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>";
    
    //changed code
    $percent=$_SESSION['score']/20*100; //work out a percentage, divide the score by total an *100
    echo "<p>Your Percentage: ".$percent."</p>";
    
    
    if($_SESSION['score'] <= 5) echo "<p id=\"verdict\"><span>S</span>everely <span>H</span>indered <span>I</span>n the <span>T</span>est!</p>\n";
    if(($_SESSION['score'] > 5) && ($_SESSION['score'] <= 10)) echo "<p id=\"verdict\"><span>C</span>ould <span>R</span>ead <span>A</span>nd <span>P</span>ractice more.</p>\n";
    if(($_SESSION['score'] > 10) && ($_SESSION['score'] <= 15)) echo "<p id=\"verdict\"><span>A</span>cronyms a<span>R</span>e <span>S</span>o <span>E</span>asy!</p>\n";
    if($_SESSION['score'] > 15) echo "<p id=\"verdict\"><span>S</span>uper <span>A</span>cronym <span>S</span>pecialist</p>";
    echo "<p id=\"compare\"><a href=\"results.php\">See how you compare! <img src=\"images/arrow.png\" /></a></p>";
    }
    ?>

     

    This is the code in the XML file that is storing the the leader information

     

    <?xml version="1.0" encoding="UTF-8"?>
    <users>
      <user>
        <name>Bobby</name>
        <score>10</score>
      </user>
      <user>
        <name>Billy</name>
        <score>1</score>
      </user>
    </users>
    

     

    and this is the show leaders code from the functions.

    function showLeaders($file,$limit,$group = null) {
        $leaders = array();
       
        // Load the xml file and place all users and associated
        // scores into the 'leaders' array.
        $xml = simplexml_load_file($file);
        foreach($xml->user as $user) {
            $name = (string)$user->name;
            $score = (string)$user->score;
            $leaders[$name] = $score;
        }
           
        // Sort the leaders array numerically, highest scorers first.   
        arsort($leaders,SORT_NUMERIC);
        
        // Initialise our $counter variable to '1'.
        $counter = 1;
    
    // Start a html ordered list to hold the leaders.
        $output = "<ul class=\"leaders\">\n";
        
        // Loop through the 'leaders' array and wrap each username and score
        // in <li> tags. If the user is the current $_SESSION['user'], wrap
        // the name/score in <strong> tags too.
        foreach ($leaders as $key => $value) {
            // Check that $counter is less than $limit.
            if ($counter <= $limit) {
                if ($key == $_SESSION['user']) {
                    $output .= "<li><strong>$key:</strong> $value/20</li>\n";
                } else {
                    $output .= "<li>$key: $value/20</li>\n";
                }
                // Check to see if $group parameter has been passed.
                // If it has, create separate lists according to the $group variable.
                if ($group) {
                    // Use the modulus operator(%) to create new sub-list.
                    if($counter % $group == 0) {
                        $output .= "</ul>\n<ul class=\"leaders\">\n";
                    }
                }
            }
        // Increment the $counter. 
        $counter++;
        }
        // End the ordered list.
        $output .= "</ul>\n";
        
        // Print out the ordered list.
        echo $output;
    }
        

     

    You have all been really helpful so far and this could be me if I get this going. Any and all help would be INSANELY grateful it really would. Thank you!

     

     

  7. Ok I think I know what you were saying before. Instead of the marks going up by 1. I need to say find out what one percent of 20 is and make it go up by that right? Right now when the score goes up, it goes up by one at a time. I need it to go up by .20 or something. Problem is I cant seem to find whats making it go up by 1. Here is all the code from the test page. If ANYONE can help I will be insanely grateful.

     

    	$_SESSION['user'] = $username;
    	$_SESSION['score'] = 0;
    	$_SESSION['correct'] = array(); 
    	$_SESSION['wrong'] = array();
    	$_SESSION['finished'] = 'no';
    	if (isset($_SESSION['error']))
    	unset($_SESSION['error']); 
    	$num = 0;
    } else {
    	$random = rand(1,1000);
    	$_SESSION['user'] = 'Anon'. $random;
    	$_SESSION['score'] = 0;
    	$_SESSION['correct'] = array(); 
    	$_SESSION['wrong'] = array(); 
    	$_SESSION['finished'] = 'no';
    	$num = 0;
    }
    } else {
    $num = (int) $_POST['num'];
    $postedanswers = str_replace("_"," ",$_POST['answers']);
    if ($postedanswers == $answers[$num]['0']) {
    	$_SESSION['score']++;
    	$_SESSION['correct'][] = $postedanswers; 
    } else {
    	$_SESSION['wrong'][] = $postedanswers;
    } 
    if ($num < count($questions)-1) {
    	$num++;
    } else {
    	$last = true;
    	$_SESSION['finished'] = 'yes';
    }/php]
    
    [code=php:0]<?php } else { 
    $file = "leaders.xml";
    $xml = simplexml_load_file($file);
    $user = $xml->addChild('user');
    $uname = $user->addChild('name',$_SESSION['user']);
    $uscore = $user->addChild('score',$_SESSION['score']);
    $xml->asXML("leaders.xml");
    
    echo "<h2 id=\"score\">{$_SESSION['user']}, your final score is:</h2>\n
    <h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>";
    if($_SESSION['score'] <= 5) echo "<p id=\"verdict\"><span>S</span>everely <span>H</span>indered <span>I</span>n the <span>T</span>est!</p>\n";
    if(($_SESSION['score'] > 5) && ($_SESSION['score'] <= 10)) echo "<p id=\"verdict\"><span>C</span>ould <span>R</span>ead <span>A</span>nd <span>P</span>ractice more.</p>\n";
    if(($_SESSION['score'] > 10) && ($_SESSION['score'] <= 15)) echo "<p id=\"verdict\"><span>A</span>cronyms a<span>R</span>e <span>S</span>o <span>E</span>asy!</p>\n";
    if($_SESSION['score'] > 15) echo "<p id=\"verdict\"><span>S</span>uper <span>A</span>cronym <span>S</span>pecialist</p>";
    echo "<p id=\"compare\"><a href=\"results.php\">See how you compare! <img src=\"images/arrow.png\" /></a></p>";
    }
    ?>

  8. } else {

    $num = (int) $_POST['num'];

    $postedanswers = str_replace("_"," ",$_POST['answers']);

    if ($postedanswers == $answers[$num]['0']) {

    $_SESSION['score']++;

    $_SESSION['correct'][] = $postedanswers;

    } else {

    $_SESSION['wrong'][] = $postedanswers;

    }

    if ($num < count($questions)-1) {

    $num++;

    } else {

    $last = true;

    $_SESSION['finished'] = 'yes';

    }

     

    I took that dudes advice and looked on google and got some good code that does what I want. Problem is, it only makes a score out of 10. I want to change it to a percentage. I really am awful at PHP and could use a hand. I think that is the stuff making the answer, how could I make it display a percentage?

  9. You know something that with the selected answers will calculate someone's compatibility score with another person. I am having a little trouble even doing this part.

     

    Another part is I wanted to make it so that one person fills it out, then emails it to another person and they fill it out and it measures the comparability by meshing up the two sets of answers. Is this even possible. I am a Php beginner and so confused and frustrated. If anyone can give me ANY help at all. I really mean ANY I will be great full for the rest of my lift.

     

    Please and thank you!

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.