New to PHP and need some help with this.

I'm creating a test, but I'm getting all of my questions randomly from my database.

For example, if this is my table:

ID S Q1 A1 A2
1 C Why A B
2 F When A B

I know that I need the query to select from Q1, A1 and A2, leaving the rest out, but I'm not sure how I can get that into a form.

Thanks for your help!

Member Avatar for P0lT10n

You must first request this data like this

$query = mysql_query("SELECT Q1, A1, A2 FROM --YOUR_TABLE--");
while($row = mysql_fetch_assoc($query)){
    echo $row['Q1']."-".$row['A1']."-".$row['A2'];
}

Thank you so much, the data requested perfect!

When I'm pulling the data to put into a form with radio buttons, am I doing that in html and pulling the options and questions from the $query?

Really lost on what to do next. Your help is greatly appreciated.

Member Avatar for P0lT10n

Thank you so much, the data requested perfect!

When I'm pulling the data to put into a form with radio buttons, am I doing that in html and pulling the options and questions from the $query?

Really lost on what to do next. Your help is greatly appreciated.

I dont understand what you need !

Im gonna give you an example based on the code you gave me. I want a form to have the question first and then the answers with radio buttons.

In my form:
I want Q1 to be the question
I want A1 to be the first option with radio buttons
I want A2 to be the second option with a radio button

I'm just having a hard time flipping from HTML back to PHP and getting it to connect.

Thanks,

I finally have what I want, I hope. Here's the code I used.

$res = mysql_query("SELECT question_no, question, answer_a, answer_b, answer_c, answer_d, answer_e FROM t_questions");
echo "<form name='whatever' action='next.php' method='get'>";
while($row = mysql_fetch_assoc($res))
{
    echo $row['question'] . '<br />';
    echo "<input type='radio' name='choice' value='" . $row['question_no'] . "' /> ". $row['answer_a']. '<br />' ;
    echo "<input type='radio' name='choice' value='" . $row['question_no'] . "' /> ". $row['answer_b']. '<br />' ;
    echo "<input type='radio' name='choice' value='" . $row['question_no'] . "' /> ". $row['answer_c']. '<br />' ;
    echo "<input type='radio' name='choice' value='" . $row['question_no'] . "' /> ". $row['answer_d']. '<br />' ;
Member Avatar for P0lT10n

But if you use as value the question number or question_no, and the user select any answer, you will get the same value, and you wont know what answer was... Mark thread as solved please !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.