Page 1 of 1

Problem with arrays

Posted: Thu Dec 16, 2004 2:45 pm
by Archy
I think this is possible, but I am having a few problems with it.

At the moment, I have a script that has several text boxes, echoed out in a loop. The text boxes have names such as option[$i]; $i being an incremented number each time through the loop.

However, once I try and do something with those number after the page is reloaded, I can only access the lastest number. To show you what I mean, I have a sample of the code below:

Code: Select all

while($noc > 0){
      $order = $order[noc];
      $option = $option[noc];
      $item = $_SESSION[noc];

      $sql = "UPDATE `database` SET `column`='$order', `column`='$option' WHERE `column`='$item'";
      $rs = mysql_query($sql) or die(mysql_error());

      $noc = $noc-1;
   }

...

...

   $i = 1;
   while($row = mysql_fetch_assoc($rs)){
      $id = $row['id'];
      $order = $row['order'];
      $name = $row['name'];

   echo"<br /><input type="text" name="order[$i]" value="$order" size="1" maxlength='3' />
        Option $i: <input type="text" name="option[$i]" value="$name" />";
   $i++
   }
$noc is a version of the incremented number $i.

This code will output this (I modified the script to show this):

9 -- >6, test93,
8 -- >, ,
7 -- >, ,
6 -- >, ,
5 -- >, ,
4 -- >, ,
3 -- >, ,
2 -- >, ,
1 -- >, ,

The numbers on the left are the incrementeres ($i) numbers, then the order[$i], and then the order[$i]. Any help would be appreciated,

Thanks.

Posted: Thu Dec 16, 2004 2:57 pm
by xisle
not sure about the while loop for $noc..
Try a for loop or foreach loop on $orders. Other than that It looks ok.

Code: Select all

foreach($orders as $key=>$value)&#123;

      $order = $value; //obviously
      $option = $option&#1111;$key]
&#125;

Posted: Thu Dec 16, 2004 3:03 pm
by Archy
This was the output of that:

9 -- >1, test21,
8 -- >, s,
7 -- >, ,
6 -- >, ,
5 -- >, ,
4 -- >, ,
3 -- >, ,
2 -- >, ,
1 -- >, ,

Posted: Thu Dec 16, 2004 3:13 pm
by xisle
use phpinfo() to see if the entire array is being posted, make sure you have entered test data for at least 5 rows

Posted: Thu Dec 16, 2004 3:19 pm
by Archy
What am I looking out for in the phpinfo(); ?

Posted: Thu Dec 16, 2004 3:26 pm
by xisle
$HTTP_POST_VARS or $_POST depending on your version
order and option should have keys and values

Posted: Thu Dec 16, 2004 4:12 pm
by Archy
Neither are on there :\

I have PHP/4.3.9