Page 1 of 1

Adding Radio Buttons to my script. Need help.

Posted: Mon Jul 23, 2007 1:12 pm
by kvazar
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


What I am trying to create is basically a page with a bunch of multiple choice questions. Basically just like a test page. I am providing the code lower. All the questions that need to show as a part of the test are stored in a separate database. There is also a test file "list.txt" that has names of files from the database that I want to display. That way you don't have to manually choose the questions. All you need is to generate a text file with questions you want to display.
Script consist of two functions. First one accesses the text file, reads it and adds names of file to the link of the database. second function opens questions from that database. All questions that same simple structure:


QUESTION
BLAH BLAH BLAH BLAH BLAH BLAH BLAH
FIGURE
None
OPTIONS
1
10
100
30
40
ANSWER
D

Additional code in second function omits lines Question, Answer, ect

Now here is my problem. I need to add radio buttons to my multiple choice answers so that they show correctly with each question.
Please help me if you can.
Thanks

Here is the code

Code: (PHP)

Code: Select all

<?php
function open_assignment($textfile) {
$file = fopen($textfile, 'r') or die ("Can't open file1");
$theData = fread($file, filesize($textfile));
fclose($file);
$delimiter = "\n";
$splitcontents = explode($delimiter, $theData);
foreach( $splitcontents as $question )
{
open_question("./database/".$question);
echo '<hr>';
}
}

function open_question($record) {
$file = fopen($record, 'r') or die("Can't open file2");
$theData = fread($file, filesize($record));
fclose($file);
$delimiter = "\n";
$splitcontents = explode($delimiter, $theData);
$expecting_answer = 0;
$answer = "unknown";
$expecting_figure = 0;
$figure = "unknown";
foreach( $splitcontents as $record )

{
if ($record == "QUESTION") {
// do something //
} else if ($record == "FIGURE") {
// do something //
$expecting_figure = 1;
} else if ($expecting_figure) {
$figure = $record;
$expecting_figure = 0;
} else if ($record == "OPTIONS") {
// do something else //
} else if ($record == "ANSWER") {
// do something else //
$expecting_answer = 1;
} else if ($expecting_answer) {
$answer = $record;
$expecting_answer = 0;
} else {

echo $record.'<br>';
}
}
}
?>

<html>
<body>
<?php
open_assignment("./list.txt");

?>

Thank you for the help


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 23, 2007 2:04 pm
by superdezign
Radio buttons are just a list of <input type="radio" /> elements with the same 'name' attribute as another. If this isn't what you're after, you'll need to give more detail.

Posted: Mon Jul 23, 2007 2:44 pm
by kvazar
Thanks
I understand the code for radio buttons.
The problem that I have is adding this code to my code, so these buttons show up after function reads my text files.
Let me know if this is not clear enough.
thanks for the help

Posted: Mon Jul 23, 2007 2:48 pm
by superdezign
Well, from a purely object-oriented point of view, I'd have the radio buttons and the question options as the same object. Your application isn't programmed in an object-oriented sense, but you still want to create a relationship between each option and a corresponding radio button. For each option you create, you should create a radio button to correspond to it.