facebook graph api video pulling - php

I'm trying to embed a facebook video gallery on a website.
My problem is that it only shows one video, while the facebook graph shows multiple. How can I make all the videos appear?
<?php
$json_link = "https://graph.facebook.com/sophia.deboer/videos?access_token=862683673820828|15Gx44NW43LHI92o__bRPA6lz44&fields=id,name,description,created_time,from,source&limit=10";
$json = file_get_contents($json_link);
$obj = json_decode($json, true);
$feed_item_count = count($obj['data']);
for ($x = 0; $x < 10; $x++) {
echo "<div class='item_box'>";
echo "<div style='overflow:hidden;'>";
// video source
$source = $obj['data'][$x]['source'];
echo "<div class='col-lg-6'>";
echo "<video src='{$source}' controls>";
echo "Your browser does not support the video tag.";
echo "</video>";
echo "</div>"; // end 'row'
echo "<div class='col-lg-6'>";
// user's custom message
$name = isset($obj['data'][$x]['name']) ? htmlspecialchars_decode($obj['data'][$x]['name']) : "Video #" . $obj['data'][$x]['id'];
$description = htmlspecialchars_decode(str_replace("\n", "<br>", $obj['data'][$x]['description']));
// when it was posted
$created_time = $obj['data'][$x]['created_time'];
$converted_date_time = date('Y-m-d H:i:s', strtotime($created_time));
$ago_value = time_elapsed_string($converted_date_time);
// from
$page_id = $obj['data'][$x]['from']['id'];
$page_name = $obj['data'][$x]['from']['name'];
echo "<h2 style='margin: 0 0 .5em 0;'>{$name}</h2>";
echo "<div>";
echo $description;
echo "</div>";
echo "<div style='margin:.5em 0 0 0; color: #999;'>";
echo "Posted {$ago_value} by <a href='https://facebook.com/{$page_id}' target='_blank'>{$page_name}</a>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<hr />";
echo "</div>"; // end 'item_box'
}
?>
Demo

After removing :
$ago_value = time_elapsed_string($converted_date_time);
The code loops all videos.
I also changed this:
$feed_item_count = count($obj['data']);
for ($x = 0; $x < 10; $x++)
To This:
$feed_item_count = count($obj['data']);
for ($x = 0; $x < $feed_item_count; $x++)
<?php
$json_link = "https://graph.facebook.com/sophia.deboer/videos?access_token=862683673820828|15Gx44NW43LHI92o__bRPA6lz44&fields=id,name,description,created_time,from,source&limit=10";
$json = file_get_contents($json_link);
$obj = json_decode($json, true);
$feed_item_count = count($obj['data']);
for ($x = 0; $x < $feed_item_count; $x++) {
echo "<div class='item_box'>";
echo "<div style='overflow:hidden;'>";
// video source
$source = $obj['data'][$x]['source'];
echo "<div class='col-lg-6'>";
echo "<video src='{$source}' controls>";
echo "Your browser does not support the video tag.";
echo "</video>";
echo "</div>"; // end 'row'
echo "<div class='col-lg-6'>";
// user's custom message
$name = isset($obj['data'][$x]['name']) ? htmlspecialchars_decode($obj['data'][$x]['name']) : "Video #" . $obj['data'][$x]['id'];
$description = htmlspecialchars_decode(str_replace("\n", "<br>", $obj['data'][$x]['description']));
// when it was posted
$created_time = $obj['data'][$x]['created_time'];
$converted_date_time = date('Y-m-d H:i:s', strtotime($created_time));
//$ago_value = time_elapsed_string($converted_date_time);
// from
$page_id = $obj['data'][$x]['from']['id'];
$page_name = $obj['data'][$x]['from']['name'];
echo "<h2 style='margin: 0 0 .5em 0;'>{$name}</h2>";
echo "<div>";
echo $description;
echo "</div>";
echo "<div style='margin:.5em 0 0 0; color: #999;'>";
echo "Posted {$ago_value} by <a href='https://facebook.com/{$page_id}' target='_blank'>{$page_name}</a>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<hr />";
echo "</div>"; // end 'item_box'
}
?>

Related

Count row and repeat the same rows within Loop

