Solved

php menu in loop

Posted on 2011-02-23
10
383 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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 …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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.

710 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