October 16, 201411 yr Hi guys. I'm Rich and only just joined this forum. I hope that you can help me please as I'm ready to rip my hair out! At the moment I'm re-creating the website that I designed for my dissertation while I was at uni. This is also going to be my portfolio to show employers my skills and knowledge which will help me to get a graduate PHP web developer job. Over the past few weeks I have had a number of job offers but they cannot offer me an interview untill I have my website up and running and on a web domain. The webs site is a learning aid for people like myself who have the learning disability called dyscalculia. There are revision pages that are now complete. I have got a quiz with 80 questions. I would like to be able to show just 20 questions that are random. This is the quiz code so far: <head> <link rel="stylesheet" type="text/css" href="/css/style.css" /> </head> <?php require 'core/init.php'; include('inc/pheader.php'); ?> <?php session_start(); if(isset($_GET['question'])){ $question = preg_replace('/[^0-9]/', "", $_GET['question']); $next = $question + 1; $prev = $question - 1; if(!isset($_SESSION['qid_array']) && $question != 1){ $msg = "Sorry! No cheating."; header("location: start.php?msg=$msg"); exit(); } if(isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])){ $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha."; unset($_SESSION['answer_array']); unset($_SESSION['qid_array']); session_destroy(); header("location: start.php?msg=$msg"); exit(); } if(isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev){ $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha."; unset($_SESSION['answer_array']); unset($_SESSION['qid_array']); session_destroy(); header("location: start.php?msg=$msg"); exit(); } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Quiz Page</title> <script type="text/javascript"> function countDown(secs,elem) { var element = document.getElementById(elem); element.innerHTML = "You have "+secs+" seconds remaining."; if(secs < 1) { var xhr = new XMLHttpRequest(); var url = "userAnswers.php"; var vars = "radio=0"+"&qid="+<?php echo $question; ?>; xhr.open("POST", url, true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { alert("You did not answer the question in the allotted time. It will be marked as incorrect."); clearTimeout(timer); } } xhr.send(vars); document.getElementById('counter_status').innerHTML = ""; document.getElementById('btnSpan').innerHTML = '<h2>Times Up!</h2>'; document.getElementById('btnSpan').innerHTML += '<a href="quiz.php?question=<?php echo $next; ?>">Click here now</a>'; } secs--; var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000); } </script> <script> function getQuestion(){ var hr = new XMLHttpRequest(); hr.onreadystatechange = function(){ if (hr.readyState==4 && hr.status==200){ var response = hr.responseText.split("|"); if(response[0] == "finished"){ document.getElementById('status').innerHTML = response[1]; } var nums = hr.responseText.split(","); document.getElementById('question').innerHTML = nums[0]; document.getElementById('answers').innerHTML = nums[1]; document.getElementById('answers').innerHTML += nums[2]; } } hr.open("GET", "questions.php?question=" + <?php echo $question; ?>, true); hr.send(); } function x() { var rads = document.getElementsByName("rads"); for ( var i = 0; i < rads.length; i++ ) { if ( rads[i].checked ){ var val = rads[i].value; return val; } } } function post_answer(){ var p = new XMLHttpRequest(); var id = document.getElementById('qid').value; var url = "userAnswers.php"; var vars = "qid="+id+"&radio="+x(); p.open("POST", url, true); p.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); p.onreadystatechange = function() { if(p.readyState == 4 && p.status == 200) { document.getElementById("status").innerHTML = ''; alert("Thanks, Your answer was submitted"+ p.responseText); var url = 'quiz.php?question=<?php echo $next; ?>'; window.location = url; } } p.send(vars); document.getElementById("status").innerHTML = "processing..."; } </script> <script> window.oncontextmenu = function(){ return false; } </script> </head> <body onLoad="getQuestion()"> <div id="status"> <div id="counter_status"></div> <div id="question"></div> <div id="answers"></div> </div> <script type="text/javascript">countDown(45,"counter_status");</script> </body> </html> There are two tables one is called "answers" and the other is called "questions" both are related/ linked as they have ID and question ID. So this is so the answers and questions can be linked along with what answer the user is regustering when taking the quiz. This is done with PHP Any help would be much appreciated Thanks guys Rich
October 18, 201411 yr You can use RAND() in your sql query to choose randomly and use LIMIT to limit 20 questions, basically something like this SELECT * FROM `table` ORDER BY RAND() LIMIT 20 Edited October 18, 201411 yr by webdesigner93
October 21, 201411 yr Author You can use RAND() in your sql query to choose randomly and use LIMIT to limit 20 questions, basically something like this SELECT * FROM `table` ORDER BY RAND() LIMIT 20 thanks for this. How on earth do I put this into the code then?
October 21, 201411 yr thanks for this. How on earth do I put this into the code then? Heres a simple example <?php //Connect with mysql $db = new mysqli("host","user","pass","db"); //Perform the query to choose random questions $query = $db->query("SELECT * FROM `table` ORDER BY RAND() LIMIT 20"); while($row = $query->fetch_assoc()): $question = $row['question']; echo $question."<br />"; endif; //close result $query->close(); //Close connection $db->close();
October 22, 201411 yr Author thanks for this I will give it a try and let you know how I get on with it
October 22, 201411 yr Author hey I have put that code into my quiz code, when i try and now run the quiz i get the following Parse error: syntax error, unexpected 'endif' (T_ENDIF) in C:\xampp\htdocs\quiz.php on line 11
October 23, 201411 yr Remove the endif; You can't have an endif without an if Oops thats suppose to be endwhile;
October 23, 201411 yr Author Oops thats suppose to be endwhile; thanks for that that has got rid of that error message but now I have got another one now :-( Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\quiz.php on line 145 (please refer to the code that I posted in my first original post)
October 23, 201411 yr thanks for that that has got rid of that error message but now I have got another one now :-( Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\quiz.php on line 145 (please refer to the code that I posted in my first original post) this usually means you for got a closing bracket in your php somewhere try adding a } on line 145 and see if that fixes it
October 23, 201411 yr Author thanks for that I have tried what you have suggested and its still coming up with the same error Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\quiz.php on line 148 this is the specific line of code that is causing my headache <script type="text/javascript">countDown(45,"counter_status") }
October 27, 201411 yr Author bump anyone? I really want to get this quiz sorted now sorry to be such a pain
October 27, 201411 yr Remove the curly bracket. I'm sure what comes after that line of code though. You'll need a semi-colon after the ) too. Mmm actually im not sure if that's the issue considering that curly bracket looks like it is part of the javascript and the error he is getting is a PHP error. Attach quiz.php to your post and i'll sort it for you if the above reply does not fix the issue
October 28, 201411 yr You're definitely correct. However: <script type="text/javascript">countDown(45,"counter_status") } Is also wrong. I believe it should be: <script type="text/javascript">countDown(45,"counter_status"); I agree the javascript is wrong which was a good find cause I didn't notice it till you mentioned it lol
November 17, 201411 yr Author hi guys sorry to have taken so long to replay back to you all. Its been a really busy few weeks!! Hope your all well. I have taken out the quiz counter for the moment, I just want to make sure that the quiz actully works. I'm now getting this parse error when i try and run the quiz; Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\quiz.php on line 147 this is the code as it stands at the moment <?php //Connect with mysql $db = new mysqli("host","user","pass","db"); //Perform the query to choose random questions $query = $db->query("SELECT * FROM `table` ORDER BY RAND() LIMIT 20"); while($row = $query->fetch_assoc()): $question = $row['question']; echo $question."<br />"; //close result $query->close(); //Close connection $db->close(); ?> <?php session_start(); if(isset($_GET['question'])){ $question = preg_replace('/[^0-9]/', "", $_GET['question']); $next = $question + 1; $prev = $question - 1; if(!isset($_SESSION['qid_array']) && $question != 1){ $msg = "Sorry! No cheating."; header("location: start.php?msg=$msg"); exit(); } if(isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])){ $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha."; unset($_SESSION['answer_array']); unset($_SESSION['qid_array']); session_destroy(); header("location: start.php?msg=$msg"); exit(); } if(isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev){ $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha."; unset($_SESSION['answer_array']); unset($_SESSION['qid_array']); session_destroy(); header("location: start.php?msg=$msg"); exit(); } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Quiz Page</title> <script type="text/javascript"> function countDown(secs,elem) { var element = document.getElementById(elem); element.innerHTML = "You have "+secs+" seconds remaining."; if(secs < 1) { var xhr = new XMLHttpRequest(); var url = "userAnswers.php"; var vars = "radio=0"+"&qid="+<?php echo $question; ?>; xhr.open("POST", url, true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { alert("You did not answer the question in the allotted time. It will be marked as incorrect."); clearTimeout(timer); } } xhr.send(vars); document.getElementById('counter_status').innerHTML = ""; document.getElementById('btnSpan').innerHTML = '<h2>Times Up!</h2>'; document.getElementById('btnSpan').innerHTML += '<a href="quiz.php?question=<?php echo $next; ?>">Click here now</a>'; } secs--; var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000); } </script> <script> function getQuestion(){ var hr = new XMLHttpRequest(); hr.onreadystatechange = function(){ if (hr.readyState==4 && hr.status==200){ var response = hr.responseText.split("|"); if(response[0] == "finished"){ document.getElementById('status').innerHTML = response[1]; } var nums = hr.responseText.split(","); document.getElementById('question').innerHTML = nums[0]; document.getElementById('answers').innerHTML = nums[1]; document.getElementById('answers').innerHTML += nums[2]; } } hr.open("GET", "questions.php?question=" + <?php echo $question; ?>, true); hr.send(); } function x() { var rads = document.getElementsByName("rads"); for ( var i = 0; i < rads.length; i++ ) { if ( rads[i].checked ){ var val = rads[i].value; return val; } } } function post_answer(){ var p = new XMLHttpRequest(); var id = document.getElementById('qid').value; var url = "userAnswers.php"; var vars = "qid="+id+"&radio="+x(); p.open("POST", url, true); p.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); p.onreadystatechange = function() { if(p.readyState == 4 && p.status == 200) { document.getElementById("status").innerHTML = ''; alert("Thanks, Your answer was submitted"+ p.responseText); var url = 'quiz.php?question=<?php echo $next; ?>'; window.location = url; } } p.send(vars); document.getElementById("status").innerHTML = "processing..."; } </script> window.oncontextmenu = function(){ return false; } </script> </head> <body onLoad="getQuestion()"> <div id="status"> <div id="counter_status"></div> <div id="question"></div> <div id="answers"></div> </div> </script> </body> </html>
November 21, 201411 yr Author Also put your "session_start();" at the top of the script. Hey rallport, thanks for this. I have check the code but its still throwing an error up when I try and run the code. Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\quiz.php on line 138 But I have to admit we are looking looking at this the wrong way.... I have just looked at the file more closely in dreamweaver. There is a read blob on line This is the line of code that I think is giving us the headache var url = 'quiz.php?question=<?php echo $next; ?>';
November 23, 201411 yr Author I have managed to sort out the extra curly bracket that was causing me the headache. this is the updated the code <?php //Connect with mysql $db = new mysqli("host", "user", "pass", "db"); //Perform the query to choose random questions $query = $db->query("SELECT * FROM `table` ORDER BY RAND() LIMIT 20"); while ($row = $query->fetch_assoc()): $question = $row['question']; echo $question . "<br />"; endwhile; //close result $query->close(); //Close connection $db->close(); session_start(); if (isset($_GET['question'])) { $question = preg_replace('/[^0-9]/', "", $_GET['question']); $next = $question + 1; $prev = $question - 1; } // <--- I added this brace. You need to double-check it logically belongs here if (!isset($_SESSION['qid_array']) && $question != 1) { $msg = "Sorry! No cheating."; header("location: start.php?msg=$msg"); exit(); } if (isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])) { $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha."; unset($_SESSION['answer_array']); unset($_SESSION['qid_array']); session_destroy(); header("location: start.php?msg=$msg"); exit(); } if (isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev) { $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha."; unset($_SESSION['answer_array']); unset($_SESSION['qid_array']); session_destroy(); header("location: start.php?msg=$msg"); exit(); } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Quiz Page</title> <script type="text/javascript"> function countDown(secs,elem) { var element = document.getElementById(elem); element.innerHTML = "You have "+secs+" seconds remaining."; if(secs < 1) { var xhr = new XMLHttpRequest(); var url = "userAnswers.php"; var vars = "radio=0"+"&qid="+<?php echo $question; ?>; xhr.open("POST", url, true); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { alert("You did not answer the question in the allotted time. It will be marked as incorrect."); clearTimeout(timer); } } xhr.send(vars); document.getElementById('counter_status').innerHTML = ""; document.getElementById('btnSpan').innerHTML = '<h2>Times Up!</h2>'; document.getElementById('btnSpan').innerHTML += '<a href="quiz.php?question=<?php echo $next; ?>">Click here now</a>'; } secs--; var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000); } function getQuestion(){ var hr = new XMLHttpRequest(); hr.onreadystatechange = function(){ if (hr.readyState==4 && hr.status==200){ var response = hr.responseText.split("|"); if(response[0] == "finished"){ document.getElementById('status').innerHTML = response[1]; } var nums = hr.responseText.split(","); document.getElementById('question').innerHTML = nums[0]; document.getElementById('answers').innerHTML = nums[1]; document.getElementById('answers').innerHTML += nums[2]; } } hr.open("GET", "questions.php?question=" + <?php echo $question; ?>, true); hr.send(); } function x() { var rads = document.getElementsByName("rads"); for ( var i = 0; i < rads.length; i++ ) { if ( rads[i].checked ){ var val = rads[i].value; return val; } } } function post_answer(){ var p = new XMLHttpRequest(); var id = document.getElementById('qid').value; var url = "userAnswers.php"; var vars = "qid="+id+"&radio="+x(); p.open("POST", url, true); p.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); p.onreadystatechange = function() { if(p.readyState == 4 && p.status == 200) { document.getElementById("status").innerHTML = ''; alert("Thanks, Your answer was submitted"+ p.responseText); var url = 'quiz.php?question=<?php echo $next; ?>'; window.location = url; } } p.send(vars); document.getElementById("status").innerHTML = "processing..."; } window.oncontextmenu = function(){ return false; } </script> </head> <body onLoad="getQuestion()"> <div id="status"> <div id="counter_status"></div> <div id="question"></div> <div id="answers"></div> </div> </script> </body> </html> But now I'm now getting the following errors when I try and run the quiz. Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\quiz.php on line 3Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\quiz.php on line 3Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\quiz.php on line 6Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\quiz.php on line 8Everything else is working on my site....
Create an account or sign in to comment