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
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>';

?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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