woodplease Posted January 30, 2010 Share Posted January 30, 2010 I am trying to create a system whereby a user enters a quiz name, and then enters questions and corresponding answers, for other users to answer later on. I have three tables, one containing quiz names, one containing questions, and one containing answers. I want to print out the question, followed by the corresponding questions, and then loop through to print out the next question and answers. The quiz table has a field "quizref". The question and answer tables also have a field "quizref". The answer table also has a field "questionno", which is a field from the question table. What i need is an sql statement which will select only the answers form the corresponding question, and then the questions that are from the corresponding quiz. At the moment the page containing the list of quizes passes the quiz ref in the url bar to the next page(the page to answer the quiz) This is the code i have so far, but i cannot seem to get anywhere with it. <?php session_start(); $db = pg_connect("host=database.dcs.aber.ac.uk port=5432 dbname=**** user=**** password=*******"); ?> <!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> <?php $id = $_GET['id']; $result = pg_query("SELECT * FROM quiz WHERE quizref = " . $id ); $row = pg_fetch_assoc($result); echo '<h3>quizref:</h3> ' .$row['quizref']; echo '<h3>name:</h3> ' .$row['name']; echo '<h3>created by:</h3> ' .$row['createdby']; //Displays the Quiz Reference and Quiz Name ?> <?php $id = $_GET['id']; $query = "SELECT * FROM questions WHERE quizref = " .$id ; $result2 = pg_query($query) or die ("Query failed"); //let's get the number of rows in our result so we can use it in a for loop $numofrows = pg_num_rows($result2); ?> <?PHP for($i = 0; $i < $numofrows; $i++) { $row = pg_fetch_array($result2); //get a row from our result set if($i % 2) { //this means if there is a remainder echo "<TR bgcolor=\"#666666\">\n"; } else { //if there isn't a remainder we will do the else echo "<TR bgcolor=\"#777777\">\n"; } echo " <p>".$row['questionno']."</p> <p>".$row['question']."</p> \n"; ?> <?php //Printing out answers $id = $_GET['id']; $query2 = "SELECT * FROM answers WHERE quizref = " .$id ."AND FROM questions WHERE quizref = " .$id ; SELECT products.product_ID, manufacturer.manufacturer_name from products LEFT JOIN manufacturer ON products.manufacturer_ID = manufacturer.manufacturer_ID ORDER BY products.product_ID "SELECT * From answers" $result3 = pg_query($query2) or die ("Query failed"); //let's get the number of rows in our result so we can use it in a for loop $numofrows2 = pg_num_rows($result3); ?> <?PHP for($i = 0; $i < $numofrows2; $i++) { $row2 = pg_fetch_array($result3); //get a row from our result set if($i % 2) { //this means if there is a remainder echo "<TR bgcolor=\"#666666\">\n"; } else { //if there isn't a remainder we will do the else echo "<TR bgcolor=\"#777777\">\n"; } echo " <p>".$row2['answerno']."</p> <p>".$row2['answeer']."</p> \n"; echo "</TR>\n"; } echo "</TABLE>\n"; echo "</TR>\n"; } echo "</TABLE>\n"; ?> </body> </html> Any help would be greatly appreciated Thanks Quote Link to comment https://forums.phpfreaks.com/topic/190362-printing-results-from-2-tables/ Share on other sites More sharing options...
wildteen88 Posted January 30, 2010 Share Posted January 30, 2010 You should look into using SQL Joins. Joins allow you to query more than one table at a time. Quote Link to comment https://forums.phpfreaks.com/topic/190362-printing-results-from-2-tables/#findComment-1004248 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.