forked from rowida22/php_pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.php
54 lines (48 loc) · 2.13 KB
/
script.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
// First of all we have to check if the form is submitted via the POST
// method.
if(isset($_POST['submit'])){
// If the form is submitted we are gonna create a new associative array
// to hold the values we will store.
// var currentId;
$new_message = array(
"id" => intval( $_POST['id']),
"firstname" => $_POST['firstname'],
"lastname" => $_POST['lastname'],
"email" => $_POST['email'],
"password" => $_POST['password'],
"gender" => $_POST['gender'],
"track" => $_POST['track'],
"certifications" => $_POST['certifications']
);
// Before storing the $new_message[] array, we have to check if this is
// the first record.
// We are doing this by checking the filesize.
if(filesize("db.json") == 0){
// if this is the first record, we creating an array to hold out message.
$first_record = array("contantInfo"=>array($new_message));
// The only purpose of this step is to create an array inside the json
// file to hold our messages. You will see in sec.
/* I'll assign the record to a generic variable for later use. */
$data_to_save = $first_record;
}else{
// If this is not the first record, and there are messages stored in the file.
// We have to pull all those old messages so we can add the new one.
// And also we have to decode the data we fetch.
$old_records = json_decode(file_get_contents("db.json"));
// We know that all of our messages are stored inside an array,
// because we created that array with the first record.
// Now we can add to that array our new message.
array_push($old_records->contantInfo, $new_message);
/* and i'll assign the record to our generic variable. */
$data_to_save = $old_records;
}
// Now our last step is to store the data to the file (messages.json).
if(!file_put_contents("db.json", json_encode($data_to_save, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), LOCK_EX)){
// if something went wrong, we are showing an error message.
$error = "Error storing message, please try again";
}else{
// and if everything went ok, we show a success message.
$success = "Message is stored successfully";
}
}