<?php

/*
$array = array("my", "litte", "array", 2);

$serialized_array = serialize($array);
$unserialized_array = unserialize($serialized_array);

var_dump($serialized_array); // gives back a string, perfectly for db saving!
var_dump($unserialized_array); // gives back the array again
*/


$allClientsfile = "data/contacts.json"; //Get existing JSON file

$jsondata = file_get_contents($allClientsfile); //Get data from existing JSON file
$arr_data = json_decode($jsondata, true); // converts existing JSON data into an Array

var_dump($arr_data); // gives back a string, perfectly for db saving!
echo "<br>";
$size = sizeof($arr_data); //get total number of clients in the exiting client array and display
//echo "<BR> Existing Contact Array Size: " . $size . "<BR>";

$serialized_array = serialize($arr_data);

$unserialized_array = unserialize($serialized_array);

var_dump($serialized_array); // gives back a string, perfectly for db saving!
echo "<br>";
var_dump($unserialized_array); // gives back the array again


// Insert record
foreach($arr_data as $userid=>$user){


  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  // prepare sql and bind parameters
  $stmt = $conn->prepare("INSERT INTO SMSContacts (clientID, firstname, lastname, initials, type, email, phone, notes, localdatetime )
            VALUES (:clientId, :firstname, :lastname, :initials, :type, :email, :phone, :notes, :localdateTime )");

   $username = $id['clientID'];
   $name = $user['name'];

   $sql = "INSERT INTO contents_arr(username,name) VALUES('".$username."','".$name."')";
   mysqli_query($con,$sql);

}

// Read record
$sql = mysqli_query($con,"SELECT * FROM contents_arr");
while($row = mysqli_fetch_assoc($sql)){
   $username = $row['username'];
   $name = $row['name'];

   echo "username : ".$username.", name : ".$name."<br>";
}

?>
