Multiple-choice letters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Testor
Forum Newbie
Posts: 12
Joined: Tue Jul 16, 2002 11:26 am

Multiple-choice letters

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Testor
Forum Newbie
Posts: 12
Joined: Tue Jul 16, 2002 11:26 am

Post by Testor »

:D That's why you're a professional and I'm a newbie :D

will these letters get carried over with the question if I need to display the question say in the results page?

Regards,
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Testor
Forum Newbie
Posts: 12
Joined: Tue Jul 16, 2002 11:26 am

Post 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 :)
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Carrying values over and the alphabet

Post 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)) &#123; 
   echo "<tr>\n"; 
   echo "<td><input type="checkbox" name="Q1" value="$letter-$answer"></td>";
   echo "<td>$letter: $answer</td>\n";	
   echo "</tr>\n"; 
&#125;
 
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&#1111;0];
$answer = $pair&#1111;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&#1111;$i]."<br />";
You could combine that bit, and the code up at the top and have it "auto-letter" the questions for you. :wink:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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++) &#123;
     echo $string&#1111;$i].'<br />';
&#125;
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
Post Reply