Click here to Skip to main content
15,915,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?php  
 $message = '';  
 $error = '';  
 if(isset($_POST["submit"]))  
 {  
      if(empty($_POST["name"]))  
      {  
           $error = "Enter name";  
      }  
      else if(empty($_POST["datedon"]))  
      {  
           $error = "Enter Date";  
      }
      else if(empty($_POST["timeon"]))  
      {  
           $error = "Enter Time";  
      }  
      else  
      {  
           if(file_exists('try.json'))  
           {  
                $current_data = file_get_contents('try.json');  
                $array_data = json_decode($current_data, true);  
                $extra = array(  
                     'name'               =>     $_POST['name'],
                     'datedon'     =>     $_POST["datedon"],
                     'timeon'     =>     $_POST["timeon"],  
                );
                $array_data[] = $extra;
                $final_data = json_encode($array_data);
                if(file_put_contents('try.json', $final_data))
                {  
                     $message = "Deals Added Successfully<p></p>";  
                }  
           }  
           else  
           {  
                $error = 'JSON File not exits';  
           }  
      }  
 }  
 ?>


What I have tried:

when ever i get input like name etc. It Should be added at the top not at the botton of json file
Posted
Updated 16-Jun-18 21:56pm
v2

1 solution

Read the following on how to insert to an array : PHP array_unshift() Function[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900