Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2004, 02:27 PM
Authorized User
 
Join Date: May 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Arrays Problem

i have the following code in my listbox.html page
What size of engine would you consider?
<html>
<body>
<br />
<br />
<select name="engineSize[]" multiple>
<option>1.0L</option>
<option>1.4L</option>
<option>1.6L</option>
<option>2.0L</option>
</select>
<br />
<br />
<input type="submit" />
</form>
</body>
</html>

and the following code in my listbox.php page
<html>
<body>
<?php
echo "<br> Engine Size(s):";
echo $_GET['engineSize[0]'];
echo $_GET['engineSize[1]'];
echo $_GET['engineSize[2]'];
echo $_GET['engineSize[3]'];
?>
</body>
</html>

however the output i get is
Engine Size(s):
and i want it to display
Engine Size(s): "whatever the user selects goes here"

how do i display multiple selections in an array through php???

thanks
 
Old June 19th, 2004, 02:45 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Quote:
quote:Originally posted by nulogix
 i have the following code in my listbox.html page
What size of engine would you consider?

...
however the output i get is
Engine Size(s):
and i want it to display
Engine Size(s): "whatever the user selects goes here"

how do i display multiple selections in an array through php???
There are a few problems here. First, you have no opening form tag.
Code:
<html>
<body>
<br />
<br />
<form action='listbox.php' method='get'>
<select name="engineSize[]" multiple>
<option>1.0L</option>
<option>1.4L</option>
<option>1.6L</option>
<option>2.0L</option>
</select>
<br />
<br />
<input type="submit" />
</form>
</body>
</html>
This is a common misconception. What you're doing is creating a multideminsional array, so the array must be formed like so $_GET['engineSize'][0].

Code:
<html>
<body>
<?php
echo "<br> Engine Size(s):";
echo $_GET['engineSize'][0];
echo $_GET['engineSize'][1];
echo $_GET['engineSize'][2];
echo $_GET['engineSize'][3];
?>
</body>
</html>
A better approach is to iterate through each indice in the array, this is done like this:

Code:
Engine Size:<br />
<?php
    if (isset($_GET['engineSize']) && is_array($_GET['engineSize']) &&  !empty($_GET['engineSize']))
    {
        foreach($_GET['engineSize'] as $value)
        {
            echo $value."<br />\n";
        }
    }
    else
    {
        echo "You did not select any engine sizes!";
    }
?>
First I've checked to make sure that $_GET['engineSize'] isset, its an array, and it isn't empty, just simple validation (although I realize it isn't presented that way in the book, just FYI). Then I use a foreach loop to loop through each value contained in the $_GET['engineSize'] array, using foreach I can split the array into just its value or key, value pairs, which is tremendously useful.

This is what you use to get variables for both key and value:
foreach ($array as $key => $value)

HTH!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old June 19th, 2004, 02:53 PM
Authorized User
 
Join Date: May 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Works Great..

thanks for the help

one thing.. on your second way of doing it is the part that says foreach($_GET['engineSize'] as $value)
taking the values that are in the engineSize array and assigning them to the $value variable???? If it is then way is the $value variable able to display multiple values instead of it erasing a value and updating it with a new one like a normal variable would do??
thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with arrays/loops jfern Javascript How-To 4 November 7th, 2006 10:29 AM
arrays ozPATT Excel VBA 2 November 4th, 2005 06:11 AM
Multidemmesional Arrays OR arrays gmoney060 Classic ASP Basics 3 November 1st, 2004 03:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.