PHP - Php Quiz Doubt
Hi all,
Can anyone offer assistance with the following: I am trying to obtain a score from a users radio button selection on a simple quiz. i am not able to obtain the results.... Similar Tutorialswhats the diff between echo,print,print_r what's the difference between include(),require,require_once Hi All, being a noob when it comes to any sort of coding I am facing a strong learning curve but I'm getting there. With the implementation of our new client portal, we require a new client sign-up api...The following was provided... <?php ////////////////////////////////////////////////////// // THIS CODE IS PROVIDED AS AN EXAMPLE OF HOW TO // USE THE DITTO PORTAL FOR AHSAY API'S. ////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// // SET API VARIABLES ///////////////////////////////////////////////////////////////// //SET CUSTOMER ID AND API KEY $customerID = '8000003'; $key = '6NbTmZw5LZdtTO8FuYH9G'; //IF POSTBACK if ($_SERVER['REQUEST_METHOD'] == 'POST') { ///////////////////////////////////////////////////////////////// // GET VALUES FROM FORM ///////////////////////////////////////////////////////////////// $company = $_REQUEST['company']; $address1 = $_REQUEST['address1']; $address2 = $_REQUEST['address2']; $city = $_REQUEST['city']; $state = $_REQUEST['state']; $postal = $_REQUEST['postal']; $country = $_REQUEST['country']; $firstname = $_REQUEST['firstname']; $lastname = $_REQUEST['lastname']; $phone = $_REQUEST['phone']; $email = $_REQUEST['email']; $type = $_REQUEST['type']; $username = $_REQUEST['username']; $alias = $_REQUEST['alias']; $language = $_REQUEST['language']; $timezone = $_REQUEST['timezone']; ///////////////////////////////////////////////////////////////// // INVOKE LIST COUNTRIES API TO POPULATE COUNTRY DROP DOWN LIST ///////////////////////////////////////////////////////////////// //BUILD URL FOR API CALL $function = 'ListCountries'; $url = 'https://api.web-portal.co/?customerID=8000003&key=6NbTmZw5LZdtTO8FuYH9G&function=ListCountries'; //MAKE API CALL USING SIMPLE XML $countrylist = simplexml_load_file($url); foreach ($countrylist as $c) { if ($country == $c->countryID) { $countryOptions.="<OPTION VALUE=\"".$c->countryID."\" selected>".$c->countryName.""; } else { $countryOptions.="<OPTION VALUE=\"".$c->countryID."\">".$c->countryName.""; } } ///////////////////////////////////////////////////////////////// // INVOKE LIST LANGUAGES API TO POPULATE LANGUAGE DROP DOWN LIST ///////////////////////////////////////////////////////////////// //BUILD URL FOR API CALL $function = 'ListLanguages'; $url = 'https://api.web-portal.co/?customerID=8000003&key=6NbTmZw5LZdtTO8FuYH9G&function=ListLanguages'; //MAKE API CALL USING SIMPLE XML $languagelist = simplexml_load_file($url); foreach ($languagelist as $l) { if ($language == $l->languageID) { $languageOptions.="<OPTION VALUE=\"".$l->languageID."\" selected>".$l->languageName.""; } else { $languageOptions.="<OPTION VALUE=\"".$l->languageID."\">".$l->languageName.""; } } ///////////////////////////////////////////////////////////////// // INVOKE LIST TIMEZONES API TO POPULATE TIMEZONE DROP DOWN LIST ///////////////////////////////////////////////////////////////// //BUILD URL FOR API CALL $function = 'ListTimezones'; $url = 'https://api.web-portal.co/?customerID=8000003&key=6NbTmZw5LZdtTO8FuYH9G&function=ListTimezones'; //MAKE API CALL USING SIMPLE XML $timezonelist = simplexml_load_file($url); foreach ($timezonelist as $t) { if ($timezone == $t->timezoneID) { $timezoneOptions.="<OPTION VALUE=\"".$t->timezoneID."\" selected>".$t->timezoneName.""; } else { $timezoneOptions.="<OPTION VALUE=\"".$t->timezoneID."\">".$t->timezoneName.""; } } ///////////////////////////////////////////////////////////////// // BEFORE CALLING THE ADDCLIENT API, YOU SHOULD VALIDATE THE // USER'S INPUT (ALL REQUIRED FIELDS COMPLETE, EMAIL ADDRESS // ETC. ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// // INVOKE ADD CLIENT API TO ACTIVATE ACCOUNT ///////////////////////////////////////////////////////////////// //SET ACCOUNT TYPE AND TRIAL LENGTH $account = 'TRIAL'; $length = '14'; //BUILD URL FOR API CALL $function = 'AddClient'; $url = 'https://api.web-portal.co/?'. "customerID=".$customerID. "&key=".$key. "&function=".$function. "&company=".$company. "&address1=".$address1. "&address2=".$address2. "&city=".$city. "&state=".$state. "&postal=".$postal. "&country=".$country. "&firstname=".$firstname. "&lastname=".$lastname. "&phone=".$phone. "&email=".$email. "&type=".$type. "&username=".$username. "&alias=".$alias. "&language=".$language. "&timezone=".$timezone. "&account=".$account. "&length=".$length; //MAKE API CALL USING SIMPLE XML $createClient = simplexml_load_file($url); //DISPLAY RESULTS OF API CALL echo $createClient; } //NOT POSTBACK else { ///////////////////////////////////////////////////////////////// // INVOKE LIST COUNTRIES API TO POPULATE COUNTRY DROP DOWN LIST ///////////////////////////////////////////////////////////////// //BUILD URL FOR API CALL $function = 'ListCountries'; $url = 'https://api.web-portal.co/?customerID=8000003&key=6NbTmZw5LZdtTO8FuYH9G&function=ListCountries'; //MAKE API CALL USING SIMPLE XML $countrylist = simplexml_load_file($url); foreach ($countrylist as $c) { $countryOptions.="<OPTION VALUE=\"".$c->countryID."\">".$c->countryName.""; } ///////////////////////////////////////////////////////////////// // INVOKE LIST LANGUAGES API TO POPULATE LANGUAGE DROP DOWN LIST ///////////////////////////////////////////////////////////////// //BUILD URL FOR API CALL $function = 'ListLanguages'; $url = 'https://api.web-portal.co/?customerID=8000003&key=6NbTmZw5LZdtTO8FuYH9G&function=ListLanguages'; //MAKE API CALL USING SIMPLE XML $languagelist = simplexml_load_file($url); foreach ($languagelist as $l) { $languageOptions.="<OPTION VALUE=\"".$l->languageID."\">".$l->languageName.""; } ///////////////////////////////////////////////////////////////// // INVOKE LIST TIMEZONES API TO POPULATE TIMEZONE DROP DOWN LIST ///////////////////////////////////////////////////////////////// //BUILD URL FOR API CALL $function = 'ListTimezones'; $url = 'https://api.web-portal.co/?customerID=8000003&key=6NbTmZw5LZdtTO8FuYH9G&function=ListTimezones'; //MAKE API CALL USING SIMPLE XML $timezonelist = simplexml_load_file($url); foreach ($timezonelist as $t) { $timezoneOptions.="<OPTION VALUE=\"".$t->timezoneID."\">".$t->timezoneName.""; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <p>NEW CLIENT SIGN UP</p> <form id="NewClientForm" name="NewClientForm" method="post" action="NewClientForm.php"> <p>Company Name:<br /> <label for="company"></label> <input type="text" name="company" id="company" value="<? echo $company; ?>" /> </p> <p>Address 1:<br /> <label for="address1"></label> <input type="text" name="address1" id="address1" value="<? echo $address1; ?>" /> </p> <p>Address 2:<br /> <label for="address2"></label> <input type="text" name="address2" id="address2" value="<? echo $address2; ?>" /> </p> <p>City:<br /> <label for="city"></label> <input type="text" name="city" id="city" value="<? echo $city; ?>" /> </p> <p>State/Province:<br /> <label for="state"></label> <input type="text" name="state" id="state" value="<? echo $state; ?>" /> </p> <p>Zip/Postal Code:<br /> <label for="postal"></label> <input type="text" name="postal" id="postal" value="<? echo $postal; ?>" /> </p> <p>Country:<br /> <label for="country"></label> <select name="country" id="country"> <? echo $countryOptions; ?> </select> </p> <p>Primary Contact First Name:<br /> <label for="firstname"></label> <input type="text" name="firstname" id="firstname" value="<? echo $firstname; ?>" /> </p> <p>Primary Contact Last Name:<br /> <label for="lastname"></label> <input type="text" name="lastname" id="lastname" value="<? echo $lastname; ?>" /> </p> <p>Primary Contact Telephone:<br /> <label for="phone"></label> <input type="text" name="phone" id="phone" value="<? echo $phone; ?>" /> </p> <p>Primary Contact Email:<br /> <label for="email"></label> <input type="text" name="email" id="email" value="<? echo $email; ?>" /> </p> <p>Account Type:<br /> <input type="radio" name="type" id="acb" value="ACB" /> <label for="acb">ACB</label> <input type="radio" name="type" id="obm" value="OBM" /> <label for="obm">OBM</label> </p> <p>Preferred Username:<br /> <label for="username"></label> <input type="text" name="username" id="username" value="<? echo $username; ?>" /> </p> <p>Alias/Nickname:<br /> <label for="alias"></label> <input type="text" name="alias" id="alias" value="<? echo $alias; ?>" /> </p> <p>Language:<br /> <label for="language"></label> <select name="language" id="language"> <? echo $languageOptions; ?> </select> </p> <p>Timezone:<br /> <label for="timezone"></label> <select name="timezone" id="timezone"> <? echo $timezoneOptions; ?> </select> </p> <p> </p> <p> <input type="submit" name="submit" id="submit" value="Sign Up" /> </p> </form> <p><br /> </p> </body> </html> The following errors are produced... http://cubetech.com.au/downloads/signup.php Can please confirm my suspicions of simplexml_load_file() are not permitted. I have been reading about cURL implementation instead can someone provide some assistance if this is possible and assist. A lot of thank in advance, Andrew Chapman Friends, suppose iam copying 1 to 100 files using a loop in php & when the copy process reach at 50th file my browser went offline. My doubt is will it copy all 100 files ?? OR it will terminate at 50th file ? if i want to get all files copied if the browser went offline then what i want to do ?? Please clear I am trying to run tests on my pages and see how much time each one of em take to execute. For this purpose, i'm using this code which i got online: Code: [Select] <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; ?> <!-- Rest of the php and validation and form elements --> <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "This page was created in ".$totaltime." seconds"; ?> microtime() returns microseconds & secs since Unix epoch. So after exploding, why do we add them both? Cant we just use the microseconds part? I've tried this code & the one above & both return same time(almost). Code: [Select] <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[0]; $starttime = $mtime; ?> <!-- Rest of the php and validation and form elements --> <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "This page was created in ".$totaltime." seconds"; ?> Hello guys, i need to create a code for making connections with SSL servers (particularly SMTPS and POP3S) and i know about the existence of OpenSSL extension. However, i'm in doubt about a basic issue: 1. Is this extension useful for creating SSL clients or SSL servers? I'm not a SSL expert, so maybe i'm misunderstanding something; i hope you can help me. Kind regards. I'm not sure if this is exactly a coding help question, excuse me if its not. I want to know what is the best & secure way to submit a form to itself. I've tried to google the answer, but did not get a proper answer with explanation or may be I didnt use proper keywords to search it. Out of these which one do i use? Code: [Select] // Leave the action field empty. <form method="POST" action=""> //$PHP_SELF, also if i use echo $PHP_SELF my form does not work like it should. <form method="POST" action="<?$PHP_SELF?>"> //$_SERVER['PHP_SELF'], same problem as $PHP_SELF, it doesnt work if i use echo. <form method="POST" action="<?$_SERVER['PHP_SELF']?>"> I have a php code that call a process (a java program). The process runs in background for a so long time - from 1 minute to 2 hours. I make the process call using exec:
<?php function execInBackground($cmd) { if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd . " &> /dev/null &"); } } $command = "java ... ReadEmailsCmdLine 2>&1"; execInBackground($command); echo "FINALIZADO";Though the command runs in background it looks that the PHP code wait for the command completion to send a answer to client browser because I just see "FINALIZADO" after the java program finished its execution. Does someone know how I can solve this problem? Or at least does have a hint/pointer for that? Since now I thanks any comment/suggestion Hello. I try to write a simple multiple choice quiz. I have my questions and answers in a xml file and I read the file into an array. My main problems is how I'll connect the various sessions where the different questions appear in order to keep the score and sum it up in the end. Also, how do I check if the selected answer is correct or not? Assuming that the correct answers are in an array in php code, can I use javascript to check the answers? Any help? Thanks. can anyone please tell me how can I make a quiz in php which displays random questions one at a time
Hey guys,
I'm making php quiz, but got some problems with results getting - they are all wrong. Here is the db table for it.
CREATE TABLE IF NOT EXISTS `quiz` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `question` text COLLATE utf8_unicode_ci NOT NULL, `answer1` text COLLATE utf8_unicode_ci NOT NULL, `answer2` text COLLATE utf8_unicode_ci NOT NULL, `answer3` text COLLATE utf8_unicode_ci NOT NULL, `correctanswer` text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`), KEY `pollid` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=34 ;And script itself. $questionNumber = 1; $query = sql_query("SELECT * FROM quiz ORDER BY RAND() LIMIT $limit"); while ($result = mysqli_fetch_array($query)) { ?> <div class="padding"> <p> <?php echo "<b><h4>" . $questionNumber . ") " . $result['question'] . "</h4><br></b>"; ?> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value=""> <?php echo $result['answer1']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value=""> <?php echo $result['answer2']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value=""> <?php echo $result['answer3']; ?> <br> </p> <hr> </div> <?php $questionNumber +=1; } $correctAnswers = 0; $wrongAnswers = 0; $questions = ''; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT id, question, answer1, answer2, answer3, correctanswer FROM quiz WHERE id IN ($idList)"; $res = sql_query($sql) ; $qno = 1; while (list($id, $q, $a, $b, $c, $correct) = mysqli_fetch_row($res)) { if ($correct == $_POST['answer'][$id]) { $correctAnswers +=1; } else { $wrongAnswers +=1; } } echo $correctAnswer; echo $wrongAnswers; ?>Help me guys! Hi people. I'm building a quiz with one correct answer out of possible 4. My problem is that after I choose a random answer (which is the correct one) I have problems with choosing random 3 wrong answers which are not the same as answer 1 (they all come from the same table). My first mysql query gives me back 1 row my second mysql query gives me back 15 row (there were 16 - 1 which is the correct one). How can take only 3 rows out of that second query and output them into variables that I can later echo into a form? Can you help me understand this problem or can you propose an alternative method to deal with this? Here are my two queries: Code: [Select] // Fetch random answer (correct) $query = "SELECT * FROM psychofarm_brand WHERE `id`=".$randomNumber; $result = mysql_query($query); $row = mysql_fetch_array($result); // Fetch wrong answer $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); $row2 = mysql_fetch_array($result2); I hope this is clear. Thanks Hey all! I am new to these forums, but this seems like a really good place for information. I am a beginner in PHP coding, so I don't have that much experience with how to do quite simple tasks. I have a job to fulfill and I have to create a personality quiz, which basically means a user answers about eight questions with three choices and then he will be dubbed as one of six different personalities. I've tired doing this, believe me, but I just don't get it to work. Are there any websites, downloads, or words of advice you guys could give me? And no, I will not be using one of those quiz generators online; it should be made from raw code. Thank you so much! Hey, I want to start by saying this is the first time I've ever looked at PHP. I'm using a Wordpress plugin from http://www.netconcepts.com/wordpress-quiz-plugin/ The user answers a few questions, and based off that, they get a results sheet with their personality type. The only issue is that none of the line breaks that I've used in the results show up. It's just one big paragraph (even when in the editing stage, I've made it look all nice). There's no plugin support for this. I think it has something to do with this file maybe... "<?php /* Copyright (c) 2007 Andrew Shell (http://blog.andrewshell.org) and Netconcepts (http://www.netconcepts.com) WordPress Quiz Plugin is released under the GNU General Public License (GPL) http://www.gnu.org/licenses/gpl.txt */ class WQP_Factory { function add_quiz_type($type, $typename, $classname, $classpath) { if (!isset($GLOBALS['WQP_Factory']['QUIZ_TYPES']) || !is_array($GLOBALS['WQP_Factory']['QUIZ_TYPES'])) { $GLOBALS['WQP_Factory']['QUIZ_TYPES'] = array(); } $GLOBALS['WQP_Factory']['QUIZ_TYPES'][$type] = array( 'type' => $type, 'typename' => $typename, 'classname' => $classname, 'classpath' => $classpath ); } function get_quiz_types() { if (!isset($GLOBALS['WQP_Factory']['QUIZ_TYPES']) || !is_array($GLOBALS['WQP_Factory']['QUIZ_TYPES'])) { $GLOBALS['WQP_Factory']['QUIZ_TYPES'] = array(); } return $GLOBALS['WQP_Factory']['QUIZ_TYPES']; } function by_id($quiz_id) { require_once dirname(__FILE__) . "/wqp-quiz.php"; global $wpdb; $quiz_id = intval($quiz_id); $sql = "SELECT * FROM " . $wpdb->wqp_quizzes . " WHERE quiz_id = {$quiz_id} LIMIT 1"; $row = $wpdb->get_row($sql); $quiz_type = strtolower($row->quiz_type); $quiz_types = WQP_Factory::get_quiz_types(); if (empty($quiz_type)) { return null; } elseif (!isset($quiz_types[$quiz_type])) { die("Invalid Quiz Type: {$quiz_type}"); } $quiz_class = $quiz_types[$quiz_type]['classname']; $quiz_file = $quiz_types[$quiz_type]['classpath']; require_once $quiz_file; $quiz = new $quiz_class($row->quiz_id); $data = unserialize($row->quiz_data); $quiz->assign_all($data); return $quiz; } function by_type($quiz_type) { require_once dirname(__FILE__) . "/wqp-quiz.php"; $quiz_type = strtolower($quiz_type); $quiz_types = WQP_Factory::get_quiz_types(); if (!isset($quiz_types[$quiz_type])) { die("Invalid Quiz Type: {$quiz_type}"); } $quiz_class = $quiz_types[$quiz_type]['classname']; $quiz_file = $quiz_types[$quiz_type]['classpath']; require_once $quiz_file; $quiz = new $quiz_class(); $quiz->assign_defaults(); return $quiz; } } if (!function_exists('scandir')) { function scandir($directory, $sorting_order = 0) { if (!is_string($directory)) { user_error('scandir() expects parameter 1 to be string, ' . gettype($directory) . ' given', E_USER_WARNING); return; } if (!is_int($sorting_order) && !is_bool($sorting_order)) { user_error('scandir() expects parameter 2 to be long, ' . gettype($sorting_order) . ' given', E_USER_WARNING); return; } if (!is_dir($directory) || (false === $fh = @opendir($directory))) { user_error('scandir() failed to open dir: Invalid argument', E_USER_WARNING); return false; } $files = array (); while (false !== ($filename = readdir($fh))) { $files[] = $filename; } closedir($fh); if ($sorting_order == 1) { rsort($files); } else { sort($files); } return $files; } } // Escape output string function wqp_escape_string($value) { $escaped = $value; $escaped = wp_specialchars($escaped, true); return $escaped; } // Filter input string function wqp_filter_string($value) { $filtered = $value; $filtered = strip_tags($filtered); $filtered = stripslashes($filtered); return $filtered; } function wqp_safe_array(&$value, $default_value = array()) { if (!isset($default_value) || !is_array($default_value)) { $default_value = array(); } return (isset($value) && is_array($value) ? $value : $default_value); } function wqp_safe_value(&$value, $default_value = '') { return (isset($value) ? $value : $default_value); } function wqp_the_content($content) { preg_match_all( '!\[quiz=([0-9]+)\]!isU', $content, $matches ); foreach (array_keys($matches[0]) as $i) { $quiz = WQP_Factory::by_id($matches[1][$i]); if (!empty($quiz) && is_object($quiz)) { if (isset($_POST['answer']) && is_array($_POST['answer'])) { $content = str_replace($matches[0][$i], $quiz->fetch_answer($_POST['answer']), $content); } else { $content = str_replace($matches[0][$i], $quiz->fetch_quiz(), $content); } } } return $content; } function wqp_the_title($title) { preg_match_all( '!\[quiz=([0-9]+)\]!isU', $title, $matches ); foreach (array_keys($matches[0]) as $i) { $quiz = WQP_Factory::by_id($matches[1][$i]); if (!empty($quiz) && is_object($quiz)) { $title = str_replace($matches[0][$i], $quiz->get_title(), $title); } } return $title; } function wqp_get_query_template($template_dir, $type) { $template = ''; if (file_exists(TEMPLATEPATH . "/{$type}.php")) { $template = TEMPLATEPATH . "/{$type}.php"; } elseif (file_exists($template_dir . "/{$type}.php")) { $template = $template_dir . "/{$type}.php"; } return apply_filters("wqp_{$type}_template", $template); } function wqp_fetch_template($template_dir, $type, $params = null) { if (!empty($params) && is_array($params)) { extract($params); } $template = wqp_get_query_template($template_dir, $type); if (file_exists($template)) { ob_start(); include($template); $contents = ob_get_contents(); ob_end_clean(); } else { $contents = ''; } return $contents; } function wqp_plugins_loaded() { WQP_Factory::add_quiz_type('personality', 'Personality', 'WQP_Quiz_Personality', dirname(__FILE__) . '/quizzes/personality.php'); do_action('wqp_register_quiz_types');" I might have to add an nlbr somewhere or something??? Let me know how to proceed. Thanks!!! " Hello every body. Currently I am creating a quiz section but I'm facing some problem.
The thing i want is that the questions which the user has played before should not come again.
So here is my quiz table.
+-------------+-------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------------------------------+------+-----+---------+----------------+ | qid | int(11) | NO | PRI | NULL | auto_increment | | questions | varchar(255) | NO | UNI | NULL | | | correct_ans | varchar(50) | NO | | NULL | | | option1 | varchar(50) | NO | | NULL | | | option2 | varchar(50) | NO | | NULL | | | option3 | varchar(50) | NO | | NULL | | | category | enum('Maths','Bollywood','Special') | NO | | NULL | | +-------------+-------------------------------------+------+-----+---------+----------------+Then I have created another tablle where I stored their user id and the questions Iid they have played. Here is the table. +----------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+---------+------+-----+---------+-------+ | mid | int(11) | NO | PRI | NULL | | | quiz_questions | text | NO | | NULL | | +----------------+---------+------+-----+---------+-------+In the above table, the ids are stored in this pattern 1,5,10,6,7,11,15,29,2 ------------------> Tis are the quiz id. I'm using this query but to bring the uinque questions but still the same questions are comming.. $q = $this->database()->prepare("SELECT quiz_questions FROM quiz_played WHERE mid = ?"); $q->bindParam(1, $mid,PDO::PARAM_INT); $q->execute(); $row = $q->fetch(PDO::FETCH_ASSOC); $q->closeCursor(); $q1 = $this->database()->prepare("SELECT * FROM quiz WHERE qid NOT IN (?) ORDER BY RAND()"); $q1->bindParam(1, $genre,PDO::PARAM_STR); $q1->bindParam(1, $row['quiz_questions'], PDO::PARAM_STR); $q1->execute(); return $q1->fetch(PDO::FETCH_ASSOC);Any help will be really appreciated. i want online quiz in php mysql Hello everyone, I have a datatbase containing fields id, question, answer, and type. The questions are in true/false format, multiple choice and fill in the blank(text). At the moment, I am only trying to get the true and false questions to work. My code is as follows Code: [Select] <?php $connection = mysql_connect("localhost","root","root") or die("couldn't connect"); $select = mysql_select_db("login") or die ("cannot select database!"); $query = mysql_query("SELECT * FROM questions"); $numrows = mysql_num_rows($query); if ($numrows!=0) { //code to login while($row = mysql_fetch_assoc($query)) { $dbquestion = $row['question']; $dbanswer = $row['answer']; $dbtype = $row['type']; $dbid = $row['id']; $correctanswer = explode('|',"$dbanswer"); switch($dbtype){ case "mchoice": echo"<br/>"; echo $dbquestion; echo"<br/>"; break; case "boolean": echo"<br/>"; echo $dbquestion; echo"<br/>"; echo $correctanswer[0]; //display radio buttons ?> <form name="boolean" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>"> True <input name="question" type="radio" value= "True" /> False <input name="question" type="radio" value= "False" /> <input name="id" input type="hidden" value="<?php $dbid ?>" /> <input type="submit" name="submit" id="submit" value="Submit" /> <?php echo "The answer submitted was "; echo $_POST['question']; ?> <?php break; case "text": echo"<br/>"; echo $dbquestion; echo"<br/>"; //display text box break; } } } ?> The problem I'm having is that this creates a submit button for each question. When choosing true or false for the answer and pressing submit, the answer is selected for every question. I need the code to look at what the user has entered, check the answer against correctanswer[0]. I'm really in a bit of a hole here and if anyone can help me it would be greatly appreciated. Many thanks in advance James Ok this is kind of a logical help which I need. I am writing a quiz script and I need to send and receive data from database on a timeout basis and I also want that when time finishes (of quiz) user must be moved to some other page. I basically need timeout based function if there are any and real time retrieval and storage of data. Thanks a lot I've made a code here.. It can create a quiz. It consist of a question and a 4 choices.. But my problem here is. when i inserted only 2 values for the choices, there would still be 4 radio button(Choices). Can someone help me. i want my code to be like : When user inputs 4 values. there would be 4 choices, but when user insert 2 values, there would be 2 values.. Here's my code for the form Code: [Select] <form name="formupload" method="post" action="setdate.php"> <div id="apDiv10"><input type="image" src="images/setdate.png" name="Image9" width="170" height="35" border="0" ></div> </form><form name="formupload" method="post" action="valdeactiveass.php"> <div id="apDiv8"><input type="image" src="images/deacass.png" name="Image9" width="170" height="35" border="0" ></div> </form><form name="formupload" method="post" action="valactiveass.php"> <div id="apDiv7"><input type="image" src="images/activeass.png" name="Image9" width="170" height="35" border="0" ></div> </form><form name="formupload" method="post" action="quiz_preview.php"> <div id="apDiv3"><input type="image" src="images/preview.png" name="Image9" width="170" height="35" border="0" ></div> </form> <form name="form2" method="post" action="delques.php"> <div id="apDiv9"><input type="image" src="images/delques.png" name="Image8" width="170" height="35" border="0" ></div> </form><form name="formupload" method="post" action="validatequiz.php"> <input type="text" style="position:absolute; top:160px; left:101px; width: 576px; height: 31px; font-size:10px;" name="question" id="question" /> <input type="text" style="position:absolute; top:244px; left:109px; width: 567px; height: 19px; font-size:10px;" name="c1" id="c1" /> <input type="text" style="position:absolute; top:297px; left:108px; width: 567px; height: 19px; font-size:10px;" name="c2" id="c2" /> <input type="text" style="position:absolute; top:350px; left:109px; width: 567px; height: 19px; font-size:10px;" name="c3" id="c3" /><input type="text" style="position:absolute; top:396px; left:110px; width: 567px; height: 19px; font-size:10px;" name="c4" id="c4" /> <select style="position:absolute; top:463px; left:309px; width: 80px; height: 23px; font-size:10px;" name="answer" id="answer"> <option value="Select">Select</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select><div id="apDiv2"><input type="image" src="images/submit.png" name="Image8" width="170" height="35" border="0" ></div> </form> And this of the quiz maker.. Code: [Select] <?php if (isset($_POST['sent'])) { for ($i=0;$i<count($questions);$i++) { if ($_POST['q2'.$i]=="w") { $score++; } else { } } $percent = number_format(($score/count($questions))*100,2,".",","); echo("<br>".$score." out of ".count($questions)." (".$percent."% right)<br>\n"); } else { echo("<form action=\"#\" method=\"post\">\n"); echo("<input type=\"hidden\" name=\"sent\">\n"); for ($i=0;$i<count($questions);$i++) { echo("<b>".$questions[$i][0]."</b><br><br>\n"); if ($questions[$i][5]==1) { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][1]."<br>\n"); } else { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][1]."<br>\n"); } if ($questions[$i][5]==2) { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][2]."<br>\n"); } else { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][2]."<br>\n"); } if ($questions[$i][5]==3) { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][3]."<br>\n"); } else { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][3]."<br>\n"); } if ($questions[$i][5]==4) { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"c\"> ".$questions[$i][4]."<br><br>\n"); } else { echo("<input type=\"radio\" name=\"q2".$i."\" value=\"w\"> ".$questions[$i][4]."<br><br>\n"); } } echo("<input type=\"submit\" value=\"Submit Exam\"></form>"); } ?> Hello, I am trying to build a quiz with PHP and MySQL. I have successfully managed to build the database and display the questions from the database. I have also managed to get the radio button to individually map to each question (rather than seeing all questions with one possible answer). What I have been unable to do (for some days now) is to match the correct radio button with the answer (all say incorrect at the moment even if you answer correctly.) Any help would be greatly appreciated. Code below questions.php Code: [Select] <?php require_once('connectvars1.php'); //Connect to the database $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name); $query = "SELECT * FROM questions"; $result = mysqli_query($dbc, $query); // if records are present if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result); if (mysqli_num_rows($result) > 0) { // print answer list as check boxes while ($row = mysqli_fetch_array($result)) { $id = $row['autokey']; $quest = $row['q_text']; $option1 = $row['op1']; $option2 = $row['op2']; $option3 = $row['op3']; $option4 = $row['op4']; $answer= $row['answer']; echo "$quest"; ?> <br /> <input type= radio name= <?php echo "$id" ?> <value="A"/>A. <?php echo "$option1" ?><br /> <input type= radio name= <?php echo "$id" ?> <value="B"/>B. <?php echo "$option2" ?><br /> <input type= radio name= <?php echo "$id" ?> <value="C"/>C. <?php echo "$option3" ?><br /> <input type= radio name= <?php echo "$id" ?> <value="D"/>D. <?php echo "$option4" ?><br /> <br /> <?PHP } ?> <br /> <div align="center"><input type="submit" name="btn_submit" value="Submit Answers" /></div> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php } echo '</form>'; } mysqli_close($dbc); ?> score.php Code: [Select] <?php // Check answers if((isset($_POST['$id'])) && ($_POST['$id'] == "$answer")){ echo 'Your answer was correct for q1!'; }else{ echo 'Your answer was incorrect for q1.'; } ?> <br /> <?php if((isset($_POST['$id'])) && ($_POST['$id'] == "$answer")){ echo 'Your answer was correct for q2!'; }else{ echo 'Your answer was incorrect for q2.'; } ?> |