使用PHP,JSON和SQL解析数据

时间:2014-05-11 12:14:02

标签: php sql json parsing

我在几乎完成的代码方面遇到了几个问题,但在大多数情况下,我已经纠正了它们。我有两个通知和两个警告,但警告和通知是同一问题的重复,所以更像是一个通知和警告要修复。

以下是代码:

<?php
    ini_set('display_errors',1);ini_set('display_startup_errors',1);error_reporting(-1);
    libxml_use_internal_errors(true);
    date_default_timezone_set('UTC');
    function saveSubteamData(){
        $con=new PDO('mysql:host=localhost;dbname=db','','');
        $query="SELECT userid FROM subteamfolders WHERE status = ?";
        $query=$con->prepare($query);
        $query->execute(array("Approved"));
        $results=$query->fetchAll(PDO::FETCH_ASSOC);
        foreach($results as $user){
            $dom=new DOMDocument();
            $html=file_get_contents('http://example.com/'.$user['userid'].);
            $dom->loadHTML($html);
            $td=$dom->getElementsByTagName('table')->item(6)->getElementsByTagName('tr')->item(1)->getElementsByTagName('td');
            $json=array(
                "id"=>$user['userid'],
                "name"=>$dom->getElementsByTagName('h1')->item(0)->textContent,
                "points"=>$td->item(6)->textContent,
                "ppd"=>$td->item(3)->textContent,
                "wus"=>$td->item(7)->textContent
            );
            $json['date']=date("Y-m-d G:i:s");
            $json=json_encode($json);
            $subteamData=file_put_contents("scripts/subteams.json",$json);
        }
    }
    if(!file_exists('scripts/subteams.json')){
        file_put_contents('scripts/subteams.json', '');
    }
    $subteamData=json_decode(file_get_contents('scripts/subteams.json'));
    if(is_null($subteamData)){
        $subteamData=(object)array();
    }
    if(isset($subteamData->date)){
        $hours=(strtotime(date("Y-m-d G:i:s"))-strtotime($subteamData->date))/3600;
        if($hours>=3){
            saveSubteamData();
        }
    }else{
        saveSubteamData();
    }?>
    <table>
        <tr>
            <td>Sub-Team</td>
            <td>Members</td>
            <td>Points</td>
            <td>PPD</td>
        </tr>
        <?php foreach($subteamData->subteams as $subteams){?>
            <tr>
                <td><?php echo $subteams->name;?></td>
                <td><?php echo count($subteamData->subteams);?></td>
                <td><?php echo $subteams->points;?></td>
                <td><?php echo $subteams->ppd;?></td>
            </tr>
        <?php }?>
    </table>
    <br>
    <br>
    <hr>
    <br>
    <?php foreach($subteamData->subteams as $subteams){?>
        <img src="/images/<?php echo $subteams->name;?>" title="<?php echo $subteams->name;?>" alt="<?php echo $subteams->name;?>">
        <br>
        <br>
        <table>
            <tr>
                <td>Username</td>
                <td>Points</td>
                <td>PPD</td>
                <td>WUs</td>
            </tr>
            <?php foreach($subteams->members as $members){?>
                <tr>
                    <td><a href='http://folding.extremeoverclocking.com/user_summary.php?s=&u=<?php echo $members->id;?>' target='_blank' title='<?php echo $members->name;?>'><?php echo $members->name;?></a></td>
                    <td><?php echo $members->points;?></td>
                    <td><?php echo $members->ppd;?></td>
                    <td><?php echo $members->wus;?></td>
                </tr>
            <?php }?>
        </table>
    <?php }
?>

我得到以下内容:

Notice: Undefined property: stdClass::$subteams
Warning: Invalid argument supplied for foreach()

这两条消息都在第56行,然后为第69行显示相同的消息。第56和69行:

foreach($subteamData->subteams as $subteams){
foreach($subteamData->subteams as $subteams){

使用不存在的JSON文件执行print_r会给我:

stdClass Object ( )

使用现有文件:

stdClass Object ( [id] => 649746 [name] => userName [points] => 845,518 [ppd] => 0 [wus] => 74 [date] => 2014-05-11 12:02:50 )

因此,除了我收到的消息之外,我还应该保存一个用户的详细信息,而应该还有更多。

1 个答案:

答案 0 :(得分:0)

foreach无法处理

stdClass 。只有数组。

更改行

$subteamData=json_decode(file_get_contents('scripts/subteams.json'));

$subteamData=json_decode(file_get_contents('scripts/subteams.json', TRUE));
  

混合json_decode(字符串$ json [,bool $ assoc = false [,int $ depth   = 512 [,int $ options = 0]]])

     

...

     

assoc当 TRUE 时,返回的对象将转换为关联对象   阵列。

参考:PHP