Page 1 of 1

2 instances of same php conflicting

Posted: Fri Jan 21, 2011 10:11 am
by Mylie Boy
Hi All,

I have tried to get this code to work on my site. If I use it once it is fine, but a second instance of the same code (with variables renamed for new location) causes the hole page to fail. Each set of code works individually, but not on the same page.

Code: Select all

<?
/*
Author: Paul KH Kim
Email: paul.kim@onlinesolution.co.nz
Date: 21/02/2009
Script: Basic Weather Forecast using Google Weather API
Description: Get Google's weather XML file and parse it to HTML format
*/  

function writeln($string)
{
    echo "{$string}\r\n";
}

function convert($temp)
{
    // Converting Fahrenheit To Celsius, vice versa
    global $config;
    $temperature = $temp;

    if( strtoupper($config['base-temp-unit']) == 'F' && strtoupper($config['display-temp-unit']) == 'C' )
    {
        // Converting Fahrenheit To Celsius
        $temperature = round((5/9)*($temp-32));
    }
    if( strtoupper($config['base-temp-unit']) == 'C' && strtoupper($config['display-temp-unit']) == 'F' )
    {
        // Converting Celsius to Fahrenheit
        $temperature = round((9/5)*$temp+32);
    }
   
    return $temperature;
}

$url = "http://www.google.com";
$location = "dublin,ie"; // <city>,<country code>
$weather_url = "{$url}/ig/api?weather={$location}";

$config['base-temp-unit'] = 'F'; // F=Fahrenheit, C=Celsius
$config['display-temp-unit'] = 'C'; // F=Fahrenheit, C=Celsius

if( $xmlData = file_get_contents($weather_url) )
{
    $xml = new SimpleXMLElement($xmlData);
   
    $eol = "\r\n";
   
    // Display basic information
    writeln("  <p><b>Dublin</b>");

   
    // Display current information
    writeln("<div id='current' class='illustration left'>");
    writeln("  <h3>Today</h3>");
    writeln("  <p><img src='{$url}{$xml->weather->current_conditions->icon->attributes()}'><br/>");
    writeln("  {$xml->weather->current_conditions->condition->attributes()}<br/>");
    writeln("  Temp: {$xml->weather->current_conditions->temp_c->attributes()} C<br/>");
    writeln("  {$xml->weather->current_conditions->humidity->attributes()}<br/>");
    writeln("  {$xml->weather->current_conditions->wind_condition->attributes()}<br/>");
    writeln("   </p></div>");
    
    writeln("   <div style='clear:both'></div>");

    foreach( $xml->weather->forecast_conditions as $i => $result )
    {
        // Display forecasts (next 4 days)
        writeln("<div id='forecast_{$i}' class='weather'>");
        writeln("  <h3>{$result->day_of_week->attributes()}</h3>");
        writeln("  <p><img src='{$url}{$result->icon->attributes()}'><br/>");
        writeln("  {$result->condition->attributes()}<br/>");
        writeln("  Low: ".convert($result->low->attributes())." ".strtoupper($config['display-temp-unit'])."<br/>");
        writeln("  Hi: ".convert($result->high->attributes())." ".strtoupper($config['display-temp-unit'])."<br/>");
        writeln("  </p></div>");


    }
}
?>
second bit of code

Code: Select all

<?
/*
Author: Paul KH Kim
Email: paul.kim@onlinesolution.co.nz
Date: 21/02/2009
Script: Basic Weather Forecast using Google Weather API
Description: Get Google's weather XML file and parse it to HTML format
*/ 

function writeln($stringcork)
{
    echo "{$stringcork}\r\n";
}

