Ignoring empty variable fields?! (pls help)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imurkid
Forum Newbie
Posts: 1
Joined: Sun Oct 23, 2011 6:55 am

Ignoring empty variable fields?! (pls help)

Post by imurkid »

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

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>
index.tmpl.php

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>

here is also the full code for feeddata.php where i tried to correct the errors and test my fix.

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);
  }
?>
Last edited by imurkid on Mon Oct 24, 2011 8:07 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Undefined Property (Facebook)

Post by social_experiment »

imurkid wrote:Parse error: syntax error, unexpected T_IF, expecting ')' in /home/content/73/8413673/html/feeddata.php on line 74
Yip it's a syntax error but the code you pasted is only 38 lines, is this the code for feeddata.php?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined Property (Facebook)

Post by Celauran »

Code: Select all

$facebook_item = array(
There's the first problem.
Post Reply