I'm working on mansory gallery here images fetching into the row first row contains three images and then for the second row contains two images and so on.
Now currently all images coming inside same row. I want to add in a loop I had tried but unable to achieve the result. For inspiration link
<?php
include('admin/config.php');
$result = mysqli_query($db, "SELECT * FROM gallery order by id desc");
// var_dump($result->num_rows);
while ($row = mysqli_fetch_array($result)) {
echo "<div class='gallery-items'>";
echo "<div class='mansory-item'>";
echo "<a href='admin/images/".$row['path']."' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/".$row['path']."' class='item-img img-1'>";
echo "</a>";
echo "</div>";
echo "</div>";
}
?>
Current output
Expected output
Can anyone suggest me how should i get this output.
Check below snippet,
$inc = 4;
$i = 1;
while ($row = mysqli_fetch_array($result)) {
if (empty($temp) || $inc != $temp) {
$temp = $inc;
if ($inc == 4) {
echo "<div class='gallery-grid'>";
}
echo "<div class='gallery-items'>";
}
if ($i <= $inc) {
// echo $i . '<>';
echo "<div class='mansory-item'>";
echo "<a href='admin/images/" . $row['username'] . "' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/" . $row['username'] . "' class='item-img img-1'>";
echo "</a>";
echo "</div>";
$i++;
}
if ($i == $inc) {
echo "</div>";
if ($i == 3) {
echo "</div>";
}
$i = 1;
$inc = ($inc == 4 ? 3 : 4);
}
}

Magento, getProductUrl() returning same url

I'm working on showing x amount of related products on the shopping cart page.
Here is my current code
$cart = Mage::getModel('checkout/cart')->getQuote();
$c = count($cart);
if($c != 0) {
$c = $c - 1;
$rand = rand(0, $c);
$i = 0;
foreach ($cart->getAllItems() as $item) {
if($i == $c) {
$productId = $item->getProduct()->getId();
$model = Mage::getModel('catalog/product');
$product = $model->load($productId);
$allRelatedProductIds = $product->getRelatedProductIds();
$rc = count($allRelatedProductIds) - 1;
$rand = rand(0, $rc);
$relatedProduct = $model->load($allRelatedProductIds[$rand]);
echo "<div class='page-title'><h1>Maybe you would like to try one of these</h1></div>";
foreach($allRelatedProductIds as $prod) {
$p = $model->load($prod);
echo "<a href='".$p->getProductUrl()."'>";
echo "<div style='float:left; font-size:1.3em; width:33%; line-height:1.25; font-family:\"karlaregular\"'>";
echo "<img src='".Mage::helper('catalog/image')->init($p, 'small_image')->resize(300,300) ."' style='width:100%; height:100%;'>";
echo "<h2 class='product-name' style='text-align:center;'>".$p->getName()."</h2>";
echo "<div class='pricebox' style='text-align:center;'><span class='regular-price'><span class='price'>£".number_format($p->getPrice(), 2)."</span></span></div>";
echo "</div>";
echo "</a>";
}
}
$i++;
}
}
This almost works 100% problem is that the $p->getProductUrl() is only being populated by the first product in the loop and gives the wrong url to all sequential products.
I cannot see any issues with the above and would someone to take a look.
Thanks
Please try below code
$cart = Mage::getModel('checkout/cart')->getQuote();
$c = count($cart);
if($c != 0) {
$c = $c - 1;
$rand = rand(0, $c);
$i = 0;
foreach ($cart->getAllItems() as $item) {
if($i == $c) {
$productId = $item->getProduct()->getId();
$model = Mage::getModel('catalog/product');
$product = $model->load($productId);
$allRelatedProductIds = $product->getRelatedProductIds();
$rc = count($allRelatedProductIds) - 1;
$rand = rand(0, $rc);
$relatedProduct = $model->load($allRelatedProductIds[$rand]);
echo "<div class='page-title'><h1>Maybe you would like to try one of these</h1></div>";
foreach($allRelatedProductIds as $prod) {
$p = Mage::getModel("catalog/product")->load($prod);
echo "<a href='".$p->getProductUrl()."'>";
echo "<div style='float:left; font-size:1.3em; width:33%; line-height:1.25; font-family:\"karlaregular\"'>";
echo "<img src='".Mage::helper('catalog/image')->init($p, 'small_image')->resize(300,300) ."' style='width:100%; height:100%;'>";
echo "<h2 class='product-name' style='text-align:center;'>".$p->getName()."</h2>";
echo "<div class='pricebox' style='text-align:center;'><span class='regular-price'><span class='price'>£".number_format($p->getPrice(), 2)."</span></span></div>";
echo "</div>";
echo "</a>";
}
}
$i++;
}
}

