-1

I want to get simple fb graph data and load that into a table with php, but somehow i receive 4 same error messages why so ever and I dont know what they mean.

Notice: Undefined offset: 0 in index.php on line 52

Notice: Undefined offset: 0 in index.php on line 55

Notice: Undefined offset: 0 in index.php on line 59

Notice: Undefined offset: 0 in index.php on line 60

Here are the lines where the error / warnings appear ..

        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>";
?>

Thanks for help and fast answer

TamoDaleko
  • 151
  • 1
  • 12

3 Answers3

0

Add your code inside loop.

Try

<?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>";
?>
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
-1

You should close for correctly. You can't use $x var out of for.

Kristiyan
  • 1,655
  • 14
  • 17
-1

Your for loop is:

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

So, basically you have } and you're trying to access $x which is declarated (unless it's global) in scope of for, but it doesn't exists below closing bracket. Put your code in for loop and it should works.

Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56