Jump to content

Drop-Down Menu Questions


chickenvindaloo

Recommended Posts

Hi, this are some newbie questions, but I can't figure them out for the life of me. I have some code for a drop-down menu, but there are parts of the code I do not understand fully. here is the code, with my question as comments.

 

<?php           

 

$Q1 = array(

 

1=>"What is your Mother's Maiden name?",

2=>"What is your Mother's Maiden name?" ,

3=>"What is your Mother's Maiden name?" ,

4=>"What is your Mother's Maiden name?" ,

5=>"What is your Mother's Maiden name?" ,

6=>"What is your Mother's Maiden name?" ,

7=>"What is your Mother's Maiden name?" ,

8=>"What is your Mother's Maiden name?" ,

9=>"What is your Mother's Maiden name?" ,

10=>"What is your Mother's Maiden name?"

);

 

$Q1 = str_replace(" ", " ", $Q1);                  //why is this line necessary?

$count = 1;

echo '<SELECT name=Q1>';

foreach ($Q1 as $key => $value)

{

 

 

echo '<OPTION value='.$count.'> '.$value.' ';  /* why is the . operator needed here for $count and $value? And why do we need the last set of single parenthesis after $value? */

 

 

$count++;

}

echo '</select>';

 

?>

 

Thank you very much for your time.

Link to comment
https://forums.phpfreaks.com/topic/169714-drop-down-menu-questions/
Share on other sites

<?php

$Q1 = array(

1=>"What is your Mother's Maiden name?",
2=>"What is your Mother's Maiden name?" ,
3=>"What is your Mother's Maiden name?" ,
4=>"What is your Mother's Maiden name?" ,
5=>"What is your Mother's Maiden name?" ,
6=>"What is your Mother's Maiden name?" ,
7=>"What is your Mother's Maiden name?" ,
8=>"What is your Mother's Maiden name?" ,
9=>"What is your Mother's Maiden name?" ,
10=>"What is your Mother's Maiden name?"
);


$Q1 = str_replace(" ", " ", $Q1);                   //why is this line necessary?
// it's not necessary. it's replacing spaces with spaces. useless.
echo '<select name=Q1>';

foreach ($Q1 as $key => $value) {
        
echo '<option value='.$key.'> '.$value.'</option>';  /* why is the . operator needed here for $count and $value? And why do we need the last set of single parenthesis after $value? 
*/
       // The . is needed to concatenate the string to the variable. this could have been written as echo "<option value='$key'> $value</option>";
       // since variables inside double quoted strings dont need to be concatenated. The last . ' ' was there for no reason.


     /*
       * Also, theres no need for $count and $count++, since all keys on the array are numeric. the array could just be
       * $Q1 = array(
       *      "What is your Mother's Maiden name?",
       *      "What is your Mother's Maiden name?" ,
       *      etc...
      * and the keys would automatically be numbers ( 0 - 9 in this case)

}
echo '</select>';

?> 

or:

 

$SQ1 = array(1 => "What is your mother's maiden name?", 2 => "What is your mother's maiden name?");
$html = "<select><option>" . implode("</option><option>", $SQ1) . "</option></select>";
echo $html;

 

is a little neater, might be faster and is a little easier to read ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.