function convert($tempcork)
{
    // Converting Fahrenheit To Celsius, vice versa
    global $configcork;
    $temperaturecork = $tempcork;

    if( strtoupper($configcork['base-temp-unit']) == 'F' && strtoupper($configcork['display-temp-unit']) == 'C' )
    {
        // Converting Fahrenheit To Celsius
        $temperaturecork = round((5/9)*($tempcork-32));
    }
    if( strtoupper($configcork['base-temp-unit']) == 'C' && strtoupper($configcork['display-temp-unit']) == 'F' )
    {
        // Converting Celsius to Fahrenheit
        $temperaturecork = round((9/5)*$tempcork+32);
    }
   
    return $temperaturecork;
}

$urlcork = "http://www.google.com";
$locationcork = "cork,ie"; // <city>,<country code>
$weather_urlcork = "{$urlcork}/ig/api?weather={$locationcork}";

$configcork['base-temp-unit'] = 'F'; // F=Fahrenheit, C=Celsius
$configcork['display-temp-unit'] = 'C'; // F=Fahrenheit, C=Celsius

if( $xmlDatacork = file_get_contents($weather_urlcork) )
{
    $xmlcork = new SimpleXMLElement($xmlDatacork);
   
    $eolcork = "\r\n";
   
    // Display basic information
    writeln("  <p><b>Dublin</b>");

   
    // Display current information
    writeln("<div id='current' class='illustration left'>");
    writeln("  <h3>Today</h3>");
    writeln("  <p><img src='{$urlcork}{$xmlcork->weather->current_conditions->icon->attributes()}'><br/>");
    writeln("  {$xmlcork->weather->current_conditions->condition->attributes()}<br/>");
    writeln("  Temp: {$xmlcork->weather->current_conditions->temp_c->attributes()} C<br/>");
    writeln("  {$xmlcork->weather->current_conditions->humidity->attributes()}<br/>");
    writeln("  {$xmlcork->weather->current_conditions->wind_condition->attributes()}<br/>");
    writeln("   </p></div>");
    
    writeln("   <div style='clear:both'></div>");

    foreach( $xmlcork->weather->forecast_conditions as $icork => $resultcork )
    {
        // Display forecasts (next 4 days)
        writeln("<div id='forecast_{$icork}' class='weather'>");
        writeln("  <h3>{$resultcork->day_of_week->attributes()}</h3>");
        writeln("  <p><img src='{$urlcork}{$resultcork->icon->attributes()}'><br/>");
        writeln("  {$resultcork->condition->attributes()}<br/>");
        writeln("  Low: ".convert($resultcork->low->attributes())." ".strtoupper($configcork['display-temp-unit'])."<br/>");
        writeln("  Hi: ".convert($resultcork->high->attributes())." ".strtoupper($configcork['display-temp-unit'])."<br/>");
        writeln("  </p></div>");


    }
}
?>
---Any ideas?

You help is much appreciated - I've spend a few hours on this already!

MB

Re: 2 instances of same php conflicting

Posted: Fri Jan 21, 2011 10:14 am
by John Cartwright
After a quick look, you are re-declaring your function names -- which is a fatal error. It would be helpful to actually see what errors you are getting.

Re: 2 instances of same php conflicting

Posted: Fri Jan 21, 2011 11:46 am
by Mylie Boy
Unfortunately no errors appear - the page fails completely.

This is all that appears on the served page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>
e

As I said each one works fine on its own, just not the two on the same page. How do I get around redeclaring the functions?


Many thanks for your help,
MB

Re: 2 instances of same php conflicting

Posted: Fri Jan 21, 2011 12:04 pm
by John Cartwright
You need to enable error reporting = E_ALL and display_errors = 1 in your php.ini, then your error messages will show.

Did you try my suggestion about re-defining the function names so they are not the same between the two files?

Re: 2 instances of same php conflicting

Posted: Fri Jan 21, 2011 12:22 pm
by Mylie Boy
Off to edit the ini file now for future mishaps.

I renamed the functions and everything is perfect now.

I knew it was something very obvious I missed - I had renamed everything BUT the functions!

Many thanks for your help with this, I really can't overstate how much I appreciate it.

All the best,
MB