Array problem

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
margarette_a
Forum Newbie
Posts: 2
Joined: Wed Sep 02, 2009 9:11 pm

Array problem

Post by margarette_a »

good day to all!

I'm kinda new at coding using php..
and I have a problem with the expected output of my code below..

For example, at first run..
I will input 'A' on the textfield named 'plate'
when I press the button Park, the output is like this:

Array ([0] => A [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )

then when I input 'B' then pressed 'Park'....
the output is like this....

Array ([0] => B [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )


I was kinda expecting the output to be like this..

Array ([0] => A [1] =>B [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => )

The problem is that the value that I will input is always saved at the FIRST index of my array everytime I press Park..
I was expecting that when I pressed 'PARK', the value that I will input will be saved to the next index and not overwrite the value of the first index of the array..

our prof has clearly specified that we should not use session when solving this machine problem...is it possible without using sessions?

any ideas?
I would really appreciate it. thank you! (^_^)

here's my code..

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 
<title>Untitled Document</title>
</head>
 
<body>
<?php
$arr= array("","","","","","","","","","");
 
?>
 
<form method="POST" action="index.php"> 
Plate #:  <input name="plate" type="text" ><br/><br/>
<input name="park" type="submit" value="PARK"> 
</form>
<?php
 
 
if(isset($_POST['park']))  
{
$plate= $_POST["plate"];
 
$flag=0;
 
for($i=0; $i<10;$i++){
 
  
  if(($arr[$i]=="") && ($flag==0)){
  $arr[$i]= $plate;
  $flag=1;
  }
 
}
 
print_r($arr);
 
 
}
?>
</body>
</html>
 
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Array problem

Post by AlanG »

In order to append to the array, you need to store the current array somewhere. You don't need to use sessions, but you will need to use something. Options include the url, cookies, files, database, or the post method (storing the current array in a hidden field for example).

I'll show you an example using a hidden field and serializing the array.

Code: Select all

<?php
$arr= array();
 
if(isset($_POST['myarray']) && !empty($_POST['myarray'])) {
    $myarray = unserialize($_POST['myarray']);
    $arr = $myarray;
}
if(isset($_POST['park']) && !empty($_POST['park'])) {
    $arr[] = $_POST['park'];
}
$myarray = (!empty($arr)) ? serialize($arr) : '';
?>
 
<form method="POST" action="index.php">
<input type="hidden" name="myarray" value="<?php echo $myarray; ?>"/>
Plate #:  <input name="plate" type="text" ><br/><br/>
<input name="park" type="submit" value="PARK">
</form>
 
margarette_a
Forum Newbie
Posts: 2
Joined: Wed Sep 02, 2009 9:11 pm

Re: Array problem

Post by margarette_a »

@AlanG
thank you so much for the response..
God Bless you..(^_^)
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Array problem

Post by AlanG »

margarette_a wrote:@AlanG
thank you so much for the response..
God Bless you..(^_^)
Your welcome. :)
Post Reply