2012-02-27 41 views
1

任何人都知道如何做到这一点?Php stdClass Object to html list

下面我有存储在一个变量的代码,并想呼应它以不同的方式,如:按国家

  • 和值的
  • 由城市
  • 和值的总和通过状态值

是这样的:

国家一个:220
国家B:80
国家-C:70

状态-A:220
状态-B:80
状态-C:70

城市-A:220
城市-b:80
城市-C:70

stdClass Object 
(
    [city-a, state-a, country-a] => 100 
    [city-a, state-a, country-a] => 120 
    [city-b, state-b, country-b] => 80 
    [city-c, state-c, country-c] => 70 

) 

甲恭喜你的帮助!

+3

refor将传入数据抹掉会使这变得更容易,数据源是什么? – 2012-02-27 03:57:05

+2

结构是如此不合适我会觉得肮脏给出一个答案。 – h0tw1r3 2012-02-27 04:13:20

+0

@Dagon:数据源来自Facebook的Graph API - Insights - page_fans_city。 developers.facebook.com/docs/reference/fql/insights(阅读洞察权限需要 - 你可以看看在FB API资源管理器https://developers.facebook.com/tools/explorer) – 2012-02-27 04:56:07

回答

1

如果您不了解代码或需要其他功能帮助,请告诉我。 我也缩短了json代码。你也许可以为这个类实现更多的功能,并为你自己创建一个活泼的FB-stats接口。

//编辑: 好的,我为你创建了一个小的API接口。它包含一个包含文件,一个使用示例和2个example-json-files。请按照以下方式命名文件,并将它们全部放在同一个文件夹中。

的index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<title>Your title</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 

<body> 

<?php 
//include files 
include 'Stats.php'; 

//create an object of Stats 
$stats = new Stats(); 

//parse any number of json files 
$stats->parseJson("json1.txt"); 
$stats->parseJson("json2.txt"); 

//call the functions 

print "<h1>var_dump of the data</h1>"; 

print '<pre>' . print_r($stats->getList(), true) . '</pre>'; 
print "<h1>By Country</h1>"; 
print "Austria:".$stats->getSumByCountry("Austria")."<br />"; 
print "Germany:".$stats->getSumByCountry("Germany")."<br />"; 
print "USA:".$stats->getSumByCountry("USA")."<br />"; 

print "<h1>By State</h1>"; 
print "Tirol:".$stats->getSumByState("Tirol")."<br />"; 
print "Bayern:".$stats->getSumByState("Bayern")."<br />"; 
print "Massachusetts:".$stats->getSumByState("Massachusetts")."<br />"; 

print "<h1>By City</h1>"; 
print "Austria:".$stats->getSumByCity("Imst")."<br />"; 
print "Berlin:".$stats->getSumByCity("Berlin")."<br />"; 
print "Los Angeles:".$stats->getSumByCity("Los Angeles")."<br />"; 




?> 
</body> 
</html> 

Stats.php:

<?php 
class Stats{ 
    private $cities; 

    public function addCity($city){ 
     $this->cities[]=$city; 
    } 


    public function parseJson($jsonFile){ 
     $jsonData = file_get_contents($jsonFile); 
     if($jsonData == null || $jsonData==""){ 
      die("Error: failed to load $jsonFile"); 
     } 

     $object = json_decode($jsonData); 
     //naviagte through the object to the data we want to have 
     $data = $object->data; 
     $temp_item = $data[0]; 
     $temp_value = $temp_item->values; 
     $temp_item2 = $temp_value[0]; 
     $value = $temp_item2->value; 

     //now create an entity of City for each entry and put it in the list of this class 
     foreach ($value as $key=> $item){ 
      $placeArray = explode(",",$key); 
      $city = trim($placeArray[0]); 
      $state = trim($placeArray[1]); 
      $country = trim($placeArray[2]); 
      $sum = $item; 
      $this->addCity(new City($sum, $city, $state, $country)); 
     } 
    } 

    public function getSumByCity($name){ 
     $sum = 0; 
     foreach ($this->cities as $city){ 
      if(strcmp($city->name,$name) == 0){ 
       $sum += $city->sum; 
      } 
     } 
     return $sum; 
    } 

    public function getSumByState($state){ 
     $sum = 0; 
     foreach ($this->cities as $city){ 
      echo ""; 
      if(strcmp($city->state,$state) == 0){ 
       $sum += $city->sum; 
      } 
     } 
     return $sum; 
    } 
    public function getSumByCountry($country){ 
     $sum = 0; 
     foreach ($this->cities as $city){ 
      if(strcmp($city->country,$country) == 0){ 
       $sum += $city->sum; 
      } 
     } 
     return $sum; 
    } 

    public function getList(){ 
     return $this->cities; 
    } 
} 

class City{ 
    public $sum, $name, $state, $country; 
    function __Construct($sum, $name, $state, $country){ 
     $this->name = $name; 
     $this->sum = $sum; 
     $this->state = $state; 
     $this->country = $country; 
    } 
} 
?> 

json1.txt

{"data":[ 
    {"id":"SOMEID/insights/page_fans_city/lifetime", 
    "name":"page_fans_city", 
    "period":"lifetime", 
    "values":[ 
     {"value": 
      {"Wattens, Tirol, Austria":160, 
      "Imst, Tirol, Austria":123, 
      "Zirl, Tirol, Austria":117}, 
     "end_time":""}], 
    "title":"", 
    "description":""}], 
"paging": 
    {"previous":"", 
    "next":""} 
} 

json2.txt:

{"data":[ 
    {"id":"SOMEID/insights/page_fans_city/lifetime", 
    "name":"page_fans_city", 
    "period":"lifetime", 
    "values":[ 
     {"value": 
      {"Boston, Massachusetts, USA":199, 
      "Los Angeles, California, USA":55}, 
     "end_time":""}], 
    "title":"", 
    "description":""}], 
"paging": 
    {"previous":"", 
    "next":""} 
} 
+0

一万@Johannes但不幸的是,我不能让它开始工作。我所做的只是file_get_contents(PATH TO JSON DATA),然后将其放入json_decode()中。此外,是否有可能将它自动分组在一起,以便我可以为不同的JSON输入(不同的councries,城市等)运行该代码。再次感谢您的帮助,非常感谢! PS。我得到的错误是:为foreach() – 2012-02-27 07:49:34

+0

提供的无效参数错误是因为数组未正确填充。如果你只做file_get_contents(http://到整个json的路径),当然你必须从最上面的节点导航没有国家/城市的数据;) 我的代码不是精确的复制和粘贴。而且您可以重复使用代码进行多个json输入!请尝试:$ json-> file_get_contents(路径到JSON数据)$ nodeX = $ json-> NodeX .... $ jsonDate - > $ Node_OF_COUNTRIES。 或等待10小时,直到我从办公室回家; – 2012-02-27 17:43:31

+0

嗨@Johannes,谢谢你,但现在我完全困惑:(对不起,但你可以告诉 - 我是PHP的新手。请帮我实施它? – 2012-02-27 21:09:16