未定义的偏移量:0

时间:2015-08-03 09:10:07

标签: javascript php facebook facebook-graph-api

我想获得简单的fb图数据并将其加载到带有php的表中,但不知怎的,我收到4个相同的错误消息,为什么如此,我不知道它们是什么意思。

注意:未定义的偏移量:第52行的index.php中的0

注意:未定义的偏移量:第55行的index.php中的0

注意:未定义的偏移量:第59行的index.php中的0

注意:未定义的偏移量:第60行的index.php中为0

以下是出现错误/警告的行。

        echo "<table class='table table-hover table-responsive table-bordered'>";
    //count the number of events
    $events_count = count($obj['data']);

     for($x=0; $x<$events_count; $x++){
        //fb events will be here
     }
     echo"</table>";
     $start_date = date( 'l, F d, Y', strtotime($obj['data'][$x]['start_time']));

    // in my case, I had to subtract 9 hours to sync the time set in facebook
    $start_time = date('g:i a', strtotime($obj['data'][$x]['start_time']) - 60 * 60 * 9);

    $pic_big = isset($obj['data'][$x]['cover']['source']) ? $obj['data'][$x]['cover']['source'] : "https://graph.facebook.com/{$fb_page_id}/picture?type=large";

    $eid = $obj['data'][$x]['id'];
    $name = $obj['data'][$x]['name'];
    $description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";

    // place
    $place_name = isset($obj['data'][$x]['place']['name']) ? $obj['data'][$x]['place']['name'] : "";
    $city = isset($obj['data'][$x]['place']['location']['city']) ? $obj['data'][$x]['place']['location']['city'] : "";
    $country = isset($obj['data'][$x]['place']['location']['country']) ? $obj['data'][$x]['place']['location']['country'] : "";
    $zip = isset($obj['data'][$x]['place']['location']['zip']) ? $obj['data'][$x]['place']['location']['zip'] : "";

    $location="";

    if($place_name && $city && $country && $zip){
        $location="{$place_name}, {$city}, {$country}, {$zip}";
    }else{
        $location="Location not set or event data is too old.";
    }
    echo "<tr>";
    echo "<td rowspan='6' style='width:20em;'>";
        echo "<img src='{$pic_big}' width='200px' />";
    echo "</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td style='width:15em;'>What:</td>";
    echo "<td><b>{$name}</b></td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>When:</td>";
    echo "<td>{$start_date} at {$start_time}</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>Where:</td>";
    echo "<td>{$location}</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>Description:</td>";
    echo "<td>{$description}</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td>Facebook Link:</td>";
    echo "<td>";
    echo "<a href='https://www.facebook.com/events/{$eid}/' target='_blank'>View on Facebook</a>";
    echo "</td>";
    echo "</tr>";
?>

感谢您的帮助和快速回答

3 个答案:

答案 0 :(得分:0)

在循环中添加代码。

尝试

<?php

    echo "<table class='table table-hover table-responsive table-bordered'>";
    //count the number of events
    $events_count = count($obj['data']);

    for($x=0; $x<$events_count; $x++)
    {
        //fb events will be here

        $start_date = date( 'l, F d, Y', strtotime($obj['data'][$x]['start_time']));

        // in my case, I had to subtract 9 hours to sync the time set in facebook
        $start_time = date('g:i a', strtotime($obj['data'][$x]['start_time']) - 60 * 60 * 9);

        $pic_big = isset($obj['data'][$x]['cover']['source']) ? $obj['data'][$x]['cover']['source'] : "https://graph.facebook.com/{$fb_page_id}/picture?type=large";

        $eid = $obj['data'][$x]['id'];
        $name = $obj['data'][$x]['name'];
        $description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";

        // place
        $place_name = isset($obj['data'][$x]['place']['name']) ? $obj['data'][$x]['place']['name'] : "";
        $city = isset($obj['data'][$x]['place']['location']['city']) ? $obj['data'][$x]['place']['location']['city'] : "";
        $country = isset($obj['data'][$x]['place']['location']['country']) ? $obj['data'][$x]['place']['location']['country'] : "";
        $zip = isset($obj['data'][$x]['place']['location']['zip']) ? $obj['data'][$x]['place']['location']['zip'] : "";

        $location="";

        if($place_name && $city && $country && $zip){
            $location="{$place_name}, {$city}, {$country}, {$zip}";
        }else{
            $location="Location not set or event data is too old.";
        }
        echo "<tr>";
        echo "<td rowspan='6' style='width:20em;'>";
            echo "<img src='{$pic_big}' width='200px' />";
        echo "</td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td style='width:15em;'>What:</td>";
        echo "<td><b>{$name}</b></td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td>When:</td>";
        echo "<td>{$start_date} at {$start_time}</td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td>Where:</td>";
        echo "<td>{$location}</td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td>Description:</td>";
        echo "<td>{$description}</td>";
        echo "</tr>";

        echo "<tr>";
        echo "<td>Facebook Link:</td>";
        echo "<td>";
        echo "<a href='https://www.facebook.com/events/{$eid}/' target='_blank'>View on Facebook</a>";
        echo "</td>";
        echo "</tr>";
    }

    echo"</table>";
?>

答案 1 :(得分:-1)

您应该正确关闭for。 您无法使用$x中的for var。

答案 2 :(得分:-1)

您的for循环是:

for($x=0; $x<$events_counts; $x++) {
// fb events will be here
}
// other code below
// $x is not available here!

所以,基本上你有}并且你正在尝试访问$x范围内声明的for(除非它是全局的),但它在关闭时不存在托架。将代码放在for循环中,它应该有效。