Help with PHP Error

Hello,

I am running into an error on Alaska Agents | AgentsWhotTweet - Find real estate agents on Twitter!

I am not sure what the error means.

<?php

      /* Load required lib files. */
      session_start();
      require_once('twitteroauth/twitteroauth.php'); /* This is the library for connecting with oAuth */
      require_once('configw.php'); /* This is the file that contains the oAuth credentials - this will be different for each app */

      /* Create a TwitterOauth object with consumer/user tokens. */
      $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);

      /*
	    The code below gets the lists of AthTweet, parses through them to get id's
	    and creates conditions around each list to be passed in. If you add a list,
	    just name it something you can remember and stay away form odd characters.
	    I dont't see this as being an issue, but just want to let you know.
	    
	    NOTE: THEY HAVE to be indentical in Spelling and ARE CASE SENSITIVE.
	    i.e. NFL must be 'NFL' not 'nfl' BECAUSE you named it NFL on Twitter.
	    If you name is volleyball on Twitter, it will be volleyball here.
        
      
	    This $siteId value below should represent the appropriate list you want to display
	    For the main page, you want to set this value to  $siteID = '';

	    So, assigning a 2 below here would give me the NCAA Football feed, If I assigned
	    $siteID = '' I would get the homepage stream.
	    
      */
      
      $siteId = 'Alaska'; /* This would display the NFL Feed */

      if($siteId != '') {
	    
	    $listsUri = 'AgentsWT_West/lists';
	    $listsXml = $connection->get($listsUri);
	    $listsXml = new SimpleXMLElement($listsXml);
	    
	    $listCount = 0;
	    $listObj = array();
	    
	    foreach ($listsXml->lists->list as $item) {
		  
		  $listName = $item[$listCount]->name;
		  $listId = $item[$listCount]->id;
		  $listObj["$listName"] = $listId;
		  
		  $listCount++;
	    }

	    foreach ($listsXml->lists->list as $item) {
		  
	        $listId = $item->id;
		$listName = $item->name;

	        if("$listName" === "$siteId") {
		  $listId = $listObj["$siteId"][0];
	    	  $listIdFin = $listId;
	        }
	    }
      
	    $listsStatusesUri = "AgentsWT_West/lists/$listIdFin/statuses";
	    $xml = $connection->get($listsStatusesUri);
	    
      }else{
	    
	    $siteStatusesUri = 'statuses/friends_timeline';
	    $xml = $connection->get($siteStatusesUri);

      }
   
      $xml = new SimpleXMLElement($xml);

      /* This is a string variable we are going to assemble the Html on */
      $streamHtml = '';
      
      if(!empty($xml->status)) {
	    foreach ($xml->status as $entry)
	    {
	    
		  // there are more elements to choose from see: http://apiwiki.twitter.com/REST+API+Documentation#Statuselement
		  $status 		= $entry->text;
		  $profilePic 	= $entry->user->profile_image_url;
		  $profileName 	= $entry->user->screen_name;
		  $statusRealName 	= $entry->user->name;
		  $profileURL 	= $entry->user->url;
		  $statusDate 	= $entry->created_at;
		  $statusSource 	= $entry->source;
		  $statusDateFormatted = twitterTime(strtotime($statusDate));
		  $profileBio 	= $entry->user->description;
		  $statusFollowCount 	= $entry->user->followers_count;

		  $streamHtml .= "<div class=\\"post hentry\\">\
";
		  $streamHtml .= "<div class=\\"image-box\\">\
";
		  $streamHtml .= "<img class=\\"photo\\">\
";
		  $streamHtml .= "<a href=\\"http://www.twitter.com/$profileName\\" target=\\"_blank\\"><img class=\\"userPicThumb\\" src=\\"$profilePic\\" alt=\\"$profileRealName\\" /></a>\
";
		  $streamHtml .= "</div>\
";
		  $streamHtml .= "<div class=\\"text-box\\">\
";
		  $streamHtml .= "<div class=\\"meta\\">\
";
		  $streamHtml .= "<strong class=\\"post-link\\">\
";
		  $streamHtml .= "<a class=\\"entry-title\\" href=\\"http://www.twitter.com/$profileName\\" target=\\"_blank\\">$profileName</a>\
";
		  $streamHtml .= "</strong>\
";		  
		  $streamHtml .= "<span class=\\"vcard\\"><span class=\\"fn\\"> $statusRealName</span></span>\
";
		  $streamHtml .= "</div>\
";
		  $streamHtml .= "<p class=\\"entry-content\\">$status</p>\
";
		  $streamHtml .= "<div class=\\"time\\">\
";		  
		  $streamHtml .= "<span>$statusDateFormatted from $statusSource</span>\
";
		  $streamHtml .= "</div>\
";
		  $streamHtml .= "</div>\
";
		  $streamHtml .= "<div class=\\"clear\\"></div>\
";
		  $streamHtml .= "</span>\
";
		  $streamHtml .= "</div>\
";

	    }
      }
      
      // this echo below is used for testing only. See 'Note:' below.
      // echo $streamHtml;
      


     //create a time display like '1 hour ago'
     function twitterTime($time) {
           $delta = time() - $time;
           if ($delta < 60) {
              return 'less than a minute ago.';
           } else if ($delta < 120) {
             return 'about a minute ago.';
           } else if ($delta < (45 * 60)) {
             return floor($delta / 60) . ' minutes ago.';
           } else if ($delta < (90 * 60)) {
             return 'about an hour ago.';
           } else if ($delta < (24 * 60 * 60)) {
             return 'about ' . floor($delta / 3600) . ' hours ago.';
           } else if ($delta < (48 * 60 * 60)) {
             return '1 day ago.';
           } else {
             return floor($delta / 86400) . ' days ago.';
           }
     }
     
     //this is an empty string container we're going to pass the result of our loop into
     $twitterString = "";
     
?>

These are the errors I am getting:

Warning: main() [function.main]: Cannot add element list number 1 when only 0 such elements exist in … on line 54

Warning: main() [function.main]: Cannot add element list number 2 when only 0 such elements exist in … on line 53

Here are lines 53/54/55

		  $listName = $item[$listCount]->name;
		  $listId = $item[$listCount]->id;
		  $listObj["$listName"] = $listId;

I would greatly appreciate it if someone could help me! Again, it looks like everything is working properly (the script is doing what i want it to do) but the errors are screwing up how the page looks.

Thanks,

Jon

That array, $Item is either uninitialised or empty and as such you can’t assign items to it.

How would I go about fixing the code?

try


          $listObj[$listName] = $listId;  // ie without quotes

Thanks cups, but I am getting an additional error when I do that, and the other error does not go away…

Then it is time to start debugging and looking into what those variables actually contain, if anything.

use:
var_dump(), var_export() or just plain echo on each of the variables during or after the loop that contains those errors - and chase them back and see where they are coming from.

compare them carefully to what a dump of what your simpleXml object contains.

Also I just spotted this code


            if("$listName" === "$siteId") { 
          $listId = $listObj["$siteId"][0]; 
              $listIdFin = $listId; 

… and again variables are being needlessly quoted, SB


            if($listName === $siteId) { 
          $listId = $listObj[$siteId][0]; 
              $listIdFin = $listId;