Page 1 of 1
Multiple-choice letters
Posted: Tue Jul 16, 2002 11:26 am
by Testor
Hello,
I've been trying to make the letters of the alphabet appear in front
of a question choice or answer instead of typing them along with the question for example:
Question 1:
question text bla bla
[chkbx] A. answer choice
[chkbx] B. answer choice
[chkbx] C. answer choice
and so on depending on the number of choices for the question.
etc.., but I was unsuccessful the best I could do was make the script
write the alphabets or repeat a letter like AA for all choices. Can
someone please show me how or point me in the right direction so I can
figure it out?
Thanx
Posted: Tue Jul 16, 2002 11:34 am
by twigletmac
How about a bit of HTML:
Code: Select all
<ol type="A">
<li>Answer 1</li>
<li>Answer 2</li>
<li>Answer 3</li>
</ol>
Mac
Posted: Tue Jul 16, 2002 11:45 am
by Testor

That's why you're a professional and I'm a newbie
will these letters get carried over with the question if I need to display the question say in the results page?
Regards,
Posted: Tue Jul 16, 2002 12:14 pm
by twigletmac
The letters won't be stored with the question because the list is part of the HTML of the page and not the PHP. In order to get the same on the results page you just need to put everything into the ordered list (<ol>) and list item (<li>) tags again. If you put them in the same order then they'll have the same letters.
Mac
Posted: Thu Jul 25, 2002 10:06 am
by Testor
well, after trying it for a while, and thinking about it, your method is great but won't do for what I want especially when I want to carry the whole thing to the results page etc.. so, is there an php way? I tried a sample code to play with where I wanted to insert the letters A, B, C etc. in front of each name for example but I was unsuccessful.
Here's the sample code I was playing with:
<?PHP
echo "<h1> This is a small application with a table</h1>";
$arr = array("Peter Pan", "Jon Doe", "Ally McBeal", "Bruce Willis");
echo "<table>";
for($i=0;$i<count($arr);$i++) {
echo "<tr>";
echo "<td>$arr[$i]</td>";
echo "</tr>";
}
echo "</table>";
?>
Appreciate any help

Carrying values over and the alphabet
Posted: Thu Jul 25, 2002 12:15 pm
by musashi
So we need to display a form with checkboxes and answers with letters....(slightly modifying your code) here is one way:
Code: Select all
echo "<h1> This is a small application with a table</h1>\n";
echo "<form method="POST" action="blah.php">\n";
$arr = array("A"=>"Peter Pan",
"B"=>"Jon Doe",
"C"=>"Ally McBeal",
"D"=>"Bruce Willis");
echo "<table>\n";
while(list($letter,$answer) = each($arr)) {
echo "<tr>\n";
echo "<td><input type="checkbox" name="Q1" value="$letter-$answer"></td>";
echo "<td>$letter: $answer</td>\n";
echo "</tr>\n";
}
echo "</table>\n
echo "<input type="submit"></form>\n";
The value for $Q1 returned on the submission should contain a string that looks like "LETTER|ANSWER". Ex: A|Peter Pan
Simply split this:
Code: Select all
$pair = split("-",$Q1);
$letter = $pairї0];
$answer = $pairї1];
Incidentally if you wanted to auto-iterate through the alphabet (or any list of characters), you can do this:
Code: Select all
$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for($i=0;$i < strlen($string);$i++)
echo $stringї$i]."<br />";
You could combine that bit, and the code up at the top and have it "auto-letter" the questions for you.

Posted: Fri Jul 26, 2002 1:40 am
by twigletmac
I agree with musashi, storing the letters in an associative array is probably the easiest way of carrying those letters through in the way that you want.
There is however an easier way of iterating through the alphabet:
Code: Select all
for($i='A';$i != 'AA'; $i++) {
echo $stringї$i].'<br />';
}
If your form processing was taking place on the same page as the form creation (ie. in the same script) then you could simplify the checkboxes to just have a value of $letter. Then you can pick up the answer again just by getting it out of the array.
Mac