Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Dedicated Servers Hosting, UK Reseller, US Reseller, Forum Hosting, Business Web Hosting
+ Reply to Thread
Results 1 to 3 of 3

Thread: Php And Xml

  1. #1
    Administrator Jaan's Avatar
    Join Date
    Nov 2009
    Location
    Estonia
    Posts
    372
    Points
    968
    Hey!



    I was thinking about writing a new tutorial for you. So here it is.



    I'm going to show you what you can do with SimpleXML in PHP and how to get info from XML file.



    Okay this is our XML file.



    users.xml



    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <users>
    
        <user id="1">
    
            <username>Jaan</username>
    
            <age>18</age>
    
            <gender>male</gender>
    
            <location>Estonia</location>
    
        </user>
    
        <user id="2">
    
            <username>Mike777</username>
    
            <age>25</age>
    
            <gender>male</gender>
    
            <location>United States</location>
    
        </user>
    
        <user id="3">
    
            <username>DingoNKangarooLove</username>
    
            <age>12</age>
    
            <gender>female</gender>
    
            <location>Australia</location>
    
        </user>
    
        <user id="4">
    
            <username>TingTongPingPong</username>
    
            <age>36</age>
    
            <gender>male</gender>
    
            <location>China</location>
    
        </user>
    
    </users>


    Hm.. Boss told you to list all users. Okay it goes like this:



    Code:
    <?php
    
    
    
    $xml = simplexml_load_file("users.xml");
    
    
    
    $count = count($xml->user)-1;
    
    
    
    for($i=0;$i<=$count;$i++){
    
        
    
        echo $xml->user[$i]->username."
    ";
    
        
    
    }
    
    
    
    ?>


    And it prints this:



    Jaan

    Mike777

    DingoNKangarooLove

    TingTongPingPong


    Eee.. okay it prints this but what did you wrote??!!1



    Ok.. it worked like this:



    $xml = simplexml_load_file("users.xml");

    This loads our XML file to $xml variable



    $count = count($xml->user)-1;

    This counts how many users we have in this file.

    It counts <user> tags. This "-1" is there because counting tags in XML file stats from 0.



    for($i=0;$i<=$count;$i++){

    Now let's go for a loop. It loops it just as many times how many users are in XML file.



    echo $xml->user[$i]->username."
    ";


    Let's show our users.

    First <user> tag is 1, second <user> tag is 2 and so on. Now in loop we are taking those <user> tags like this user[$i] whitch is like user['1'], user['2'].



    Oh yea.. it works but now your Boss told you to get user from XML database and print user with all information.



    Hm.. ok it goes like this:



    Code:
    <?php
    
    
    
    $xml = simplexml_load_file("users.xml");
    
    
    
    $count = count($xml->user)-1;
    
    
    
    for($i=0;$i<=$count;$i++){
    
        
    
        echo "Username: ".$xml->user[$i]->username."
    "
    
            ."Age: ".$xml->user[$i]->age."
    "
    
            ."Gender: ".$xml->user[$i]->gender."
    "
    
            ."Location: ".$xml->user[$i]->location."
    
    ";
    
        
    
    }
    
    
    
    ?>


    It works just like the first script but it just have more information.

    It works like this:



    Code:
    [1]    [2]         [3]
    
    $xml->user[$i]->username;


    [1] XML file

    [2] Container tag (For us it's <user>)

    [3] Information tag (We have <username>, <age>, <gender> and <location>)



    Now Boss is happy but he wants to get information about users dynamically by IDs. And he wants to take information like this from url: http://company.com/db/getUsers.php?id={id_here}.







    Hm.. okay it works like this:



    Code:
    <?php
    
    
    
    $xml = simplexml_load_file("users.xml");
    
    
    
    $id = $_GET['id'];
    
    $num = $id-1;
    
    
    
    foreach($xml->children() as $info){
    
        
    
        if($info['id'] == $id){
    
            
    
            echo "Username: ".$xml->user[$num]->username."
    "
    
                ."Age: ".$xml->user[$num]->age."
    "
    
                ."Gender: ".$xml->user[$num]->gender."
    "
    
                ."Location: ".$xml->user[$num]->location;
    
            
    
        }
    
        
    
    }
    
    
    
    ?>


    Eee.. explain?

    Ok.. it goes like this:



    $xml = simplexml_load_file("users.xml");

    Load XML file



    $id = $_GET['id'];

    Get ID from URL



    $num = $id-1;

    Let's calculate our <user> location



    foreach($xml->children() as $info){

    Well our user tag looks like this: <user id="2"> and this $xml->children() creates an array of those id="2" things.



    if($info['id'] == $id){

    Now let's get that id="2" and if id IS for example 2 then let's display user's info.



    echo "Username: ".$xml->user[$num]->username."
    "


    This works just like I explained before. It takes <user> tags location and displays it's <username>.



    ."Age: ".$xml->user[$num]->age."
    "


    ."Gender: ".$xml->user[$num]->gender."
    "


    ."Location: ".$xml->user[$num]->location;

    These things work just like username.



    }

    This closes if



    }

    And this closes foreach



    Well.. This is it. Your boss is now happy and you are happy because you learned to use XML files as a "database" and how to take info from that "database".



    If you have questions just ask, don't be afraid



    Regards,

    Jaan
    Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
    Reply With Quote Reply With Quote  
    Share with Facebook

  2. #2
    Member CrazyQuentin's Avatar
    Join Date
    Mar 2010
    Posts
    36
    Points
    36
    I was looking for that !

    Very nice tutorial!
    Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
    Reply With Quote Reply With Quote  
    Share with Facebook

  3. #3
    Administrator Jaan's Avatar
    Join Date
    Nov 2009
    Location
    Estonia
    Posts
    372
    Points
    968
    Well, it's good that it helped you, thank you
    Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
    Reply With Quote Reply With Quote  
    Share with Facebook

+ Reply to Thread

Bookmarks

Posting Permissions

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