Facebook php script doesn't work online

I have a FB script and it works fine locally but it doens't online.
The first script is een script for the events and the second one is to get de Facebook albums.
<?php
$fb_page_id = "408403535882715";
$access_token = "Acces_token";
$year_range = 10;
$since_date = date('Y-01-01', strtotime('-' . $year_range . ' years'));
$until_date = date('Y-01-01', strtotime('+' . $year_range . ' years'));
// unix timestamp years
$since_unix_timestamp = strtotime(date("Y-m-d"));
$until_unix_timestamp = strtotime($until_date);
$fields="id,name,description,location,venue,timezone,start_time,cover";
$json_link = "https://graph.facebook.com/{$fb_page_id}/events/feed/?fields={$fields}&access_token={$access_token}&since={$since_unix_timestamp}&until={$until_unix_timestamp}";
$json = file_get_contents($json_link);
$obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
// count the number of events
$event_count = count($obj['data']);
for($x=0; $x<$event_count; $x++){
if($x<1) {
$start_date = date( 'l, F d, Y', strtotime($obj['data'][$x]['start_time']));
$start_time = date('H:i', strtotime($obj['data'][$x]['start_time']) - 60 * 60 * 23);
$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'];
$location = isset($obj['data'][$x]['location']) ? $obj['data'][$x]['location'] : "";
$description = isset($obj['data'][$x]['description']) ? $obj['data'][$x]['description'] : "";
echo "<div class='eventFrontpage'";
echo "<div class='info'>";
echo "<h1>{$name}</h1>";
echo "<span class='datum'>{$start_date} om {$start_time} uur</span></br>";
echo "<span class='locatie'>Locatie: {$location}</span></br>";
echo "<span class='content'>Info: {$description}</span></br>";
echo "</div>";
echo "</div>";
break;
}
}
?>
and this one (on different pages)
<?php
$fb_page_id = "408403535882715";
$json_link = "http://graph.facebook.com/{$fb_page_id}/albums?fields=id,name,description,link,cover_photo,count";
$json = file_get_contents($json_link);
$obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
$album_count = count($obj['data']);
for($x=0; $x<$album_count; $x++){
$id = $obj['data'][$x]['id'];
$name = $obj['data'][$x]['name'];
$description = $obj['data'][$x]['description'];
$link = $obj['data'][$x]['link'];
$cover_photo = $obj['data'][$x]['cover_photo'];
$count = $obj['data'][$x]['count'];
// if you want to exclude an album, just add the name on the if statement
if(
$name!="Profile Pictures" &&
$name!="Cover Photos" &&
$name!="Timeline Photos" &&
$name!="Mobile Uploads"
){
$show_pictures_link = "photos.php?album_id={$id}&album_name={$name}";
echo "<a class='mediaAlbum {$name}' href='{$link}' target='_blank'>";
echo "<img class='img-responsive' src='http://graph.facebook.com/{$cover_photo}/picture' alt=''>";
echo "<h2>{$name}</h2>";
echo "</a>";
}
}
?>
Someone an idea?
Answer: Because it was PHP < 5.4 on my server de code needed a translation :
$obj = json_decode(preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $json), true);

Netbeans says PHP function has too many lines

