Solved

php menu in loop

Posted on 2011-02-23
10
364 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
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.

777 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