Jump to content

Array problem


margarette_a

Recommended Posts

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 would be:

 

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..

 

 
<!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>

Link to comment
https://forums.phpfreaks.com/topic/172925-array-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.