I have a function and netbeans is saying I can only have 20 lines in my code.
Why is this and how could I fix this. I have another function with the same problem. Dreamweaver doesn't say anything so I don't know if this is a big problem.
my code:
function dispalyEvent($weekNr, $week, $year){
echo "<p>";
$gendate = new DateTime();
$gendate->setISODate($year,$week,$weekNr);
$month = $gendate->format('m');
$day = $gendate->format('d');
$event_query = mysql_query("SELECT * FROM calendar ORDER BY starttime");
while($event = mysql_fetch_array($event_query)) {
$startYear = $event['startyear'];
$startMonth = $event['startmonth'];
$startDay = $event['startdate'];
$endYear = $event['endyear'];
$endMonth = $event['endmonth'];
$endDay = $event['enddate'];
$period = new DatePeriod(
new DateTime($startYear.$startMonth.$startDay),
new DateInterval('P1D'),
new DateTime($endYear.$endMonth.$endDay +1)
);
$currentDate = $year."-".$month."-".$day;
foreach ($period as $savedDate) {
if ($currentDate == $savedDate->format('Y-m-d')){
if ($event['Approved'] == "Approved"){
echo "</p>";
echo "<p>";
if ($event['ad']) {
echo "<img src='images/".$event['ad']."' alt='event-ad' width='300' height='100' />";
} else { echo " "; }
echo "</p>";
echo "<p> </p>";
echo "<div class='toggleLink' style='cursor: pointer; color: #333;'>";
echo $event['starttime']." ".$event['title'];
echo "</p>";
echo "</div>";
echo " <div class='toggle'>";
echo "<p class='toggleLink'>";
echo "(".$event['starttime']."-".$event['endtime'].") ".$event['location']." - ".$event['address']." - Admission Price: $".$event['price']."<br>".$event['description'];
echo "</div>";
}}}}
echo "</p>";
}
?>

Split table in php

i got this script. it looks into a file and read all the lines and put it nicely in a table.. what i need to do now is to take all the table data and split it into two tables ..
eg. if their is 100 rows.. then instead of one long list, i will get 50 data in one table and the other 50 in the other table...
enter code here<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo "<table border=\"1\">";
for ($count = 1; $count < sizeof($lines); $count++)
{
$fields = explode(",",$lines[$count]);
$sz = sizeof($fields);
if ($sz > 1)
{
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
?>
Use a foreach instead of a for and $count to calculate if it is needed to echo a table tag. $count is only incremented when a row was inserted.
<style>
div.container {
width: 100%; /* Change the width to the desired width */
padding: 0;
}
div.container table {
width: 50%; /* Change the width to half of the width of the div */
margin: 0;
float: left;
}
</style>
<?php
if(isset ($_GET['type']))
{
$otype = $_GET['type'];
}
else
{
$otype = 'm';
}
$statusFile = "d:\\CLIENTS\\status.txt";
$file_handler = fopen($statusFile, "r");
// read the contents
$contents = fread($file_handler, filesize($statusFile));
// close the file
fclose($file_handler);
if (strcasecmp($otype, "m") == 0)
{
echo $contents;
}
else
{
$lines = explode("\n",$contents);
$frow = explode(",", $lines[0]);
if (strcmp($frow[1],"1") == 0)
{
echo "Update Status: <b>Complete</b>";
//to count total lines in txt file
$statusFile = "d:\\CLIENTS\\status.txt";
$line = count(file($statusFile));
echo "There are".$line."lines in";
}
else
{
echo "Update Status: <b>Incomplete</b>";
}
echo '<div class="conatiner">';
$count = 0;
foreach ($lines as $currLine)
{
$fields = explode(",",$currLine);
$sz = sizeof($fields);
if ($sz > 1)
{
if ($count == 0) {
echo "<table border=\"1\">";
}
elseif ($count % 50 == 0) {
echo "</table>";
echo "<table border=\"1\">";
}
$str = "OK";
echo "<tr>";
echo "<td>" . $fields[0] . "</td>";
echo "<td>" . $fields[1] . "</td>";
//echo "<td>" . $fields[2] . "</td>";
if (strpos($fields[2],'OK') !== false)
{
echo "<td><font color='green'>". $fields[2] ."</font></td>";
//echo "<td style='background-color: #00FF00;'>". $fields[2] ."</td>";
}
else
{
//echo "<td><font color='red'>". $fields[2] ."</font></td>";
echo "<td style='background-color: #FF0000;'>". $fields[2] ."</td>";
}
echo "</tr>";
$count++;
}
}
echo "</table>";
echo '</div>';
}

Categories