Solved

php menu in loop

Posted on 2011-02-23
10
346 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
  • 5
  • 5
10 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:GPB1983
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
Actually index 3 is missing as well.
0
 

Author Comment

by:GPB1983
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
top quality explanations!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now