2016-04-27 4 views
1

Ich habe ein nettes einfaches Bit von PHP, das Daten aus meiner Datenbank-View ergreift und eine JSON-Datei erstellt und dann auf meinen Server schreibt.Einfügen von Datensätzen zwischen JSON-Einträgen mit PHP

Es ist eine Liste von Mitarbeitern aus 5 verschiedenen Unternehmen.

Was ich gerne tun könnte, ist, bevor der erste Angestellte jedes Unternehmens, das zurückgegeben wird, ist ein Element, das die Firma (einschließlich eines Logos) darstellt, einzufügen.

So wäre es etwa so aussehen ...

UNTERNEHMEN 1 EMPLOYEE EMPLOYEE EMPLOYEE UNTERNEHMEN 2 EMPLOYEE UNTERNEHMEN 3 EMPLOYEE EMPLOYEE

Im Moment arbeite ich gerade bin alle Mitarbeiter bekommen.

Das ist mein PHP, die die 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); 


?> 

Hier ist meine JSON Probe zurück erzeugt.

[{ 
    "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" 
}] 

Ich habe zwei Tabellen in der Datenbank, eine Tabelle Firma, die die URL des Bildes I enthält und eine Tabelle Mitarbeiter wollen, dass alle Mitarbeiterdaten hat. Hier

ist die Gesellschaft Tabellenstruktur

enter image description here

Hier wird die Mitarbeitertabellenstruktur

enter image description here

ich dann tun ein Inner auf denen Sie Mitglied meiner Datenbank Ansicht zu erstellen, die aufgerufen wird im PHP-Beispiel.

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`; 

So kann meine erwartete Ausgabe in etwa so aussehen, wo ein „IS_COMPANY“ und fügen hinzu: „1“ in die JSON konnte mein app helfen festzustellen, ob das Image des Unternehmens oder Mitarbeiter Bild anzuzeigen.

[{ 
     "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" 
    }] 

lese ich dann diese JSON-Datei in einen iOS-App den Inhalt anzuzeigen.

Irgendwelche Ideen, wie ich in der Lage sein könnte, diese zusätzlichen Knoten hinzuzufügen?

Vielen Dank

Simon

+0

Sie können ein (wahrscheinlich ein LEFT JOIN?) JOIN. Ohne die Struktur Ihrer Tabellen zu sehen, ist es schwer vorstellbar, wie. – fusion3k

+0

Aktuelle JOIN- und Tabellenstrukturen, die jetzt zu der ursprünglichen Frage hinzugefügt wurden @ fusion3k –

+0

Fügen Sie Ihrer Abfrage 'company_image' hinzu und folgen Sie dann dem folgenden Antwortschema. – fusion3k

Antwort

0

So etwas könnte den Trick tun:

<?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

Das funktioniert in dem Sinne, dass es Werte an der richtigen Stelle innerhalb der JSON setzt, das einzige Problem ist, sie sind alle NULL –

+0

und natürlich sind Sie derjenige, der die '$ json_array_company' Variable füllen muss. Ich werde versuchen, es im Beispiel etwas klarer zu machen. – Naryl

+0

Ich habe es, verpasst nur diese Linie, danke! –

Verwandte Themen