Click to See Complete Forum and Search --> : PHP echos


AaronChiles
04-22-2006, 07:04 PM
I am a newbie to this stuff and this is what I want to accomplish. My XML feed outputs a <skies> thing that displays in text what the skies are like (i.e. mostly cloudy). I want to change that text it outputs from

echo $w->getCurrent('skies');

and I want to add something like

<img src="images/weather_icons/

to the beginning and something like

.png" alt="weather" height="24" width="24">

to the end. So that once the PHP is rendere don the server it will come to the browser looking like

<img src="images/weather_icons/mostly cloud.png" alt="weather: height"24" width="24">

What is the proper syntax to append such code to the "skies" variable that i am getting? I have tried a lot and nothing has worked so far.

Thanks,
Aaron

Sheldon
04-22-2006, 07:11 PM
<?php

$w->getCurrent('skies');

echo("<img src=\"images/weather_icons/".$w.".png\" alt=\"weather:".$w."\" height\"24\" width=\"24\">");

delr2691
04-22-2006, 07:11 PM
echo '<img src="images/weather_icons/';
echo $w->getCurrent('skies');
echo '.png" alt="weather" height="24" width="24">';

echo '<img src="images/weather_icons/'.$w->getCurrent('skies').'.png" alt="weather" height="24" width="24">';

AaronChiles
04-22-2006, 08:01 PM
Okay, thanks for those posts they helped me out a bunch, but looking at the xml parser I found a switch function, but I am still getting T_Parse errors; here is my code:

function weatherString($code) {

switch($code) {
case "TS":
return "<img src="images/weather_icons/ts.png" alt="Thunderstorms" height="24" width="24">;
break;
case "RA":
return "<img src="images/weather_icons/ra.png" alt="Rain" height="24" width="24">";
break;
case "MC":
return "<img src="images/weather_icons/mc.png" alt="Mostly Cloudy" height="24" width="24">";
break;
case "SU":
return "<img src="images/weather_icons/su.png" alt="Sunny" height="24" width="24">";
break;
case "MO":
return "<img src="images/weather_icons/mo.png" alt="Mostly Clear" height="24" width="24">";
break;
case "PC":
return "<img src="images/weather_icons/pc.png" alt="Partly Cloudy" height="24" width="24">";
break;
case "SN":
return "<img src="images/weather_icons/ts.png" alt="Snow" height="24" width="24">";
break;
case "CL":
return "<img src="images/weather_icons/cl.png" alt="Clear" height="24" width="24">";
break;
case "FG":
return "<img src="images/weather_icons/fg.png" alt="Fog" height="24" width="24">";
break;
}

return null;

delr2691
04-22-2006, 08:12 PM
you are not scaping the quote after every .png and those in the alt, height and width proprties.. you can change every return "etccc" to return 'etc'.. it's better to use single quotes when you are returning html data with double quotes so they are no problem.. You can even notice this by seeing how your code is highlited...

AaronChiles
04-22-2006, 08:18 PM
Thanks, I think I will just go back to the old way I did it.

delr2691
04-22-2006, 08:22 PM
ok.. :D but still, it was just the way you made the return statement.. in case you change your mind, you should do something like this:


return '<img src="images/weather_icons/ts.png" alt="Thunderstorms" height="24" width="24">';

Sheldon
04-22-2006, 11:09 PM
an easier less code way would be like this
seeings as all of your image names are the same as your weather names


<?php

$code = ""; //what ever or how ever you poplutate it

$code = strtolower($code);
echo("<img src=\"images/weather_icons/". $code .".png\" alt=\"". $code ."\" height=\"24\" width=\"24\">");

?>

delr2691
04-22-2006, 11:20 PM
yeah.. that was how he was originally going to do it.. and it's the wisest way.. :D but try to make an array with all the posible codes and then compare the entry with the array so the script does not try to load images that doesnt exist if an unvalid code is entered..

Sheldon
04-22-2006, 11:31 PM
or he can use file_exsists with it


function weatherimage($code)
{

$code = strtolower($code);
if(file_exists("images/" . $code . ".png"))
{
$code = $code;
}
else
{
$code = "default";
}

return $code;
}

$code = ""; //again how ever you populate it

$code = weatherimage($code);
echo("<img src=\"images/weather_icons/". $code .".png\" alt=\"". $code ."\" height=\"24\" width=\"24\">");

delr2691
04-22-2006, 11:37 PM
yeah that'd be good.. but i think AaronChiles is not posting anymore hehe

AaronChiles
04-22-2006, 11:40 PM
Ha, I'm still here just trying to use all the great advice. I am actually trying to get a weather.com feed to work:

http://www.webdeveloper.com/forum/showthread.php?p=555317#post555317

over there, no sucess yet though, but thanks for all the help on this thread.

Sheldon
04-22-2006, 11:41 PM
Ah good ideas for other people though.


BTW is it working aaron? The last method will work in a number of different situations.

AaronChiles
04-22-2006, 11:44 PM
Honestly, I think I am still using this to change them:

<?php
require_once("pxweather.class.php");
$w = new Weather($city = "KCLT", $force = true);
$w->load();

echo '<img src="images/weather_icons/'.$w->getCurrent('weather_code').'.png" alt="weather" height="24" width="24"> ';

echo $w->getCurrent('temp.F');
?>

delr2691
04-22-2006, 11:50 PM
ohh.. you are the same guy that posted about dynamic includes, arent you? cuz i think i already visit your site to see how was it going :) hmm.. but sorry i dont know so much about parsing xml data but i have done it a couple of times with javascript.. and resulted pretty easy.. :D hope that's useful..

AaronChiles
04-22-2006, 11:54 PM
Yeah, same guy with the dynamic includes, which are working very well, just a little complicated when trying to do a search engine. I'll look into the javascript, thanks.

delr2691
04-23-2006, 12:15 AM
no problem... hey you can use ajax to load the xml data and then refer to any of its nodes as a DOM object.. ie: xmldata.getElementsByTagName('weatherDescription')[0].getAttribute('icon')
and then you can set a timeout so it updates automatically without having to reload the page.. :D