Solved

php menu in loop

Posted on 2011-02-23
10
387 Views
Last Modified: 2012-06-27
hello,

I am having a problem with a dropdown menu in php, the menu works fine, it displays everything as it should, but it will not pass down any values. the reason there is a $i next to it is because the number of menus must be the same as the counter in the loop, and each menu must have its own name obviously, so I opted for 'firstname$i' where the first menu would be seen as firstname0, 2nd being firstname1 etc....  the [] are there because tjhe user must be able to select multiple options from the menu.

the code section is:

<?

?>

<FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?


$i = 0;
while ( $i <= 5 ) {

//echo"Firstname: <input type='text' name='firstname$i'[] />";


echo"<select name= 'firstname[$i]'[] size=4 multiple>
  <option>Milk</option>
  <option>Coffee</option>
  <option>Tea</option>
</select>";

$i++;

}

?>

</Select>
<input type=Submit value=pass on>
</form>

<?


$var2 = $_POST['firstname0'];



print_r($var2);
?>

please can someone show me where it is that I am going wrong.

thanks, Gavin
0
Comment
Question by:GPB1983
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34959916
In this line

echo"<select name= 'firstname[$i]'[] size=4 multiple>

Move the quote between the ] [

echo"<select name= 'firstname[$i][]' size=4 multiple>
0
 

Author Comment

by:GPB1983
ID: 34960014
hiya,

thanks for the quick reply, but I have already tried that on another example, the code compiles fine, but doesnt work. it doesnt pass anything.

the url for it is:
http://www.gavinbuczko.co.uk/phplooptrial.php

regards, Gavin
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34960133
Works for me.....

_POST["firstname"]      

Array
(
    [0] => Array
        (
            [0] => milk
            [1] => coffee
            [2] => tea
        )

    [2] => Array
        (
            [0] => milk
            [1] => coffee
        )

    [4] => Array
        (
            [0] => milk
            [1] => tea
        )

    [5] => Array
        (
            [0] => coffee
            [1] => tea
        )

)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:GPB1983
ID: 34960208
hi thanks, yes it does, but what I need it to do is if i choose stuff from menu one then it prints that, if i select stuff from menu 2 then print that, at the moment it prints everything out.

regards, Gavin
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34960248
No - it only prints what is selected. If you look at the above you will see that the array indexes are 0, 2, 3, 4 and 5. Index number 1 is missing because I did not select anything on that menu.

Perhaps I'm missing the point?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34960251
Actually index 3 is missing as well.
0
 

Author Comment

by:GPB1983
ID: 34960295
Hi, sorry I mis understood, but you are right.
what I was trying to do was to get the contents of menu 0 into

$var2 = $_POST['firstname0'];
$var3 = $_POST['firstname1'];

etc..

sorry I should have explained better.

regards, Gavin
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 500 total points
ID: 34960420
Something like this will pick out the data

<?php
if (isset( $_POST['firstname'] ) ) {
     $data = $_POST['firstname'];
     

     foreach( $data as $index => $aGroup ) {
          echo "Group number $index selected ";

          foreach( $aGroup as $aValue )
               echo "$aValue ";

          echo "<br/>";

     }
}

Open in new window


It could be modified to store the data easily enough
0
 

Author Comment

by:GPB1983
ID: 34961739
thanks mate, I really appreciate all the help, and the detailed explanation! top marks allround mate! thanks,
regards,
Gavin
0
 

Author Closing Comment

by:GPB1983
ID: 34961749
top quality explanations!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question