2 Replies - 522 Views - Last Post: 14 February 2012 - 12:59 PM Rate Topic: -----

#1 java_newbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-November 11

PHP function not being displayed when called

Posted 14 February 2012 - 12:23 PM

Currently I'm pulling in a twitter feed from a XML file using a PHP function and displaying it onto a webpage.

<?php
function get_twitter(){
$feed = simplexml_load_file('http://search.twitter.com/search.atom?geocode=51.506325%2C-0.127144%2C10.0mi%23london%22&lang=en&rpp=5');
if ($feed) {
    foreach ($feed->entry as $item) {
        $date = date("D, dS M Y g:i:sA T", strtotime($item->published));
        echo '<a href="' . $item->link->attributes()->href . '">' . $item->title . '</a>'.'<br />' . $date . '<br />';
    }
}
else
    echo "Cannot find Twitter feed!";
}
?>


But when I have tried to call this function from another PHP script it doesn't display the results on the page.

switch ($currentPage) {
case 'twitterFeed.php':
                echo get_twitter();
 break;
            default:
                echo 'Sorry.  This content is currently unavailable';
                break;
        }




I have pasted the code into the section where it is being called and the results are displayed correctly, its just when I try and call the function that nothing is displayed on the page.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP function not being displayed when called

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP function not being displayed when called

Posted 14 February 2012 - 12:59 PM

Hi,

Are you certain your calling code has access to the function? You may have to include the file where the function is declared.

Are you seeing any errors? What are you seeing on your screen?
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1201
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: PHP function not being displayed when called

Posted 14 February 2012 - 12:59 PM

Well, assuming that your first code block works (it sounds as though it does), I am just going to take a look at the second one. And I do notice a few things with it. First, you are trying to echo the result of a function that doesn't return anything. This will cause an error.

switch ($currentPage) {
case 'twitterFeed.php':
                echo get_twitter();
 break;
            default:
                echo 'Sorry.  This content is currently unavailable';
                break;
        }




Should be something like:
switch ($currentPage) {
case 'twitterFeed.php':
                get_twitter();
 break;
            default:
                echo 'Sorry.  This content is currently unavailable';
                break;
        }




Second, are you sure that the function exists in the second code? You can check and know for sure by using:
if(!function_exists("get_twitter")){
  echo "The function 'get_twitter' doesn't exist...";
}


If it says that the function doesn't exist, then you want to make sure that the file containing the function is included (or required). If you believe you have included it, but it is still saying that the function doesn't exist, try checking the path to make sure you are actually including what you think you are. If it is required already it not being included should cause the page load to fail.

Third, you may want to check that the $currentPage is actually set to twitterFeed.php. It is possible that it isn't set as you are expecting, or that there are some extra characters or some other random mistake in the variable causing the case to never be called. However, I doubt that this is it as you said the code worked when you put them in the same file.

Fourth, check the error logs. If you are on a linux machine they will be located where all the rest of your logs are (example: /var/log/apache2/errors.log for Ubuntu). It should have all the problems your code is running into along with file (path) and line numbers that the error was encountered on.

Hope that helps.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1