如何在json中创建循环响应?

时间:2016-04-09 05:58:53

标签: javascript php mysql json

我从3个表中获取数据:table.service_master_newtable.department_mastertable.document_master

表格结构:

Structure of Service Master

Structure of Department master

Structure of Document Master

表格中的数据:

Data of service table

data of department table

data of document table

我的用于获取数据的php代码是:

<?php
include 'mysql.php';
$matrix = array();
$getServices = mysqli_query($db,"SELECT * FROM service_master_new");
while($row=mysqli_fetch_array($getServices,MYSQLI_ASSOC)){
    $xmatrix = array();
    $dept_code = $row['dept_code'];
            $getDeptName = mysqli_query($db,"SELECT * FROM department_master WHERE Dept_Code='$dept_code'");
            while($row1 = mysqli_fetch_array($getDeptName,MYSQLI_ASSOC)){
                $xxmatrix = array();
                $dept_name = $row1['Dept_Name'];
                $xmatrix['dept_name'] = $dept_name;
                array_push($xmatrix, $dept_name);

                $service_name = $row['service_name'];
                $xmatrix['service_name'] = $service_name;
                array_push($xmatrix, $service_name);
                $contact_point = $row['contact_point'];
                $xmatrix['contact_point'] = $contact_point;
                array_push($xmatrix, $contact_point);
                $timeline = $row['timeline'];
                $xmatrix['timeline'] = $timeline;
                array_push($xmatrix, $timeline);

                $doc_codes = $row['doc_codes'];
                $xdocs = array();
                $codes = explode(",",$doc_codes);
                $countCodes = count($codes);
                for($i=0;$i<$countCodes;$i++){
                    $getDocs = mysqli_query($db,"SELECT * FROM document_master WHERE Doc_Code='$codes[$i]'");
                    while($row4 = mysqli_fetch_array($getDocs,MYSQLI_ASSOC)){
                        $doc_name = $row4['Doc_Name'];
                        array_push($xdocs, $doc_name);
                    }
                    $xmatrix['docs'] = $xdocs;
                }
        }


    array_push($matrix, $xmatrix);
}
echo json_encode($matrix);
?>

这是我得到的回应:

