decboys515 Posted May 6, 2014 Share Posted May 6, 2014 I'm working on this quiz. Something has gone wrong with my form though. The page displays a random question but instead of showing 4 different answer options, it repeats one option 4 times. How do should I go about fixing this? <?php SESSION_START(); //Connect to MySQL: $con = new mysqli("localhost", "root", "", "quizproject"); ?> <html> <body> <h1>Hello, <?php echo $_SESSION["fname"]; " " ?> <?php echo $_SESSION["lname"]; ?>.</h1> <p><h2>Quiz Time! Please answer each of the following questions:</h2><br> <?php //retrieve question list: $get_questions = $con->prepare("SELECT question_ID, question FROM questions"); $get_questions->execute(); $get_questions->bind_result($question_ID, $question); $questions = array(); while ($get_questions->fetch()) { $questions[$question_ID] = array($question_ID, $question, array()); } // retrieve answer list: $get_answers = $con->prepare("SELECT id, question_ID, answers, correct FROM answers"); $get_answers->execute(); $get_answers->bind_result($id, $question_ID, $answers, $correct); while ($get_answers->fetch()) { $questions[$question_ID][2][$id] = array($id, $answers, $correct); } // Scramble the array and print: shuffle($questions); ?> <form method="get" action="result.php"> <div class="question"> <label><h2><?php echo $question ?></h2></label> <br> <input type="hidden" name="question<?php echo $question_ID ?>_id" value="<?php echo $question_ID; ?>" id="question<?php echo $question_ID; ?>_id"/> <input name="answer<?php echo $id ?>" id="q<?php echo $id ?>_a1" value="1" type="radio"/> <label for="q<?php echo $id ?>_a1"> <?php echo $answers ?></label><br> <input name="answer<?php echo $id?>" id="q<?php echo $id ?>_a2" value="2" type="radio"/> <label for="q<?php echo $id ?>_a2"> <?php echo $answers ?></label><br> <input name="answer<?php echo $answers ?>" id="q<?php echo $id ?>_a3" value="3" type="radio"/> <label for="q<?php echo $id ?>_a3"> <?php echo $answers ?></label><br> <input name="answer<?php echo $id ?>" id="q<?php echo $id ?>_a4" value="4" type="radio"/> <label for="q<?php echo $id ?>_a4"> <?php echo $answers ?></label><br> </div> <?php echo "<br><input name=\"submit\" type=\"submit\" value=\"Submit\">"; echo "</form></body></html>"; ?> </form> </html> </body> Quote Link to comment https://forums.phpfreaks.com/topic/288296-php-and-forms/ Share on other sites More sharing options...
Strider64 Posted May 7, 2014 Share Posted May 7, 2014 (edited) I wrote a trivia game in Flash and the used a MySQL database, but I have since converted the trivia game over to JQuery, PHP and AJAX. The one thing that has remain constant over all these years is the query string (with minor changes obviously): $query = 'SELECT id, question, answerA, answerB, answerC, answerD, correct FROM movieTrivia WHERE id>=:current_id ORDER BY id ASC LIMIT 1'; Your code looks like it doesn't have individual answers, I could be wrong for I just did a quick look over of your code: If you want you can have a look at the code that I wrote for the trivia game at: http://www.jrpepp.com/displayPage.php?page=189 and a working script at : http://www.pepster.com/ Edited May 7, 2014 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478552 Share on other sites More sharing options...
decboys515 Posted May 7, 2014 Author Share Posted May 7, 2014 @strider64 it's set where it pulls all the questions but it only displays one. thank you for giving it a glance and for the links, i'll check those out now. Quote Link to comment https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478573 Share on other sites More sharing options...
maxxd Posted May 7, 2014 Share Posted May 7, 2014 (edited) Couple things here. First, I may be missing it but I'm not seeing where you're setting the $answers variable. Or the $question variable, for that matter - are you not showing some looping? In addition, if your script is giving you anything other than an array to string conversion error, you're printing the same string ($answers) over and over again. Your initial queries could be rewritten using a join to be a single call to the database as well. There are a couple different ways to get one random question and it's associated answers without having to load the entire contents of both tables into an array. Edited May 7, 2014 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478577 Share on other sites More sharing options...
decboys515 Posted May 7, 2014 Author Share Posted May 7, 2014 Couple things here. First, I may be missing it but I'm not seeing where you're setting the $answers variable. Or the $question variable, for that matter - are you not showing some looping? In addition, if your script is giving you anything other than an array to string conversion error, you're printing the same string ($answers) over and over again. Your initial queries could be rewritten using a join to be a single call to the database as well. There are a couple different ways to get one random question and it's associated answers without having to load the entire contents of both tables into an array. no on the looping which is probably a significant issue since it's suppose to output a total of 5 questions. this is my first crack at working with databases so i'm a little confused and i'm not sure how to use a join. Quote Link to comment https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478580 Share on other sites More sharing options...
KevinM1 Posted May 7, 2014 Share Posted May 7, 2014 Before you do anything with a database, you need to ensure that your table structure is sound. Otherwise, basic operations become difficult to do properly, to say nothing of more complex actions. So, read through this tutorial first. It will get you on the right track, which will then make your queries easier to write. http://mikehillyer.com/articles/an-introduction-to-database-normalization/ Quote Link to comment https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478582 Share on other sites More sharing options...
maxxd Posted May 7, 2014 Share Posted May 7, 2014 (edited) As KevinM1 points out, the first thing you want to do is acquaint yourself with some database theory (nice article, BTW). The table structure you define in your original queries doesn't look bad depending on the content of the 'answers' column in the answers table, but that's outside the scope of this question. Basically, I'd do something along the lines of SELECT q.question_id ,q.question ,a.id AS answer_id ,a.answers FROM questions q LEFT JOIN answers a ON q.question_id = a.question_id If you're feeling real adventurous, you can add an ORDER BY RAND() clause and try to limit the response to one question in order to avoid having to do it in PHP.Once you've got your record set returned (for this example, I'm assuming in an associative array called $result), try something along the lines of the following: <h2><?php echo $result['question'] ?></h2> <input type="hidden" name="question_id" value="<?php echo $result['question_ID']; ?>" id="question<?php echo $result['question_ID']; ?>_id"/> <?php foreach( $result as $answer ){ echo "<label for='q{$answer['[answer_id']}'>{$answer['answers']}</label>"; echo "<input name='answer_{$answer['question_id']}' id='q{$answer['answer_id']}' value='{$answer['answer_id']}' type='radio'/>"; } ?> As an aside, I wouldn't bother selecting the 'a.correct' column while ouputting the question and answer - you're not going to need that information until you grade the answer (after form submission). Also, I should think it goes without saying, but this won't work if you want to display more than 1 question at a time, but you get the general idea I hope. Edited May 7, 2014 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/288296-php-and-forms/#findComment-1478592 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.