Community Forums Today's Posts     Member List     Archive    
Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2006
    Location
    Ireland
    Posts
    269

    Need Some PHP Help

    OK I need some php help here.
    I know this hasnt realy got much to do with Teamspeak, but I dont know who else to get help from but you brainy people here.

    Ok I need to extract data from an XML file. I can get it into arrays but dont know how to extract it out of the array.

    Here the PHP
    PHP Code:
    <?php
    $xml 
    simplexml_load_file("file.xml");

    $result $xml->xpath("weather");

    print_r($result);

    ?>

    The above prints out the following.
    Code:
    Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array (
    [module_id] => 0 [tab_id] => 0 ) [current_conditions] =>
    SimpleXMLElement Object ( [condition] => SimpleXMLElement Object (
    [@attributes] => Array ( [data] => Cloudy ) ) [temp_f] =>
    SimpleXMLElement Object ( [@attributes] => Array ( [data] => 55 ) )
    [temp_c] => SimpleXMLElement Object ( [@attributes] => Array ( [data]
    => 13 ) ) [humidity] => SimpleXMLElement Object ( [@attributes] =>
    Array ( [data] => Humidity: 82% ) ) [icon] => SimpleXMLElement Object (
    [@attributes] => Array ( [data] => /images/weather/cloudy.gif ) )
    [wind_condition] => SimpleXMLElement Object ( [@attributes] => Array (
    [data] => Wind: S at 16 mph ) ) ) ) )
    The part that I need the most is [data] => Cloudy.


    If anyone can help I would be very greatful.

    Thanks,
    Andy

  2. #2
    Join Date
    Jul 2006
    Posts
    1,573
    do the official php sites not provide any xml reader examples?

  3. #3
    Join Date
    Jun 2006
    Location
    Ireland
    Posts
    269
    Yes that is were I got the bit of php code in my previous post, but I just want to know how to get the data out of the arrays.

  4. #4
    Join Date
    Jun 2006
    Location
    Ireland
    Posts
    269
    Anyone got any solutions???

  5. #5
    Join Date
    May 2008
    Location
    Berlin, Germany
    Posts
    18
    if you could post the xml file i could tell you how to get the data.

  6. #6
    Join Date
    Jun 2006
    Location
    Ireland
    Posts
    269
    Sorry for not replying quicker been bussy with other issues.

    The XML is as follows:
    Code:
    <xml_api_reply version="1">
    −
    	<weather module_id="0" tab_id="0">
    −
    	<forecast_information>
    <city data=""/>
    <postal_code data=""/>
    <latitude_e6 data="54650001"/>
    <longitude_e6 data="-6219999"/>
    <forecast_date data="2008-05-09"/>
    <current_date_time data="2008-05-09 07:50:00 +0000"/>
    <unit_system data="US"/>
    </forecast_information>
    −
    	<current_conditions>
    <condition data="Mostly Cloudy"/>
    <temp_f data="59"/>
    <temp_c data="15"/>
    <humidity data="Humidity: 77%"/>
    <icon data="/images/weather/mostly_cloudy.gif"/>
    <wind_condition data="Wind: SE at 8 mph"/>
    </current_conditions>
    −
    	<forecast_conditions>
    <day_of_week data="Today"/>
    <low data="48"/>
    <high data="64"/>
    <icon data="/images/weather/mostly_cloudy.gif"/>
    <condition data="Mostly Cloudy"/>
    </forecast_conditions>
    −
    	<forecast_conditions>
    <day_of_week data="Sat"/>
    <low data="46"/>
    <high data="62"/>
    <icon data="/images/weather/chance_of_rain.gif"/>
    <condition data="Chance of Rain"/>
    </forecast_conditions>
    −
    	<forecast_conditions>
    <day_of_week data="Sun"/>
    <low data="50"/>
    <high data="68"/>
    <icon data="/images/weather/mostly_sunny.gif"/>
    <condition data="Partly Sunny"/>
    </forecast_conditions>
    −
    	<forecast_conditions>
    <day_of_week data="Mon"/>
    <low data="50"/>
    <high data="69"/>
    <icon data="/images/weather/mostly_sunny.gif"/>
    <condition data="Mostly Sunny"/>
    </forecast_conditions>
    </weather>
    </xml_api_reply>
    Thanks for trying to help me, much apretiated.
    Andy

  7. #7
    Join Date
    May 2008
    Location
    Berlin, Germany
    Posts
    18
    ok i developed a class to parse this thingie.
    Actually a class is not needed but i thought it might be nice for extending it.

    At the moment the class just parses the xml (which is loaded via URI) and returns all it contents as an associative array via "getall()"-method.

    I tried to implement a "getone($which_one)"-method but didnt worked yet because the array is multidimensional. I ve just to work on on that xD

    So there is the code for you (the information is currently loaded from "weather.xml" in the current working directory. If you want to change this, just pass on a filename to the constructor to load from

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    class 
    weatherxml {
        public 
    $version;
        public 
    $module_id;
        public 
    $tab_id;
        public 
    $city;
        public 
    $postal_code;
        public 
    $latitude_e6;
        public 
    $longitude_e6;
        public 
    $forecast_date;
        public 
    $forecast_date_timestamp;
        public 
    $current_date_time;
        public 
    $current_date_time_timestamp;
        public 
    $unit_system;
        public 
    $current;
        public 
    $forecast;
        public function 
    __construct($url "weather.xml") {
            if((
    $xml simplexml_load_file($url)) === false)
                die(
    "XML Reading Error");

            
    $this->version = (int) $xml->attributes()->version;
            
    $this->module_id = (int) $xml->weather->attributes()->module_id;
            
    $this->tab_id = (int) $xml->weather->attributes()->tab_id;
            
            
    $shortcut = &$xml->weather->forecast_information;
            
    $this->city = (string) $shortcut->city->attributes()->data;
            
    $this->postal_code = (int) $shortcut->postal_code->attributes()->data;
            
    $this->latitude_e6 = (int) $shortcut->latitude_e6->attributes()->data;
            
    $this->longitude_e6 = (int) $shortcut->longitude_e6->attributes()->data;
            
    $this->forecast_date = (string) $shortcut->forecast_date->attributes()->data;
            
    $this->forecast_date_timestamp strtotime($this->forecast_date);
            
    $this->current_date_time = (string) $shortcut->current_date_time->attributes()->data;
            
    $this->current_date_time_timestamp = (strtotime(substr($this->current_date_time019)));
            
    $this->unit_system = (string) $shortcut->unit_system->attributes()->data;
            
            
    $shortcut = &$xml->weather->current_conditions;
            
    $this->current = array();
            
    $current = &$this->current;
            
    $current["condition"] = (string) $shortcut->condition->attributes()->data;
            
    $current["temp_f"] = (int) $shortcut->temp_f->attributes()->data;
            
    $current["temp_c"] = (int) $shortcut->temp_c->attributes()->data;
            
    $current["humidity"] = (int) $shortcut->humidity->attributes()->data;
            
    $current["icon"] = (string) $shortcut->icon->attributes()->data;
            
    $current["wind_condition"] = (string) substr($shortcut->wind_condition->attributes()->data6);
            
    $forecast = &$this->forecast;
            
    //We have 3 forecasts
            
    $i 0;
            foreach(
    $xml->weather->forecast_conditions as $input) {
                
    //the key will be the timestamp for the day
                //for which the forecast is
                
    $c_fc = &$forecast[$i];
                
    $c_fc["day_of_week"] = (string) $input->day_of_week->attributes()->data;
                
    $c_fc["low"] = (int) $input->low->attributes()->data;
                
    $c_fc["high"] = (int) $input->high->attributes()->data;
                
    $c_fc["icon"] = (string) $input->icon->attributes()->data;
                
    $c_fc["condition"] = (string) $input->condition->attributes()->data;
                
    $i++;
            }
        }
        function 
    getall() {
            return (array) 
    $this;
        }
    }
    $weather_parser = new weatherxml();
    $weather_info $weather_parser->getall();
    ?>
    <pre>
    <?php
    print_r
    ($weather_info);
    ?>
    </pre>
    Best regards Frank
    Last edited by Ch3ck3r; 17-05-2008 at 01:15.

  8. #8
    Join Date
    Jun 2006
    Location
    Ireland
    Posts
    269
    Lovely I cant thank you enough for helping me out, Thanks sooo much.
    I havent tried it yet as I am currently in uni, but ill be home tomo so ill run it then.

    So all ill need to do is work out how to pull the exact piece of data i need from the array.

    Emmm any ideas anyone on how to pull data from a multidimensional array?

    Thanks soo much Ch3ck3r, Ill get back to you on how it works.

    Andy

  9. #9
    Join Date
    May 2008
    Location
    Berlin, Germany
    Posts
    18
    getting the data you want is really easy.

    The first thing you need to do is to get the data which is really easy:
    PHP Code:
    //Location of the xml file with the information
    $url "weather.xml";
    //Parse the file
    $parsed = new weatherxml($url);
    //Get the data
    $data $parsed->getall(); 
    now $data contains the following data:
    Code:
    Array
    (
        [version] => 1
        [module_id] => 0
        [tab_id] => 0
        [city] => New York
        [postal_code] => 0
        [latitude_e6] => 54650001
        [longitude_e6] => -6219999
        [forecast_date] => 2008-05-09
        [forecast_date_timestamp] => 1210284000
        [current_date_time] => 2008-05-09 07:50:00 +0000
        [current_date_time_timestamp] => 1210312200
        [unit_system] => US
        [current] => Array
            (
                [condition] => Mostly Cloudy
                [temp_f] => 59
                [temp_c] => 15
                [humidity] => 0
                [icon] => /images/weather/mostly_cloudy.gif
                [wind_condition] => SE at 8 mph
            )
    
        [forecast] => Array
            (
                [0] => Array
                    (
                        [day_of_week] => Today
                        [low] => 48
                        [high] => 64
                        [icon] => /images/weather/mostly_cloudy.gif
                        [condition] => Mostly Cloudy
                    )
    
                [1] => Array
                    (
                        [day_of_week] => Sat
                        [low] => 46
                        [high] => 62
                        [icon] => /images/weather/chance_of_rain.gif
                        [condition] => Chance of Rain
                    )
    
                [2] => Array
                    (
                        [day_of_week] => Sun
                        [low] => 50
                        [high] => 68
                        [icon] => /images/weather/mostly_sunny.gif
                        [condition] => Partly Sunny
                    )
    
                [3] => Array
                    (
                        [day_of_week] => Mon
                        [low] => 50
                        [high] => 69
                        [icon] => /images/weather/mostly_sunny.gif
                        [condition] => Mostly Sunny
                    )
    
            )
    
    )
    so now, if you need (e.g.) the condition of the forecast in 3 days this would be the way
    PHP Code:
    echo $data["forecast"][3]["condition"
    This would output "Mostly Sunny". I've made the corresponding line bold in the data above, so that you can find what is printed out.

    I hope you are able to use this now ^^

  10. #10
    Join Date
    Jun 2006
    Location
    Ireland
    Posts
    269
    Ch3ck3r your a genius, it works a charm. Thank you, Thank you.
    I cant thank you enough for helpen me.


    Andy

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. .ASP TeamSpeak Script (No PHP please)
    By oceans777 in forum [TeamSpeak 2] Addons & Scripts
    Replies: 3
    Last Post: 12-11-2007, 03:52
  2. FreeBSD and MySQL
    By Paterson in forum [TeamSpeak 2] Server Support
    Replies: 1
    Last Post: 18-01-2006, 08:35
  3. show stats on PHP
    By Ace-SG1- in forum [TeamSpeak 2] Addons & Scripts
    Replies: 3
    Last Post: 18-12-2005, 17:51
  4. TS PHP Script
    By Neonis in forum [TeamSpeak 2] Client Support
    Replies: 1
    Last Post: 05-10-2005, 10:05
  5. TS2 Account management from php
    By Bones_taw in forum [TeamSpeak 2] Addons & Scripts
    Replies: 1
    Last Post: 12-03-2005, 20:35

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •