0

There is a Problem When value change of array it works but When I change second value changes made before gone because array redeclare on page loading please give a solution when I submit array value permanently change and I can change much more value of array.

</head>
<body>
    <form action="table5.php" method="post">
<table border="1">
    <?php
    if (isset($_POST['day']) && isset($_POST['time'])){
        $day=$_POST['day'];$time=$_POST['time'];
    }


        $cars = array(
                     array(0,0,0,0,0,0),
                     array(0,0,0,0,0,0),
                     array(0,0,0,0,0,0),
                     array(0,0,0,0,0,0),
                     array(0,0,0,0,0,0),
                     array(0,0,0,0,0,0),
                     array(0,0,0,0,0,0)
                );

            $cars[$time][$day]=1;
            $time++;
            $cars[$time][$day]=2;
            for ($row = 0; $row < 7; $row++) {
               echo "<tr>";
               for ($col = 0; $col < 6; $col++) {
                 echo "<td>".$cars[$row][$col]."</td>";
               }
               echo "</tr>";
             }
    ?><br>
</table>
day<input type="number" name="day">
time<input type="number" name="time">
<input type="submit" name="submit">
</form>
</body>
Ayrton
  • 2,218
  • 1
  • 14
  • 25
  • HTTP requests (and therefore by extension the PHP scripts which respond to them) are _stateless_. It's a concept you need to fully understand in order to be successful at web development. Every time you submit your form, the PHP script runs again from the beginning. All variables are reset. The script has no knowledge that it has ever been executed previously. If you want to keep a record of values the user has submitted previously, then you must consciously take steps to store them somewhere - like a database or a file (e.g. XML or JSON format). This is known as "persistent" storage. – ADyson Feb 15 '19 at 20:32
  • Another option is: If you only need to keep the values during the time the user is currently using the application (and not save for another time) then you can store the values in the PHP Session. The values will then persist until the user's session expires (i.e. usually there's a timeout value, so if the user goes too long between requests to the server, the session will be reset). – ADyson Feb 15 '19 at 20:32
  • Thank you Session Worked! If I want to store data in xml file I need to file Function of php? – Rohit Kanojiya Feb 17 '19 at 04:53
  • If you google about writing to XML files with PHP you'll see lots of examples already, for instance this one: https://stackoverflow.com/questions/2038535/create-new-xml-file-and-write-data-to-it . With a bit of simple research you can often answer your own questions very quickly :-) – ADyson Feb 17 '19 at 10:41

0 Answers0