I have looked at both the posts here so far and found them quite helpful although have become stuck: Parsing JSON to MySQL table and https://www.daniweb.com/programming/web-development/threads/381669/json-to-mysql-with-php
Originally I started with an html table created from the JSON data:
echo "<h1>Table name: ". $sheetObj->name ."</h1>";
echo "<br>";
//table start
echo "<table>";
foreach ($sheetObj->columns as $column) {
echo "<td>".$column->title ."</td>";
}
echo "<tr>";
foreach ($sheetObj->rows as $row) {
foreach ($row->cells as $cell)
{
echo "<td>".$cell->displayValue."</td>";
}
echo "<td></td><td></td><td></td>";
echo "</tr>";
}
echo "</table>";*/
The aim was then to take this data and place it straight in to a Mysql database rather than creating an html table.
And have produced this code so far:
$getSheetResponseData = curl_exec($curlSession);
$sheetObj = json_decode($getSheetResponseData);
curl_close($curlSession);
// Connect to the database
$dbconn = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$global_arr = array(); // Contains decoded json (ROWS
$global_keys = array(); // Contains decoded json (COLUMNS)
foreach ($sheetObj->columns as $column) {
$global_keys[$column->title] = '';
}
$selected = mysql_select_db("Database1",$dbconn)
or die("Could not select Database..");
// CREATE SQL TABLE
$query = "CREATE TABLE IF NOT EXISTS `json_table` (
`id` int(11) unsigned NOT NULL auto_increment,";
foreach($global_keys as $key => $val)
{
$query .= "
`$key` varchar(100) NOT NULL default '' ,";
}
$query .= "PRIMARY KEY (`id`))";
mysql_query($query) or die('mysql_error: '.mysql_error());
for($i=0; $i<count($global_arr); $i++)
{
foreach($global_arr[$i] as $key => $value){
$sql[] = (is_numeric($value)) ? "`$key` = $value" : "`$key` = '" . mysql_real_escape_string($value) . "'";
}
$sqlclause = implode(",",$sql);
$rs = mysql_query("INSERT INTO `json_table` SET $sqlclause");
}
It is the second part of the code where I attempt to add the row values to the correct columns that I am having difficulty.
foreach ($sheetObj->rows as $row) {
foreach ($row->cells as $cell)
{
$global_arr[$cell->displayValue];
}
}
Any advice or help as to how I can get the row data to enter the correct columns would be great!
Thanks in advance.
===========================================================================
So I have come up with this code so far, it feels very long winded and clumbsy but works.
<?php
$global_keys = array(); // Contains columns for SQL
// Grabs column names from JSON
foreach ($sheetObj->columns as $column) {
$global_keys[$column->title] = '';
}
$selected = mysql_select_db("MYDatabase",$dbconn)
or die("Could not select MYDatabase..");
// Deletes TABLE
$query = "DROP TABLE IF EXISTS `MYtable`";
$q1=mysql_query($query);
if($q1){echo "deleted the table MYtable....<br>";}
else{echo mysql_error();}
// Creates TABLE with new data
$query = "CREATE TABLE IF NOT EXISTS `MYtable` (
`id` int(11) unsigned NOT NULL auto_increment,";
foreach($global_keys as $key => $val)
{
$query .= "
`$key` varchar(100) NOT NULL default '' ,";
}
$query .= "
PRIMARY KEY (`id`)
)";
mysql_query($query) or die('mysql_error: '.mysql_error());
//////////////////////////////////////////////////////
//Puts all column names in an array
$a = array();
foreach ($sheetObj->columns as $column)
{
if(isset($column->title)){
$a[]= $column->title;
}else{
$a[]= nul;
}
}
//Puts all row values in an array
$b = array();
foreach($sheetObj->rows as $row)
{
foreach ($row->cells as $cell) {
if(isset($cell->displayValue)){
$b[]= $cell->displayValue;
}else{
$b[]= nul;
}
}
}
// Merge the 2 arrays together using the column names as keys
$rep_array = $b;
$rep_keys = $a;
function combine5 ($keys, $array)
{
$tmp = array_chunk($array, 5);
$result = array();
foreach ($tmp as $data)
{
$result[] = array_combine ($keys, $data);
}
return $result;
}
$new = combine5($rep_keys, $rep_array);
//Place the correct row data in to the correct columns (cycle through till complete)
foreach($new as $item) {
mysql_query("INSERT INTO `MYDatabase`.`MYtable` (`id`, `Field1`, `Field2`, `Field3`, `Field4`, `Field5`)
VALUES ( NULL, '".$item['Field1']."', '".$item['Field2']."', '".$item['Field3']."', '".$item['Field4']."', '".$item['Field5']."');");
}
?>
json_decode? would need to see at least part of your json to understand how is it displayed. secondly instead of having insert query for each row insert all of them together in 1 query [few if its a large json]