Forum Moderators: coopster

Message Too Old, No Replies

php & xml edit

php & xml edit

         

Why_not

11:05 pm on Oct 2, 2008 (gmt 0)

10+ Year Member



hi ! i have any class for show weather yahoo . this code worked nice . NOW : how to add drop down list city for choice and show weather !? his code show by defualt ( or change manual ) but my questions is ! how to add drop down city list ?
Example : "AUXX0016"
"AUXX0025"
"AUXX0075"
"AUXX0079"
"AUXX0052"
"AUXX0062"
"AUXX0023"
click each city ( "AUXX0016" ) send data & show weather ! please help thanks regards

[edited by: eelixduppy at 11:08 pm (utc) on Oct. 2, 2008]
[edit reason] removed URL [/edit]

eelixduppy

11:08 pm on Oct 2, 2008 (gmt 0)



Please post the relevant code instead of linking to it. Thanks.

Why_not

6:45 am on Oct 3, 2008 (gmt 0)

10+ Year Member



classweather.php
<?
class weather
{
var $locationcode;
var $allurl;
var $parser;
var $unit;
var $cache_expires;
var $cache_lifetime;
var $source;
var $forecast=array();
function weather($location, $lifetime, $unit, $cachedir)
{
$this->cache_lifetime = $lifetime;
$this->locationcode = $location;
$this->unit = $unit;
$this->cachedir = $cachedir;
$this->filename = $cachedir . $location;
}
function parse()
{
$this->allurl = "http://xml.weather.yahoo.com/forecastrss";
$this->allurl .= "?u=" . $this->unit;
$this->allurl .= "&p=" . $this->locationcode;
$this->parser = new xmlParser();
$this->parser->parse($this->allurl);
$content=&$this->parser->output[0]['child'][0]['child'];
foreach ($content as $item) {
switch ($item['name']) {
case 'TITLE':
case 'LINK':
case 'DESCRIPTION':
case 'LANGUAGE':
case 'LASTBUILDDATE':
$this->forecast[$item['name']]=$item['content'];
break;
case 'YWEATHER:LOCATION':
case 'YWEATHER:UNITS':
case 'YWEATHER:ASTRONOMY':
foreach ($item['attrs'] as $attr=>$value)
$this->forecast[$attr]=$value;
break;
case 'IMAGE':
break;
case 'ITEM':
foreach ($item['child'] as $detail) {
switch ($detail['name']) {
case 'GEO:LAT':
case 'GEO:LONG':
case 'PUBDATE':
$this->forecast[$detail['name']]=$detail['content'];
break;
case 'YWEATHER:CONDITION':
$this->forecast['CURRENT']=$detail['attrs'];
break;
case 'YWEATHER:FORECAST':
array_push($this->forecast,$detail['attrs']);
break;
}
}
break;
}
}
$this->source = 'live';
}
function writecache() {
unset($this->parser);
$this->cache_expires = time() + $this->cache_lifetime;
$fp = fopen($this->filename, "w");
fwrite($fp, serialize($this));
fclose($fp);
}
function readcache()
{
$content=@file_get_contents($this->filename);
if ($content==false) return false;
$intweather = unserialize($content);
if ($intweather->cache_expires < time()) return false;
$this->source = 'cache';
$this->forecast = $intweather->forecast;
return true;
}
function parsecached() {
if ($this->readcache()) return;
$this->parse();
$this->writecache();
}
}
?>

xmlparser.php
<?
class xmlParser{
var $xml_obj = null;
var $output = array();
function xmlParser(){
$this->xml_obj = xml_parser_create();
xml_set_object($this->xml_obj,$this);
xml_set_character_data_handler($this->xml_obj, 'dataHandler');
xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
}
function parse($path){
if (!($fp = fopen($path, "r"))) {
die("Cannot open XML data file: $path");
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_obj, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_obj)),
xml_get_current_line_number($this->xml_obj)));
xml_parser_free($this->xml_obj);
}
}
return true;
}
function startHandler($parser, $name, $attribs){
$_content = array('name' => $name);
if(!empty($attribs))
$_content['attrs'] = $attribs;
array_push($this->output, $_content);
}
function dataHandler($parser, $data){
if(!empty($data)) {
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['content'] = $data;
}
}
function endHandler($parser, $name){
if(count($this->output) > 1) {
$_data = array_pop($this->output);
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['child'][] = $_data;
}
}
function GetNodeByPath($path,$tree = false) {
if ($tree) {
$tree_to_search = $tree;
}
else {
$tree_to_search = $this->output;
}
if ($path == "") {
return null;
}
$arrPath = explode('/',$path);
foreach($tree_to_search as $key => $val) {
if (gettype($val) == "array") {
$nodename = $val[name];
if ($nodename == $arrPath[0]) {
if (count($arrPath) == 1) {
return $val;
}
array_shift($arrPath);
$new_path = implode($arrPath,"/");
return $this->GetNodeByPath($new_path,$val[child]);
}
}
}
}
}
?>
weathershow.php
<html>
<head>
</head>
<body>
<?
include("class.xml.parser.php");
include("class.weather.php");
$timeout=3*60*60; // 3 hours
if (isset($_ENV["TEMP"]))
$cachedir=$_ENV["TEMP"];
else if (isset($_ENV["TMP"]))
$cachedir=$_ENV["TMP"];
else if (isset($_ENV["TMPDIR"]))
$cachedir=$_ENV["TMPDIR"];
else
$cachedir="/tmp";
$cachedir=str_replace('\\\\','/',$cachedir);
if (substr($cachedir,-1)!='/') $cachedir.='/';
$weather_chile = new weather("CIXX0020", 3600, "C", $cachedir);
$weather_chile->parsecached();
print "<h1>Various</h1>";
print "title: ".$weather_chile->forecast['TITLE']."<br>";
print "city: ".$weather_chile->forecast['CITY']."<br>";
print "sunrise: ".$weather_chile->forecast['SUNRISE']."<br>";
print "sunset: ".$weather_chile->forecast['SUNSET']."<br>";
print "yahoolink: ".$weather_chile->forecast['LINK']."<br>";
print "<hr>";
print "<h1>Actual Situation</h1>";
print "acttext: ".$weather_chile->forecast['CURRENT']['TEXT']."<br>";
print "acttemp: ".$weather_chile->forecast['CURRENT']['TEMP']."<br>";
print "acttime: ".$weather_chile->forecast['CURRENT']['DATE']."<br>";
print "actcode: ".$weather_chile->forecast['CURRENT']['CODE']."<br>";
print "image: <img src=http://us.i1.yimg.com/us.yimg.com/i/us/we/52/".$weather_chile->forecast['CURRENT']['CODE'].".gif>";
print "<hr>";
for ($day=0; isset($weather_chile->forecast[$day]); $day++) {
print "<h1>Forecast Day $day</h1>";
print "day: ".$weather_chile->forecast[$day]['DAY']."<br>";
print "date: ".$weather_chile->forecast[$day]['DATE']."<br>";
print "low °C: ".$weather_chile->forecast[$day]['LOW']."<br>";
print "high °C: ".$weather_chile->forecast[$day]['HIGH']."<br>";
print "text: ".$weather_chile->forecast[$day]['TEXT']."<br>";
print "imgcode: ".$weather_chile->forecast[$day]['CODE']."<br>";
print "image: <img src=http://us.i1.yimg.com/us.yimg.com/i/us/we/52/".$weather_chile->forecast[$day]['CODE'].".gif>";
print "<hr>";
}
?>
</body>
</html>

thanks for help