Jump to content

dynamic radio button source code not showing fetched data


Recommended Posts

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

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 by mac_gyver

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.