[
{
0: "Fire",
1: "No Objection Certificate",
2: "O/O Chief Fire Officer",
3: "15",
dept_name: "Fire",
service_name: "No Objection Certificate",
contact_point: "O/O Chief Fire Officer",
timeline: "15",
docs: [
"Record of Rights Form 'F'",
"Sketch Map",
"Approved Building Plan",
"Statutory License to carry business",
"Building Stability Certificate",
"Quantity & Completion certificate of Electrical wiring from recognized Authority"
]
},
{
0: "Electricity",
1: "New Electricity Connection (LT)",
2: "O/O Assistant Engineer, IPP, HQ",
3: "30",
dept_name: "Electricity",
service_name: "New Electricity Connection (LT)",
contact_point: "O/O Assistant Engineer, IPP, HQ",
timeline: "30",
docs: [
"Record of Rights Form 'F'",
"Sketch Map",
"Identity Proof"
]
},
{
0: "Electricity",
1: "New Electricity Connection (HT)",
2: "O/O Assistant Engineer, IPP, HQ",
3: "45",
dept_name: "Electricity",
service_name: "New Electricity Connection (HT)",
contact_point: "O/O Assistant Engineer, IPP, HQ",
timeline: "45",
docs: [
"Record of Rights Form 'F'",
"Sketch Map",
"Identity Proof"
]
},
{
0: "Electricity",
1: "New Electricity Connection (Extra HT)",
2: "O/O Assistant Engineer, IPP, HQ",
3: "180",
dept_name: "Electricity",
service_name: "New Electricity Connection (Extra HT)",
contact_point: "O/O Assistant Engineer, IPP, HQ",
timeline: "180",
docs: [
"Record of Rights Form 'F'",
"Sketch Map",
"Identity Proof"
]
},
{
0: "APWD",
1: "Planning Permissin ",
2: "PBMC / Concerned Panchayat",
3: "15",
dept_name: "APWD",
service_name: "Planning Permissin ",
contact_point: "PBMC / Concerned Panchayat",
timeline: "15",
docs: [
"Record of Rights Form 'F'",
"Sketch Map",
"Building Plan / Site Plan",
"Statement of proposed Development",
"Document of Ownership & title of Property",
"Receipt of Payment of Application Fee"
]
},
{
0: "PBMC",
1: "Planning Permissin ",
2: "PBMC",
3: "160",
dept_name: "PBMC",
service_name: "Planning Permissin ",
contact_point: "PBMC",
timeline: "160",
docs: [
"Building Plan / Site Plan",
"Document of Ownership & title of Property",
"Form of Notice of Intention to errect a Building or to execute any work as prescribed in Appendix 'A'",
"Form of notice of Completion Appendix 'E'",
"Form of Occupancy Certificate Appendix 'F, G & H'",
"Performa of details of Construction of Building around Port Blair Airport, Appendix I, IIA, IIB, IIC, IID, IIE & C"
]
},
{
0: "Labour",
1: "Licence Under the Factories ACT, 1948",
2: "O/O Labour Commissioner",
3: "60",
dept_name: "Labour",
service_name: "Licence Under the Factories ACT, 1948",
contact_point: "O/O Labour Commissioner",
timeline: "60",
docs: [
"Building Plan / Site Plan",
"Receipt of Payment of Application Fee",
"Site Map in the Scale (1cm : 20Mtrs)",
"Flow chart of the Manufacturing Process"
]
},
{
0: "Labour",
1: "Registration Under Contract Labour (R ",
2: "O/O Labour Commissioner",
3: "15",
dept_name: "Labour",
service_name: "Registration Under Contract Labour (R ",
contact_point: "O/O Labour Commissioner",
timeline: "15",
docs: [
"Receipt of Payment of Application Fee",
"Affidavit in Prescribed Format"
]
},
{
0: "Labour",
1: "Registration Under A ",
2: "O/O Labour Commissioner",
3: "30",
dept_name: "Labour",
service_name: "Registration Under A ",
contact_point: "O/O Labour Commissioner",
timeline: "30",
docs: [
"Form D and E as per Annexures"
]
},
{
0: "DC Office",
1: "Land Conversion",
2: "O/O Deputy Commissioner",
3: "28",
dept_name: "DC Office",
service_name: "Land Conversion",
contact_point: "O/O Deputy Commissioner",
timeline: "28",
docs: [
"Record of Rights Form 'F'",
"Sketch Map",
"Document of Ownership & title of Property",
"Project Report",
"Affidavit in Prescribed Format",
"Site Map in the Scale (1cm : 20Mtrs)",
"Photograph of the proposed site with the Signature"
]
},
{
0: "PCC",
1: "Common Consent to Establish, Operate",
2: "O/O A ",
3: "90",
dept_name: "PCC",
service_name: "Common Consent to Establish, Operate",
contact_point: "O/O A ",
timeline: "90",
docs: [
"Record of Rights Form 'F'",
"Sketch Map",
"Building Plan / Site Plan",
"Document of Ownership & title of Property",
"Notarized Affidavit in twenty rupees stamp paper or valuation certificate from Chartered Accountant giving details of Capital Investment including Land, Building and Plant & Machinery without depreciation",
"Registration Certificate of DIC",
"Project Report",
"CRZ-Clearance/Environmental Clearance / Noc from Forest Department of from Revenue Authority showing the distance of the Building / Unit from High Tide Level (HTL)",
"Copy of Permission granted for Ground water withdrawal from Competent Authority"
]
}
]

但我希望数据由他们的部门安排,例如:

电力部门不应该两次;它应该在电力部门内的另一个阵列中。

1 个答案:

答案 0 :(得分:0)

我只是添加一些示例代码,请自行修改。

假设我们有3个不同的表,我们应该从这些表中获取数据并使其成为json输出。这是一个示例代码:

foreach ($table_one as $to) {

    foreach($table_two as $tt) {

        foreach($table_three as $ttr) {

         $data[]        =   array
         (
         'value1' => $ttr['value1'], 
         'value2' => $tt['value2'], 
         'value3' => $to['value3']
         );
         }
    }
}

$json   =   json_encode($data);

请记住,这只是一个例子。别忘了为自己修改它!