PHP - Question About A Quiz Script
Hi,
My name is Karel van der Veen. I am trying to creat a script for a quiz on my website but I am stuck. I was hoping you might have an idea for the solution. The idea is a page with a picture and multiple choice questions about that picture. For example a picture of a bike. Question 1: what colour is the bike? --> choices are green, red, blue. Question 2: what shape are the wheels? --> round, square. I have made a script that compares the answers given by the student, with the correct answers I put in the database corresponding with the bike on the picture. If the student has it right, than after clicking on "next", the "correct answer" page will load. If he made a mistake a "wrong answer" page will load. So far the script works. But what I would like to happen is if the student gives a wrong answer, that after clicking on the `next` button, he will go back to the front page where the picture and the questions are with a sign or mark around the question he answered wrong, so he knows what question he had wrong and has to answer again. Below is the script I have so far. Do you know what I have to change or to add to the script? regards Karel Code: [Select] script of the question page: <?php $array_vorm = Array("rond","vierkant"); $array_kleur= array("rood","blauw","groen"); $array_grootte = array("4x4","8x8"); ?> <form method="post" action="results.php"><br /> <div id="vorm"> <div id="vorm1"><img src="images/vorm.gif" alt="vorm" width="75" height="75" class="vorms" /></div> <div id="vorm2"> <table> <tr><td> <label class="vormhead">VORM</label><br> <select name="form_vorm[]" class="randmenu" size="4" multiple > <option><?php echo implode("</option><option>",$array_vorm); ?></option> </select> </td></tr> </table> </div> </div> <div id="kleur"> <div id="kleur1"><img src="images/kleur.gif" alt="vorm" width="75" height="75" class="kleur" /></div> <div id="kleur2"> <table> <tr><td> <label class="kleurhead">KLEUR</label><br> <select name="form_kleur[]" class="randmenu" size="4" multiple > <option><?php echo implode("</option><option>",$array_kleur); ?></option> </select> </td></tr> </table> </div> </div> <div id="grootte"> <div id=" grootte 1"><img src="images/ grootte .gif" alt="vorm" width="75" height="75" class=" grootte " /></div> <div id=" grootte2"> <table> <tr><td> <label class=" grootte head">GROOTTE</label><br> <select name="form_ grootte []" class="randmenu" size="4" multiple > <option><?php echo implode("</option><option>",$array_ grootte ); ?></option> </select> </td></tr> </table> </div> </div> <input type="submit" class="zoekbalk" value="zoeken" /> And this is the script of the result page: <?php error_reporting ( E_ALL | E_STRICT ); ini_set ( 'display_errors', 1 ); ?> <?php include 'connect.php'; ?> <?php $geenbeschrijving = "De beschrijving is onjuist. $geenselectie = "Je hebt geen selectie gemaakt, maak eerst een selectie en klik dan op zoeken. <br/><br/> $antwoord = "Antwoord: "; $diagnose = "Beschrijving: "; $diagnose2 = "Diagnose: "; $vormeggo = "Vorm: "; $kleureggo = "Kleur: "; $grootteeggo = "Grootte: "; $array_vorm = array("rond","vierkant"); $array_kleur= array("rood","blauw","groen"); $array_grootte = array("4x4","8x8"); if($_SERVER['REQUEST_METHOD'] == 'POST' ) { $query = "select * from foto1 where "; $query_init = $query; $first_option = true; if (isset($_POST["form_vorm"])){ if ($first_option == false){ $query .= " AND "; } else{ $first_option = false; } $query .= "("; for ($i=0; $i<=count($_POST["form_vorm"])-1; $i++){ if ($i == 0){ $query .= "(vorm LIKE '%".$_POST["form_vorm"][$i]."%') "; }else{ $query .= " OR (vorm LIKE '%".$_POST["form_vorm"][$i]."%') "; } } $query .= ")"; } if (isset($_POST["form_ kleur "])){ if ($first_option == false){ $query .= " AND "; } else{ $first_option = false; } $query .= "("; for ($i=0; $i<=count($_POST["form_ kleur "])-1; $i++){ if ($i == 0){ $query .= "( kleur LIKE '%".$_POST["form_ kleur "][$i]."%') "; }else{ $query .= " AND (kleur LIKE '%".$_POST["form_ kleur "][$i]."%') "; } } $query .= ")"; } if (isset($_POST["form_ grootte "])){ if ($first_option == false){ $query .= " AND "; } else{ $first_option = false; } $query .= "("; for ($i=0; $i<=count($_POST["form_ grootte "])-1; $i++){ if ($i == 0){ $query .= "( grootte LIKE '%".$_POST["form_ grootte "][$i]."%') "; }else{ $query .= " AND (grootte LIKE '%".$_POST["form_ grootte "][$i]."%') "; } } $query .= ")"; } if ($query == $query_init){ echo "<div class=\"geenbeschrijving\">" . $geenselectie . "</div>"; }else{ //echo $query."<br><br>"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)<>0) { while ( $row = mysql_fetch_assoc ( $result ) ) { echo "<div class=\"beschrijving\">" . $antwoord . "</div>"; echo "<div class=\"keuze\">" . $vormeggo . $row["VORM"]. "</div>"; echo "<div class=\"keuze2\">" . $kleureggo . $row["KLEUR"]. "</div>"; echo "<div class=\"keuze2\">" . $grootteeggo . $row["GROOTTE"]. "</div>"; </div>"; $beschrijvingquery = "select BESCHRIJVING from beschrijving where Catalog_ID=".$row["ID"]; if ($beschrijvingresult = mysql_query($beschrijvingquery)) { if (mysql_num_rows($beschrijvingresult)<>0) { while ( $beschrijvingrow = mysql_fetch_assoc ( $beschrijvingresult ) ) { echo "<div class=\"beschrijving\">" . $diagnose . "</div>"; echo "<div class=\"beschrijving2\">" . $beschrijvingrow["BESCHRIJVING"]."</div></br>"; } } else { echo "geen beschrijving<br />"; } } $diagnosequery = "select DIAGNOSE from diagnose where Catalog_ID=".$row["ID"]; if ($diagnoseresult = mysql_query($diagnosequery)) { if (mysql_num_rows($diagnoseresult)<>0) { while ( $diagnoserow = mysql_fetch_assoc ( $diagnoseresult ) ) { echo "<div class=\"beschrijving\">" . $diagnose2 . "</div>"; echo "<div class=\"beschrijving2\">".$diagnoserow["DIAGNOSE"]."<br /><br />"; } } else { echo "geen beschrijving<br />"; } } $fotoquery = "select FOTOthumb, FOTObig from foto where Catalog_ID=".$row["ID"]; if ($fotoresult = mysql_query($fotoquery)) { if (mysql_num_rows($fotoresult)<>0) { while ( $fotorow = mysql_fetch_assoc ( $fotoresult ) ) { echo "<div class=\"beschrijving4\">" .'<a title="' . $fotorow['FOTObig'] . '" href="images/big/' . $fotorow['FOTObig'] . '" rel="lightbox"><img src="images/thumb/' . $fotorow['FOTOthumb'] . '"></a></br></br><hr><hr> '; } } else { echo "<img src='geenfoto.jpg' alt='geen foto' /><br />"; } } } } else { echo "<div class=\"geenhuidaandoeningen\">" . $geenhuidaandoeningen . "</div>"; } } } } ?> Similar TutorialsI am creating a quiz, with Multiple choice, true and false, and fill in the blanks questions.
The first 2 types are working fine, and I am working on tesing the fill the blank questions...
See code:
//QUESTION 9 if (empty($_POST["ix"])) { $ixErr = "Please select an answer."; } else { $ix = test_input($_POST["ix"]); // check if question only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$ix)) { $ixErr = "Only letters and white space allowed."; else ($ix == "charming") { $score = $score+10; echo "<p>9.) " . $ix . " is correct.</p>\n"; } else { echo "<p>9.) " . $ix . " is incorrect.</p>\n"; } } }First, I am testing to see if it is empty, if yes, display error, otherwise use the test_input function on the entered information. Then I want to test and verify that the entered data only contains characters and/or blank spaces, if that returns yes, then I want to test the content for the correct answer. But for some reason the code gets stuck upon testing for the correct answer, please note: I did try elseif, but same issue, please help, thank you. He, I am builing some kind of quiz script. I got 10 images people see and they have to say if they agree or not. Simply by clicking yes / no. First i used a submit button, but now i want it to submit whem people click yes/no. tried some stuff already but cant find the correct way. Please help here is the code: <?php session_start(); $thispage = 'test.php'; $img = array( '1','2','3','4','5','6','7','8','9','10' ); function getQuestion( $q ) { global $img; $curImg = $img[$q]; return "Do you agree? <br> <img src=\"images/{$curImg}.jpg\">"; } if ( isset( $_GET['reset'] ) ) { unset( $_SESSION['q'] ); unset( $_SESSION['answer'] ); header("Location: {$thispage}"); } if ( !isset( $_SESSION['q'] ) ) { $_SESSION['q'] = 0; } if ( !isset( $_SESSION['answer'] ) ) { $_SESSION['answer'] = array(); } if ( isset( $_POST['answer'] ) ) { $answer = $_POST['submit']; if ( $answer == '1' || $answer == '2' ) { $next = $_SESSION['q'] + 1 ; if ( array_key_exists( $next,$img ) ) { $_SESSION['answer'][$_SESSION['q']] = $answer; } $_SESSION['q'] = $next ; } } if ( $_SESSION['q'] >= count( $img ) ) { $html = ''; foreach( $_SESSION['answer'] as $q => $value ) { $html .= getQuestion( $q ) . " Answer: {$value}<br />"; } $html .= "<br /><br /><a href=\"{$thispage}?reset=Y\">Reset Quiz</a>"; } else { $question = getQuestion( $_SESSION['q'] ); $html =<<<HTML <html> <body> <h1>{$question}</h1> <input type="submit" value="Yes" name="answer" />Yes<br /> <input type="submit" value="No" name="answer" />No<br /> </form> </body> </html> HTML; } echo $html; ?> This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=315539.0 This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=315590.0 I am pretty new to using php and mysql and am struggling to understand how to go about processing a multiple choice quiz script to check if the answer given by the user is right or wrong and give the user the final result . I understand how to write and process register pages and login scripts and the like but for some reason I just can't understand what i need to do for this type of script So far my thinking has been 1) When the page is submitted have a query that compares the answer selected to the answer in the database. 2) Have php variables for correct and and incorrect both initialised to 0 and which will change depending on the answers given. 3) Use an if else statement similar to the code below to add up the score : - if($result['answer']==$_POST[$result['id']]){ $correct_answer++; }else{ $incorrect_answer++; } 4) Have a query that enters the person taking the quiz's grade in to the database. I would really appreciate it someone could give me some guidance for the best way to do this. I have included below my code for selecting the questions form the database. <?php // Starts the session session_start(); /* */ if (!isset($_SESSION['user_level']) or ($_SESSION['user_level'] != 0)) { header("Location: error_page.php"); exit(); } ?> <?php require_once ('mysqli_connect.php'); $email_address = ['email_address']; $quizGradeBegan=("INSERT INTO learner_grade_db( email_address, score, )VALUES ('$email_address',0) "); $result=@mysqli_query($dbcon, $submittedAnswerQuery); if(isset($_SESSION['email_address'])){ ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Q-Quiz: Learner Homepage</title> <link rel="stylesheet" href="sdnt.css" /> </head> <body> <div class="wrapper"> <!-- Start of wrapper div --> <div class="header"> <!-- Start of header div --> <h1>e-Quiz: <br> Learner Homepage</h1> <?php //Makes a call to includes/leaner_nav.php in order to show The Learner Navigation Bar require_once("includes/learner_nav.php"); //Makes a call to includes/welcome_name.php in order to show the Learners name. require_once("includes/welcome_name.php"); /*Makes a call to includes/current_date in order to shows the current date. Will appear in the Day Month Year Format.*/ require_once("includes/current_date.php"); ?> <!-- Shows the Change text size images --> <div id="textSizer"><!-- Start of the textSizer div --> <img id="smallStyler" class="textSize" src="imgs/fontSmall.jpg" height="18" width="18" alt="Smallest" /> <img id="mediumStyler" class="textSize" src="imgs/fontMedium.jpg" height="18" width="18" alt="Default" /> <img id="largeStyler" class="textSize" src="imgs/fontLarge.jpg" height="18" width="18" alt="Biggest" /> </div> <!-- End of textsizer div --> </div><!-- End of Header div --> <div class="article"> <!-- Start of the article div --> <?php $query=("SELECT question_id, question, distractor_1, distractor_2, distractor_3, distractor_4 FROM multiplechoice_db ORDER BY RAND() LIMIT 3 " ); $result = @mysqli_query ($dbcon, $query); // Run the query. while($row = mysqli_fetch_array($result, MYSQL_BOTH)):?> <br> <form action="gradepage.php" method="post"> <?php echo $row['question_id'];?> <br> <?php echo $row['question'];?> <br> <input type="radio" value="1" id=' distractor <?php echo $row['question_id'];?>' name ='<?php echo $row['question_id'];?>'/><?php echo $row['distractor_1'];?> <br> <input type="radio" value="2" id=' distractor <?php echo $row['question_id'];?>' name ='<?php echo $row['question_id'];?>'/><?php echo $row['distractor_2'];?> <br> <input type="radio" value="3" id=' distractor <?php echo $row['question_id'];?>' name ='<?php echo $row['question_id'];?>'/><?php echo $row['distractor_3'];?> <br> <input type="radio" value="4" id=' distractor <?php echo $row['question_id'];?>' name ='<?php echo $row['question_id'];?>'/><?php echo $row['distractor_4'];?> <br> <?php endwhile;?> <p> <input id="submit" type="submit" name="submit" value="Click To Submit Completed Quiz "></> </div> <!-- End of the article div --> <div class="aside"> <!-- Start of the aside div --> <ul class="nav2"> <?php //Makes a call to includes/leanersidebar1.php in order to show The Learner Sidebar require_once("includes/learnersidebar1.php"); ?> </ul> <br><br><br><br><br> </div> <!-- End of the aside div --> <div class="footer "><!-- Start of the footer div --> <?php //Makes a call to includes/footer.php in order to show the Footer require_once("includes/footer.php"); ?> </div><!-- End of the footer div --> </div><!-- End of the wrapper div --> </body> <html> so a user fills out a form, data gets sent to send.php then if successful redirects back to index.php, how on that index.php can i get it to say, sent successfully? Hey, So I have a couple of files, and I'm trying to create a login script. There is a MySQL query that accesses a database with a list of usernames and passwords. I have a feeling something is wrong with my SQL query, because it's not working correctly. Code: [Select] <?php $connect = mysql_connect("localhost", "root", "root"); if(!$connect){//If user can't connect to database die('Could not connect: ' . mysql_error()); //Throw an error } mysql_select_db("colin_db", $connect); //Get given username and password from username field and password field $givenUsername = $_POST["usernameField"]; $givenPassword = $_POST["passwordField"]; $myQuery = "SELECT * FROM ADMINS WHERE USERNAME = $givenUsername AND PASSWORD = $givenPassword"; $queryResult = mysql_query($myQuery); $numRows = mysql_num_rows($queryResult); if($numRows == 1){ //If the details are correct... //Reload the page and login echo "<script type = 'text/javascript'> window.location.reload() </script>"; echo "Details correct"; } elseif($numRows == 0){ //Else if the details are not found //Display error accordingly echo "Details not correct!"; //This is what happens every time } mysql_close($connect); ?> The database is configured correctly, but I'm not sure how to correctly create a SQL query to determine if the given username and password are correct. In case you'd like to see it, the segment from the index.php file is below. Code: [Select] <form action = "login.php" method = "POST"> Admin Login: <br> Username: <input type = "text" name = "usernameField"/><br> <!-- Password field--> Password: <input type = "password" name = "passwordField"/><br> <!-- Username field --> <input type = "submit" value = "Login" name = "submitButton"/> <!-- Login button --> </form> Any ideas? Thanks, Jake I am needing help designing password recover script. This script must include a password recovery script. I am needing for the person to reset there password, need to first answer a form named: Your Email. If the email is found, it goes to another form that asks for the secret answer. Then it goes to a form that asks the answer of there secret question that they entered when they first signed up. If the answer is correct it updates the database table value of the password with a random string generated with: abcdefghijklmnopqrstuvwxyz023456789 And it also, sends that password to the email provided. What I am wanting to know, is how would the code with the if statements would be structured like and the column types and names. Or if anyone has ever created something like this, and would be glad to post a snippet of there code would be great. I have a install.php file that I created. It asks for the MySQL user, pass, hostname and database they want to use. When the script is run, the tables are created and the MySQL info is inserted into a connection table. Now the tricky part. On each page that needs to connect to the MySQL database, I have an include, which contains the following: Code: [Select] define ('DB_USER', ' '); define ('DB_PASS', ' '); define ('DB_HOST', ' '); define ('DB_NAME', ' '); $dbc = mysql_connect (DB_HOST, DB_USER, DB_PASS) OR die ('Cannot connect to MySQL server.'); mysql_select_db (DB_NAME) OR die ('Cannot connect to database.'); How do I tell the database connection script what the variables are from the connections table? Yeah, I know I can manually put them in there, but the point of the script was to make it so the user does not have to go into the code. Hello, I am slightly nervous about posting this because I am almost completely new to php, I have a few introductory books on the subject which I am working through at the moment as well as some reference books but I am still getting through the basics of it all. I recently downloaded a login script, which allows a user to login and also allows the protection of some pages if users are not logged in. This script was a free one from easykiss123. it comes with other .php files and I have given them all a look over and I get the general idea of what's going on for the most part, and I THINK as I keep reading my books I will understand everything even more. However, what I really want to do right now is make it so a website would know which user is logged on, and then use this information elsewhere. For example if a particular user logged on and submitted something, I would like obviously the submission to be recorded but also the id of the user that submitted it, at the moment with this code, I do not think that is possible, however I could be wrong. I am looking for any pointers or a nudge in the right direction or link to a tutorial of how I would go about this, anything that may help. I think I would be storing the user ID in a global variable that can be used throughout the site, but again I am not sure. Thanks in advance for any help, I have included both the login script and the script used for protecting pages, as its already freely available online I see no issue with posting snippits of it here since the source has been referenced. Code: [Select] <?php # Script 16.8 - login.php // This is the login page for the site. require_once ('includes/config.inc.php'); $page_title = 'Login'; include ('includes/header.html'); if (isset($_POST['submitted'])) { require_once (MYSQL); // Validate the email address: if (!empty($_POST['email'])) { $e = mysqli_real_escape_string ($dbc, $_POST['email']); } else { $e = FALSE; echo '<p class="error">You forgot to enter your email address!</p>'; } // Validate the password: if (!empty($_POST['pass'])) { $p = mysqli_real_escape_string ($dbc, $_POST['pass']); } else { $p = FALSE; echo '<p class="error">You forgot to enter your password!</p>'; } if ($e && $p) { // If everything's OK. // Query the database: $q = "SELECT user_id, first_name, user_level FROM users WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (@mysqli_num_rows($r) == 1) { // A match was made. // Register the values & redirect: $_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC); mysqli_free_result($r); mysqli_close($dbc); $url = BASE_URL . 'index.php'; // Define the URL: ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } else { // No match was made. echo '<p class="error">Either the email address and password entered do not match those on file or you have not yet activated your account.</p>'; } } else { // If everything wasn't OK. echo '<p class="error">Please try again.</p>'; } mysqli_close($dbc); } // End of SUBMIT conditional. ?> <h1>Login</h1> <p>Your browser must allow cookies in order to log in.</p> <form action="login.php" method="post"> <fieldset> <p><b>Email Address:</b> <input type="text" name="email" size="20" maxlength="40" /></p> <p><b>Password:</b> <input type="password" name="pass" size="20" maxlength="20" /></p> <div align="center"><input type="submit" name="submit" value="Login" /></div> <input type="hidden" name="submitted" value="TRUE" /> </fieldset> </form> <?php // Include the HTML footer. include ('includes/footer.html'); ?> Code: [Select] <?php require_once ('includes/config.inc.php'); $page_title = 'YOUR PAGE TITLE GOES HERE'; // Start output buffering: ob_start(); // Initialize a session: session_start(); // Check for a $page_title value: if (!isset($page_title)) { $page_title = 'User Registration'; } // If no first_name session variable exists, redirect the user: if (!isset($_SESSION['first_name'])) { $url = BASE_URL . 'index.php'; // Define the URL. ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } ?> Code: [Select] <?php // Flush the buffered output. ob_end_flush(); ?> Hi, I am very new to PHP (and web developing generally, as well). I have been passing through two different ways of verifying user input in a PHP webpage: 1- By calling a different script file, where the verification logic code is listed and then recall the referer $_SERVER['HTTP_REFERER'] page and pass the result using $_SESSION. (I understood this is basically done to avoid repeating the action in the code with every refreshment of the browser window). 2- By enclosing the verification logic code withing the same PHP page, so the page is a big mix of HTML & PHP. (I understood this is basically done, in order to keep the user input without using $_SESSION and it should save one trip of data transfer.) For me, I see both are working; still I want to learn the best coding practices. So your advice is appreciated, and please feel free to correct my, if I missed something about both methods. I have been trying to insert data into my database thru php script with no luck. I think its has to do with not being able access my database. My question is when I use MySQL command client and enter the password and get mysql> does that mean I can use that password to access my databases as long as I point to the correct database with the mysqli_connect function. I dont think I fully understand how this works. I have everything stored on my local computer and I use "localhost" in mysqli_connect function. Hello, I'm trying to take the value from an HTML form and insert it into a database on a button click. It inserts a null value into the database. The script is called submitColumnDetails.php. This is where I create the text field that I want to take the information from. This is in a separate file. Code: [Select] echo <<<END <form action = "submitColumnDetails.php" method = "POST"> <input type = "text" name = "columnField"/> </form> END; This is the submitColumnDetails.php file Code: [Select] <?php //Submit Column Data //-----------------------------------------------------// //Connect to localhost server $connector = mysql_connect("localhost", "root", "root"); if(!$connector){ //If user can't connect to database die('Could not connect: ' . mysql_error()); //Throw an error } //-----------------------------------------------------// mysql_select_db("colin_db", $connector); $newValue = $_POST["columnField"]; //Data from column field. THIS IS WHAT RETURNS NULL $newColumnQuery = "INSERT INTO `colin_db`.`allColumnFields` (`DATA`) VALUES ('$newValue')"; mysql_query($newColumnQuery); //Query to add form to main table $newColumnIntoMainTableQuery = "ALTER TABLE colin_table ADD ('$newValue' varchar(50))"; mysql_query($newColumnIntoMainTableQuery); //Query to add column to main table mysql_close($connector); //Close database connection echo "<script type = 'text/javascript'> window.location = 'index.php'</script>"; //Go back to original page ?> Even when I print out the $newValue, it does not print anything. What am I doing incorrectly? I wrote this basic script yesterday to process and generate a Google Site Map. And it works! BUT I want to advance this script to accommodate for something else and I don't know the correct path to take from here, but I will tell you what I've found out so far.. Current Situation: 1 - Currently my below script generates urls in the site map like: http://abcdefg.com/index.php?dispatch=products.view&product_id=29826 2 - I have .htaccess configured to rewrite the urls to the products name data like: http://abcdefg.com/pennies/wheat-pennies/lincoln-wheat-penny-cent.html (just an example) and these urls are ONLY active if clicking on the site links themselves - meaning if I enter: http://abcdefg.com/index.php?dispatch=products.view&product_id=29826 directly into the url, the url does not resolve to this natural friendly url name. What Id like to achieve (which I don't know what direction I should be looking!): - I'd like my xml output urls (as current) to be written in the natural format (as in #2 above). FYI here is a current example output item in my sitemap: Code: [Select] <url> <loc>http://abcdefg.com/index.php?dispatch=products.view&product_id=29803</loc> <changefreq>weekly</changefreq> <lastmod>2010-09-24T08:00:00+04:00</lastmod> </url> Can anyone give me some guidance on what method might work for this? Do you think it's more a mod_rewrite issue? Or can this be handled easier with straight up modifications to my below? I'm just a bit confused on what direction I should be looking.. Thanks for any input. <?php header("Content-Type: text/xml;charset=iso-8859-1"); echo '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; //include('config.local.php'); $cxn = mysqli_connect($config['db_host'], $config['db_user'], $config['db_password'], $config['db_name']); $query = "SELECT cscart_product_descriptions.product_id, cscart_products.product_id, cscart_products.timestamp FROM cscart_product_descriptions JOIN cscart_products ON cscart_product_descriptions.product_id = cscart_products.product_id WHERE cscart_products.status='A' LIMIT 10000"; $result = mysqli_query($cxn, $query); $row = mysqli_fetch_array($result); while ($row = mysqli_fetch_array($result)) { $formatedTime = $row['timestamp']; echo '<url> <loc>http://abcdefg.com/index.php?dispatch=products.view&product_id=' . $row['product_id'] . '</loc> <changefreq>weekly</changefreq> <lastmod>'. date('c',$formatedTime) .'</lastmod> </url>'; } //while ($row = mysqli_fetch_array($result)) echo '</urlset>'; ?> 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! can anyone please tell me how can I make a quiz in php which displays random questions one at a time
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. Could anyone help me please. I am trying to create a quiz in (quiz.php) which is works fine, but when I am trying to correct the quiz in another page (result.php) I had an error. I think the problem with posted answers and I couldn't solve it.
This is my code in (quiz.php):
<form method="post" action="result.php"> <?php session_start(); $questionNumber = 1; $query = mysql_query("SELECT * FROM Question WHERE TID = 'TEST' order by RAND()"); while ($result = mysql_fetch_array($query)) { ?> <?php echo $questionNumber . ") " . $result['Question'] . "<br>"; ?> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="A"> <?php echo $result['A']; ?> <br> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="B"> <?php echo $result['B']; ?> <br> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="C"> <?php echo $result['C']; ?> <br> <input type="radio" name="answer<?php echo $result["QID"] ?>" value="D"> <?php echo $result['D']; ?> <br> <?php $questionNumber +=1; } ?> <input type="submit" name="checkQuiz" value="Check Quiz" onclick="location.href='result.php'" /> </form>And This is my code in (result.php): <?php if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT QID, Answers FROM correct WHERE QID IN ($idList)"; while (list($QID, $correct) = mysql_fetch_row($res)) { if ($correct == $_POST['answer'][$QID]) { $correctAnswers +=1; } else { $wrongAnswers +=1; } } } ?> // in the body <?php $numberOfQs = $correctAnswers + $wrongAnswers; $score = round(($correctAnswers / $numberOfQs) * 100); ?> Correct Answers: <?php echo $correctAnswers; ?> <br> Wrong Answers: <?php echo $wrongAnswers; ?> <br> Sco <?php echo $score; ?> 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 |