2016-04-09 14 views
-1

table.service_master_new,table.department_masterおよびtable.document_masterの3つのテーブルからデータをフェッチしています。jsonでループ応答を作成する方法は?

テーブル構造:

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

しかし、私はデータになりたいです彼らの部署によって整理された:

電気部門は二度来るべきではありません。それは電気部門内の別の配列になければなりません。

+0

「foreach in foreach」(ネストされたforeach)を試すことができます。 –

+0

これを行う際の提案やアプローチ... –

+0

そうするためのアプローチではないのですか? – GolezTrol

答えて

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

これは単なる例に過ぎません。あなた自身でそれを変更することを忘れないでください!

+0

iam jstこれで初心者は、私にしてください。 –

+0

もし私がそれを作るなら、あなたは学ばないでしょう。試してみてください、私はあなたが処理できると確信しています。 –

+0

ok確信してみてください...信頼のためにthnx ...あなたは私にPHPスニペットの儀式をくれましたか? –

関連する問題