Problem with arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Problem with arrays

Post 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.
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post 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;
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

This was the output of that:

9 -- >1, test21,
8 -- >, s,
7 -- >, ,
6 -- >, ,
5 -- >, ,
4 -- >, ,
3 -- >, ,
2 -- >, ,
1 -- >, ,
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post 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
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

What am I looking out for in the phpinfo(); ?
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

$HTTP_POST_VARS or $_POST depending on your version
order and option should have keys and values
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

Neither are on there :\

I have PHP/4.3.9
Post Reply