FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Multidimensional arrays





woodenbrick
I want to create a multidimensional array for some textboxes, all with the same name
Code:

//This sits in a loop so there are many textboxes named qty[]
Qty:<input type='text' size='2' name='qty[]' value='1'>

//on the process page
$id=$_GET['id'];
$qty=$_POST['qty'][$id];

I've been searching for ages trying to find out what's wrong, can anyone point me in the right direction?
Stubru Freak
woodenbrick wrote:
I want to create a multidimensional array for some textboxes, all with the same name
Code:

//This sits in a loop so there are many textboxes named qty[]
Qty:<input type='text' size='2' name='qty[]' value='1'>

//on the process page
$id=$_GET['id'];
$qty=$_POST['qty'][$id];

I've been searching for ages trying to find out what's wrong, can anyone point me in the right direction?


There's nothing wrong with the code in itself I think. What are you trying to accomplish?
woodenbrick
Well I want to put $qty into a database to track how many of each item someone has bought, but it always comes out as zero no matter what quantity I enter.
AftershockVibe
I'm confused. Why should it be an array and not just an integer then?
kv
I don't see anything wrong with that code. But may be $id is having a wrong value. Just print the value of $id and see if it is empty or the index is out of range.

You may also want to convert $id into integer before using it ( $id+0 will do), since the array values are indexed against integer, not string.

Eg:
Code:

<input type="text" name="test[]" value="A">
<input type="text" name="test[]" value="B">
<input type="text" name="test[]" value="C">
<input type="text" name="test[]" value="D">

$test = $_POST['test'];
// In PHP, the test is an array like
array(
0 => "A",
1 => "B",
2 => "C",
3 => "D"
)
woodenbrick
For those who are confused about exactly what I am doing here is a link:
shopping cart
Also I put up the source code as well.
kv, I've tried this with $id, it's definitely an integer value and not empty.
Stubru Freak
When clicking the link, you're not actually submitting the form, you're just loading page process.php?id=1.
You should change the Add to cart button to a submit button.
woodenbrick
Ok thanks Stubru I am a bit of a tard but have it sort of working now, will finish it off when I have time.
Thanks for your help!
Related topics
PHP Write to line
[php scripts ] phpweather&email
Multidimensional Arrays & passing info between windows
Visual Basic Tutorial - Control Arrays: What, Why and How
C program with 3D array, in Linux
PHP page from arrays displays nothing, what's going on?
Is it safe to edit arrays?
Php Arrays :((((((((
10 frih$ contest: what does this c++ code do?
Inserting PHP multidimensional arrays into a mySQL database
-> and =>
[PHP] Some beginner questions
Generic arrays in Java
Array in php
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.