newone Posted December 23, 2016 Share Posted December 23, 2016 if(! $conn ) { die('Could not connect: ' . error); } $sql = 'SELECT q1,option1,option2,option3 FROM questions'; //mysqli_select_db('tutorial'); $result = mysqli_query( $conn, $sql); if(! $result ) { die('Could not get data: ' ); } while($row = mysqli_fetch_array($result)) { echo('<input type="radio" name="q1" value="q1'.$row[0].'"/>'.//$row1[0] :$row[0]} <br><br> ". " {$row[1]} <br> ". " B {$row[2]} <br> ". " C {$row[3]} <br> ". "--------------------------------<br>"); } basically i am fetching the data from mysql using the above command and want to display theses options as radio button but instead the output is showing radio button but no fetched data from mysql the lline is highlighted kindly help , thanks in advance i am trying to learning php Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 23, 2016 Share Posted December 23, 2016 (edited) before you can write code to perform a task, you must define what you want the result to be. if you are displaying a list of questions and three possible answers for each question, wouldn't your code need to have html markup to do each of these things? for each question, display the question text, then display the three radio buttons, each with a value that indicates which answer they correspond to, and display the answer text for each radio button. for radio buttons to work, all the buttons in a group must have the same name. i recommend using an array name, with the question id as the array index. this will let you simply loop over the submitted data using a php foreach(){} loop to get the question id and the chosen answer value. to accomplish the stated output, you would need the question text, the question id (to produce the radio button name array index and to facilitate the efficient storage of the submitted answers), and the text for three possible answers (you should actually have answer ids as the radio button values, but for simplicity, you could use the answer text as the radio button values.) does your current design have these values? what is the q1 column? is that the question text or the question id? the name of your columns should indicate the purpose of the data in the column. if you have columns named q1, q2, q3, ... this is not a good design and should be fixed. see the next point - your database design should actually store the questions (with columns for the question id and question text) and possible answers (with columns for the answer id, answer text, and if the answers are unique to each question, a question id) in separate tables (the submitted responses are stored in a third table.) if you use exceptions to handle database statement errors, you can eliminate all the conditional logic you are adding for each statement (connection and query) that can fail. an exception will be thrown for any error, which you can let php catch, and your main code only has to deal with the error free execution of statements. i also recommend that you fetch and use data from any sql query using an associative array, instead of a numerical array. this will help prevent mistakes in your code and make your code easier to read. Edited December 23, 2016 by mac_gyver Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 23, 2016 Share Posted December 23, 2016 Here's something that you can tinker with to see how this s/b done. /* comment out query for now $sql = 'SELECT q1,option1,option2,option3 FROM questions'; $result = mysqli_query( $conn, $sql); if(!$result ) { die('Could not get data: ' ); } */ // create an array to use as data in this example. Remove this and uncomment above // query logic to use your data later. $result = array( array('q1'=>'Question 1 text','option1'=>'Ans 1A','option2'=>'Ans 1B','option3'=>'Ans 1C'), array('q1'=>'Question 2 text','option1'=>'Ans 2A','option2'=>'Ans 2B','option3'=>'Ans 2C'), array('q1'=>'Question 3 text','option1'=>'Ans 3A','option2'=>'Ans 3B','option3'=>'Ans 3C'), array('q1'=>'Question 4 text','option1'=>'Ans 4A','option2'=>'Ans 4B','option3'=>'Ans 4C') ); $q_no = 0; //while($row = mysqli_fetch_array($result)) // loop thru the array until you uncomment the while above to use real data. foreach ($result as $row) { $q++; // added the php_eol in order to show the html code clearer for now // remove them when you are done testing echo "Question $q_no: ".$row['q1']."<br>".PHP_EOL; echo "Answers:".PHP_EOL; // show the answers in an unframed box indented under the question echo "<div style='margin-left:4%;'>"; echo "<br>"; echo "<input type='radio' name='ans$q_no' value='" .$row['option1'] ."'/>" . $row['option1'] . PHP_EOL; echo"<br><input type='radio' name='ans$q_no' value='" .$row['option2'] . "'/>" .$row['option2'] .PHP_EOL; echo"<br><input type='radio' name='ans$q_no' value='" .$row['option3'] . "'/>" .$row['option3'].PHP_EOL; echo "</div>"; echo "<br>--------------------------------<br>".PHP_EOL; } Of course you'll have to add an html form around all this and then grab the resulting $_POST elements to get the user's selected answers. Hope this helps. Quote Link to comment 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.