Subject: How to use $_GET with arrays?
Posted By: pseudo class Post Date: 10/25/2004 9:43:20 PM
I'm on chapter 3 of the book and I just need to know how to echo variables like $enginesize[0], $enginesize[1], etc..etc.. with register_globals shut off.

Thanks in advance


Reply By: Snib Reply Date: 10/26/2004 12:16:40 PM
Try $_GET['enginesize'][0], etc.

HTH!

-Snib
Where will you be in 100 years?
Try new FreshView 0.2!
There are only two stupid questions: the one you can't find the answer to and don't ask, and the one that you can find the answer and do ask.
Reply By: pseudo class Reply Date: 11/1/2004 10:32:36 PM
that didnt work for me either, do you mean like

echo . $_GET['enginesize'][0]
echo . $_GET['enginesize'][1]
echo . $_GET['enginesize'][2]

etc...etc

b/c that didnt work for me.

Thanks in advance

Reply By: richard.york Reply Date: 11/2/2004 7:09:10 AM
$_GET works if the method of the form is the get method. I'm betting that the method of the form is probably post, whereas you should use the $_POST superglobal.

$_POST['enginesize'][0]

If you're still not getting anywhere do something like this to see if anything is being passed.

echp "<pre>";
var_dump($_POST);
echo "</pre>";

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
Reply By: Tim Gamble Reply Date: 11/20/2004 11:56:21 AM
quote:
Originally posted by richard.york

$_GET works if the method of the form is the get method. I'm betting that the method of the form is probably post, whereas you should use the $_POST superglobal.

$_POST['enginesize'][0]

If you're still not getting anywhere do something like this to see if anything is being passed.

echp "<pre>";
var_dump($_POST);
echo "</pre>";

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail



Reply By: Tim Gamble Reply Date: 11/20/2004 12:10:03 PM
quote:
Originally posted by Tim Gamble

quote:
Originally posted by richard.york

I'm having the same problem with the arrays in chapter 3 with the register_globals turned off. I took your suggestion and checked the method and it was set as GET but am still not able to echo more than one variable. Here is the code for listbox.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>
        <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
        <title>List Boxes</title>
    </head>

    <body bgcolor="#ffffff">
        <form id="FormName" action="listbox.php4" method="GET" name="FormName">
            What price car are you looking to buy?&nbsp;<select name="Price" size="4" multiple>
                <option>Under $5,000</option>
                <option>$5,000-$10,000</option>
                <option>$10,000-$25,000</option>
                <option>Over $25,000</option>
            </select><br>
            <p>What size engine would you consider?&nbsp;<select name="EngineSize[]" size="4" multiple>
                    <option>1.0L</option>
                    <option>1.4L</option>
                    <option>1.6L</option>
                    <option>2.0L</option>
                </select><br>
                <br>
                <input type="submit" name="submitButtonName" value="Let me know"><br>
            </p>
        </form>
    </body>

</html>

and here is the code for listbox.php4

<html>

    <head>
        <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
        <title>agl:pagetitle</title>
    </head>

    <body bgcolor="#ffffff">
        <p></p>
        
        
 
 You chose <br>
        <?php
 echo $_GET['Price'];
 ?>
 <br>
 <?php
 echo $_GET['EngineSize'][0];
 ?>
 
    </body>

</html>

The code will only returrn the last item selected in the EngineSize list.

Can you suggest anything else?

Tim









Reply By: richard.york Reply Date: 11/20/2004 1:06:27 PM
Ok, the problem with multiple select fields is the there isn't a way to see if option 4 is checked, versus say option 1, you can only look at the value.

For this example a foreach loop is most appropriate.


<?php
echo "<strong>The values of 'enginesize' are:</strong><br />\n";

if (isset($_GET['enginesize']) && is_array($_GET['enginesize']))
{
    foreach ($_GET['enginesize'] as $value)
    {
        echo $value."<br />\n";
    }
}
else
{
    echo "You didn't enter any values for 'enginesize'!<br />\n";
}
?>


The foreach loop is designed specifically for arrays, in this case $_GET['enginesize'] is a multidimensional array, so it itereates through each value contained in it, when running this you'll see that only the selected values are output.

HTH!


Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design

Go to topic 22410

Return to index page 710
Return to index page 709
Return to index page 708
Return to index page 707
Return to index page 706
Return to index page 705
Return to index page 704
Return to index page 703
Return to index page 702
Return to index page 701