2014-05-19 34 views
-1

進出口新的PHP和我做了這個展示給我的網站是什麼已經在FDPP門戶使用的foreach做一個表

<?php $resp = file_get_contents("http://fdpp.blgs.gov.ph/api/documents?source=datatable&sSearch=kalinga"); 
$clean = json_decode($resp); 

print_r($clean); ?> 

這被上傳的是結果:

stdClass Object 
(
    [iTotalRecords] => 130035 
    [iTotalDisplayRecords] => 879 
    [sEcho] => 0 
    [aaData] => Array 
     (
      [0] => stdClass Object 
       (
        [lgu] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129293'>CAR<br/>Kalinga<br />Balbalan</a> 
        [document] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129293'>Local Disaster Risk Reduction and Management Fund Utilization (LDRRMF)</a> 
        [period] => Quarter 1 2014 
        [status] => Required &bull; SUBMITTED 
        [desc] => The atng. 
       ) 

      [1] => stdClass Object 
       (
        [lgu] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129188'>CAR<br/>Kalinga<br />Balbalan</a> 
        [document] => <a href=' http://fdpp.blgs.gov.ph/documents/view/129188'>Manpower Complement</a> 
        [period] => Quarter 1 2014 
        [status] => Required &bull; SUBMITTED 
        [desc] => The file ag. 
       ) 

什麼我應該添加到我的代碼中,把它放在列名爲lgu,document,period的表中嗎?我嘗試閱讀foreach手冊,但我無法弄清楚有人可以幫我嗎?

+1

沒有你的嘗試,並告訴我們你卡在哪裏,這幾乎不可能幫助你。 – PeeHaa

+0

澄清,似乎他試圖在服務器端處理中使用jQuery DataTables庫。 [這是一個例子](http://datatables.net/examples/server_side/simple.html)。 – mason

回答

0

我不得不同意PeeHaa(+1)沒有足夠的信息,但我會試圖幫助你。

以下假設:

  1. 你有一個本地的MySQL數據庫名爲 「testdb的」
  2. 你有PDO擴展安裝
  3. 在 「TESTDB」,你有一個表名爲「TEST_TABLE 「與4個 列(ID,LGU,文檔,週期)

首先,你需要在json_decode設置第二個參數爲true,返回一個關聯數組而不是對象(PHP: json_decode)

因此,基於有限的信息,這裏是一個工作版本:

<?php 
$resp = file_get_contents("http://fdpp.blgs.gov.ph/api/documents?source=datatable&sSearch=kalinga"); 
$clean = json_decode($resp,true); 

// Open connection to your MySQL DB 
$db = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password'); 

// Parse the now-associative array and insert into table 
foreach($clean['aaData'] as $doc){ 
    $stmt = $db->prepare("INSERT INTO test_table(lgu,document,period) VALUES(:lgu,:document,:period)"); 
    $stmt->execute(array(':lgu' => $doc['lgu'], ':document' => $doc['document'], ':period' => $doc['period'])); 
} 
?> 

你真的應該提供更準確的答案一些更多的信息。

但是,這應該讓你在正確的方向。

相關問題