 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP 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
|
|
|
|

October 25th, 2004, 09:43 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to use $_GET with arrays?
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
|
|

October 26th, 2004, 12:16 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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.
|
|

November 1st, 2004, 11:32 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 2nd, 2004, 08:09 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
$_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
|
|

November 20th, 2004, 12:56 PM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
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
|
|
|

November 20th, 2004, 01:10 PM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Tim Gamble
Quote:
|
quote:Originally posted by richard.york
|
Quote:
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? <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? <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
|
|
|
|

November 20th, 2004, 02:06 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
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.
Code:
<?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
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Difference b/w $_GET & $_HTTP_GET_VARS |
ashuphp |
Beginning PHP |
1 |
April 14th, 2007 08:50 AM |
| Problems with $_GET and $_POST |
everetts |
Beginning PHP |
1 |
March 18th, 2007 03:44 AM |
| $_GET |
sudheshna |
Beginning PHP |
1 |
February 18th, 2006 05:22 AM |
| php $_GET |
samon_18 |
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 |
1 |
December 24th, 2005 07:39 PM |
|
 |