Ignoring empty variable fields?! (pls help)
Posted: Sun Oct 23, 2011 7:11 am
Here is the full code i use on home.php. I use the feeddata.php page to var_dump raw data and check elements. Is my if statement on feeddata.php the correct way to go about not including certain elements?
The errors arise when the data fields are empty which is true for the facebook posts that dont have a picture or picture posts that dont have content.
home.php
index.tmpl.php
here is also the full code for feeddata.php where i tried to correct the errors and test my fix.
The errors arise when the data fields are empty which is true for the facebook posts that dont have a picture or picture posts that dont have content.
home.php
Code: Select all
<div class="main">
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
// get data from Twitter
$twitter_raw = file_get_contents('http://twitter.com/statuses/user_timeline/46936176.rss');
// Turn data in xml element
$twitter_xml = new SimpleXMLElement($twitter_raw);
$twitter_data = array();
foreach($twitter_xml->channel->item as $item){
// remove the username from each post
$description = trim(str_replace('iMurKID:', '', $item->description));
$twitter_item = array(
'content' => $description,
'date' => strtotime($item->pubDate),
'type' => 'twitter'
);
array_push($twitter_data, $twitter_item);
}
// get data from Facebook
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/689440296/feed?access_token=AAABZBcwdXt04BAOuRL8gZAYbdaZAexdYIzi7kMhqihTFN4kD7OEOVZAljQBHMXLZC54B6Am9tWYomJwKYIafKZBAzUu8qagZCcZD');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$facebook_raw = curl_exec($ch);
$facebook_json = json_decode($facebook_raw);
$facebook_data = array();
foreach($facebook_json->data as $item){
$facebook_item = array(
'content' => $item->message,
'picture' => $item->picture,
'date' => strtotime($item->created_time),
'type' => 'facebook'
);
array_push($facebook_data, $facebook_item);
}
// combine Twitter and Facebook data
$combined_data = array();
$t_index = $f_index = 0;
$count = 0;
while($count < 100){
// make sure we haven't exhausted the data in both arrays
if(!isset($twitter_data[$t_index]) && !isset($facebook_data[$f_index])){
break;
}
if(isset($twitter_data[$t_index]) && !isset($facebook_data[$f_index])){
// add Twitter
array_push($combined_data, $twitter_data[$t_index]);
$t_index++;
}
elseif(!isset($twitter_data[$t_index]) && isset($facebook_data[$f_index])){
// add Facebook
array_push($combined_data, $facebook_data[$f_index]);
$f_index++;
}
else{
// compare dates and add the more recent
if($twitter_data[$t_index]['date'] > $facebook_data[$f_index]['date']){
// add Twitter
array_push($combined_data, $twitter_data[$t_index]);
$t_index++;
}
else{
// add Facebook
array_push($combined_data, $facebook_data[$f_index]);
$f_index++;
}
}
$count++;
}
include('index.tmpl.php');
?>
</div>
<div class="footer">
<?php include_once "footer_template.php"; ?>
</div>
</div>
</body>
</html>
Code: Select all
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="css/style.css" rel="stylesheet" media="screen" />
<title>Social Media Feed</title>
</head>
<body>
<div id="container">
<ul id="feed">
<?php foreach($combined_data as $item): ?>
<li class="<?php echo $item['type'] ?>">
<p><?php echo $item['content']; ?> <br /><br />
<img border="0" src="<?php echo $item['picture']; ?>" alt="" width="50%" height="50%" />
<p class="date"><abbr class="timeago" title="<?php echo date('c', $item['date']); ?>"><?php echo date('r', $item['date']); ?></abbr></p><br />
<b>Add to Timeline</b> <br />
<select name="mydropdown">
<option value="Milk">Copenhagen</option>
<option value="Cheese">Sirocco</option>
<option value="Bread">Stockholm</option>
<input type="submit" value="Save"></input>
</select>
</li>
<?php endforeach; ?>
</ul>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script src="js/jquery.timeago.js"></script>
<script>
$(document).ready(function(){
$('abbr.timeago').timeago();
});
</script>
</body>
</html>
Code: Select all
<?php
// Start_session, check if user is logged in or not, and connect to the database all in one included file
include_once("scripts/checkuserlog.php");
// Include the class files for auto making links out of full URLs and for Time Ago date formatting
include_once("wi_class_files/autoMakeLinks.php");
include_once ("wi_class_files/agoTimeFormat.php");
// Create the two new objects before we can use them below in this script
$activeLinkObject = new autoActiveLink;
$myObject = new convertToAgo;
?>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
// get data from Twitter
$twitter_raw = file_get_contents('http://twitter.com/statuses/user_timeline/46936176.rss');
// Turn data in xml element
$twitter_xml = new SimpleXMLElement($twitter_raw);
var_dump($twitter_xml);
$twitter_data = array();
foreach($twitter_xml->channel->item as $item){
// remove the username from each post
$description = trim(str_replace('iMurKID:', '', $item->description));
$twitter_item = array(
'content' => $description,
'date' => strtotime($item->pubDate),
'type' => 'twitter'
);
array_push($twitter_data, $twitter_item);
}
// get data from Facebook
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/689440296/feed?access_token=AAABZBcwdXt04BAOuRL8gZAYbdaZAexdYIzi7kMhqihTFN4kD7OEOVZAljQBHMXLZC54B6Am9tWYomJwKYIafKZBAzUu8qagZCcZD');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$facebook_raw = curl_exec($ch);
$facebook_json = json_decode($facebook_raw);
var_dump($facebook_json);
$facebook_data = array();
foreach($facebook_json->data as $item){
$facebook_item = array(
if (!isset('content' => $item->message)){
'picture' => $item->picture,
'date' => strtotime($item->created_time),
'type' => 'facebook'
}
elseif (!isset('picture' => $item->picture)) {
'content' => $item->message,
'date' => strtotime($item->created_time),
'type' => 'facebook'
}
else {
'content' => $item->message,
'picture' => $item->picture,
'date' => strtotime($item->created_time),
'type' => 'facebook'
};
array_push($facebook_data, $facebook_item);
}
?>