2016-04-27 68 views
1

我已經獲得了一個簡單的PHP位置,它從我的數據庫中抓取數據查看並生成一個JSON文件,然後將其寫入到我的服務器。使用PHP在JSON條目之間插入記錄

這是來自5個不同公司的員工名單。

我希望能夠做的是在每個獲得退貨的公司的第一名員工之前插入代表公司的商品(包括標識)。

所以,它會是這個樣子......

公司1個 EMPLOYEE EMPLOYEE EMPLOYEE 公司2 EMPLOYEE 公司3 EMPLOYEE EMPLOYEE

目前我只是得到所有的員工。

這是我的PHP產生JSON

<?php  
$db=new PDO('mysql:dbname=DB;host=localhost;','username','pass');  
$row=$db->prepare('select * from Employee_JSON'); 

$row->execute();//execute the query 
$json_data=array();//create the array 
foreach($row as $rec)//foreach loop 
{ 
$json_array['employee_id']=$rec['employee_id']; 
    $json_array['employee_firstname']=$rec['employee_firstname']; 
    $json_array['employee_surname']=$rec['employee_surname']; 
    $json_array['employee_image']=$rec['employee_image']; 
    $json_array['employee_status']=$rec['employee_status']; 
    $json_array['company_id']=$rec['company_id']; 
    $json_array['company_name']=$rec['company_name']; 
    $json_array['company_hex']=$rec['company_hex']; 
//here pushing the values in to an array 
    array_push($json_data,$json_array); 

} 

//built in PHP function to encode the data in to JSON format 
echo json_encode($json_data); 

//write to json file 
$fp = fopen('data/employees.json', 'w'); 
fwrite($fp, json_encode($json_data)); 
fclose($fp); 


?> 

這裏是我的JSON樣品返回。

[{ 
    "employee_id": "9", 
    "employee_firstname": "Test", 
    "employee_surname": "Name", 
    "employee_image": "9.jpg", 
    "employee_status": "1", 
    "company_id": "1", 
    "company_name": "CDE", 
    "company_hex": "99C440" 
}, { 
    "employee_id": "49", 
    "employee_firstname": "Testy", 
    "employee_surname": "Test", 
    "employee_image": "ce_holding.png", 
    "employee_status": "1", 
    "company_id": "1", 
    "company_name": "CDE", 
    "company_hex": "99C440" 
}, { 
    "employee_id": "8", 
    "employee_firstname": "Tester", 
    "employee_surname": "McTest", 
    "employee_image": "8.jpg", 
    "employee_status": "1", 
    "company_id": "1", 
    "company_name": "CDE", 
    "company_hex": "99C440" 
}] 

我在數據庫中有兩個表,包含我想要的圖像,並具有所有員工數據的Employee表的URL一個公司表。

下面是本公司的表結構

enter image description here

這裏是Employee表結構

enter image description here

然後我做了一個內部聯接那些創建我的數據庫視圖被稱爲在PHP示例中。

CREATE VIEW `Employee_JSON` 
AS SELECT 
    `Employee`.`employee_id` AS `employee_id`, 
    `Employee`.`employee_firstname` AS `employee_firstname`, 
    `Employee`.`employee_surname` AS `employee_surname`, 
    `Employee`.`employee_image` AS `employee_image`, 
    `Employee`.`employee_status` AS `employee_status`, 
    `Company`.`company_id` AS `company_id`, 
    `Company`.`company_name` AS `company_name`, 
    `Company`.`company_hex` AS `company_hex` 
FROM (`Employee` join `Company` on((`Employee`.`employee_company_id` = `Company`.`company_id`))) where (`Employee`.`employee_active` = 1) order by `Company`.`company_name`,`Employee`.`employee_surname`,`Employee`.`employee_firstname`; 

所以我預期的輸出可能是這個樣子,在那裏加入了「is_company」:「1」到JSON能幫助我的應用程序檢測是否顯示公司形象或員工的形象。

[{ 
     "employee_id": "0", 
     "employee_firstname": "Company", 
     "employee_surname": "Name", 
     "employee_image": "companyimage.jpg", 
     "employee_status": "1", 
     "company_id": "1", 
     "company_name": "CDE", 
     "is_company": "1", 
     "company_hex": "99C440", 

    }, { 
     "employee_id": "9", 
     "employee_firstname": "Test", 
     "employee_surname": "Name", 
     "employee_image": "9.jpg", 
     "employee_status": "1", 
     "company_id": "1", 
     "company_name": "CDE", 
     "company_hex": "99C440" 
    }, { 
     "employee_id": "49", 
     "employee_firstname": "Testy", 
     "employee_surname": "Test", 
     "employee_image": "ce_holding.png", 
     "employee_status": "1", 
     "company_id": "1", 
     "company_name": "CDE", 
     "company_hex": "99C440" 
    }, { 
     "employee_id": "8", 
     "employee_firstname": "Tester", 
     "employee_surname": "McTest", 
     "employee_image": "8.jpg", 
     "employee_status": "1", 
     "company_id": "1", 
     "company_name": "CDE", 
     "company_hex": "99C440" 
    }] 

然後我將這個JSON文件讀入iOS應用程序來顯示內容。

任何想法如何能夠實現在這些額外的節點添加?

非常感謝

西蒙

+0

您可以使用JOIN(可能是一個LEFT JOIN?)。沒有看到你的表格結構,很難想象如何。 – fusion3k

+0

當前JOIN和表結構現在已添加到原始問題@ fusion3k –

+0

將'company_image'添加到您的查詢中,然後按照下面的答案模式。 – fusion3k

回答

0

像這樣的事情可能做的伎倆:

<?php  
//Before this you should have the data you want to insert in the JSON of each company. I guess you should get it from a database. 
$json_data_c=array(); 
foreach($row as $rec_company){ //I supose you have the information in the $row variable after a query to the database 
    $json_array['company_field1']=$rec_company['company_field1']; 
    .... 
    $json_data_c[$rec_company['company_id']]=$json_array; 
} 

//end of company information 

$db=new PDO('mysql:dbname=DB;host=localhost;','username','pass');  
$row=$db->prepare('select * from Employee_JSON'); 

$row->execute();//execute the query 
$json_data=array();//create the array 
$company_id_loop=-1; 
foreach($row as $rec)//foreach loop 
{ 
    if($company_id_loop!=$rec['company_id']){ 
     //insert here the json with the data of the company $rec['company_id'] 
     $json_array_company=$json_data_c[$rec['company_id']]; 
     array_push($json_data, $json_array_company); 
     $company_id_loop=$rec['company_id']; 
    } 
$json_array['employee_id']=$rec['employee_id']; 
    $json_array['employee_firstname']=$rec['employee_firstname']; 
    $json_array['employee_surname']=$rec['employee_surname']; 
    $json_array['employee_image']=$rec['employee_image']; 
    $json_array['employee_status']=$rec['employee_status']; 
    $json_array['company_id']=$rec['company_id']; 
    $json_array['company_name']=$rec['company_name']; 
    $json_array['company_hex']=$rec['company_hex']; 
//here pushing the values in to an array 
    array_push($json_data,$json_array); 

} 

//built in PHP function to encode the data in to JSON format 
echo json_encode($json_data); 

//write to json file 
$fp = fopen('data/employees.json', 'w'); 
fwrite($fp, json_encode($json_data)); 
fclose($fp); 


?> 
+0

它的作用在於它將值放入JSON中的正確位置,唯一的問題是,它們都是NULL –

+0

當然,您必須填充'$ json_array_company'變量。我會盡量在這個例子中更清楚一點。 – Naryl

+0

我明白了,只是錯過了那條線,謝謝! –