2014-05-19 15 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'])); 
} 
?> 

你真的应该提供更准确的答案一些更多的信息。

但是,这应该让你在正确的方向。