Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    55
    Thanks
    8
    Thanked 2 Times in 1 Post

    Displaying XML Data on a web page

    Hi, ok I'm going to start from the beginning. I have a php script that lists all of the xml files in a directory by filename. I am wondering if there is a way instead of it listing by filename to get a fields data in the xml file and display it by that. so instead of getting filename.xml it would get for example <field>Title Name</field> and display Title Name. Hope this question makes sense lol. Thanks in advance

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by dustywb View Post
    Hi, ok I'm going to start from the beginning. I have a php script that lists all of the xml files in a directory by filename. I am wondering if there is a way instead of it listing by filename to get a fields data in the xml file and display it by that. so instead of getting filename.xml it would get for example <field>Title Name</field> and display Title Name. Hope this question makes sense lol. Thanks in advance
    'Title Name' field is in a separate xml or exists in each xml file from that directory?
    You have multiple solution in both cases but you need to build or get the pair filename - title name someway

    best regards

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    55
    Thanks
    8
    Thanked 2 Times in 1 Post
    Thanks for the reply. There are multiple xml files in the directory, not a specific amount it's sort of a contact directory, anyone can add their information to it so theres no specific amount of people. Each xml file has a name field and I would rather just so it looks pretty have the link name be the persons name that is specified in the xml file instead of the name of the xml file. For example instead of "thisfile.xml" would have "Juan Johnson" or whatever

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by dustywb View Post
    Thanks for the reply. There are multiple xml files in the directory, not a specific amount it's sort of a contact directory, anyone can add their information to it so theres no specific amount of people. Each xml file has a name field and I would rather just so it looks pretty have the link name be the persons name that is specified in the xml file instead of the name of the xml file. For example instead of "thisfile.xml" would have "Juan Johnson" or whatever
    you can use dom or simplexml php extensions:

    http://www.php.net/manual/en/book.dom.php
    http://www.php.net/manual/en/book.simplexml.php

    based on what you have done until now, you can use the file list from your script to open each xml file, read the field and build a hash array with keys filename and values the value of the field for each filename.
    if you post the code you have I guess I could say where and what to modify.

    best regards

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    55
    Thanks
    8
    Thanked 2 Times in 1 Post
    I'm trying to figure it out, looks a bit confusing, anyways here's my code
    PHP Code:
    $ext 'xml';
    $dirname "pages/"
    $files scandir($dirname);

      foreach(
    $files as $file)
      {
        if((
    $file != ".") and ($file != ".."))
        {
          
    $fileChunks explode("."$file);
          if(
    $fileChunks[1] == $ext) {

        echo 
    '<div class="info"><a href="#" onclick=\'open("pages/' $file '","","width=300,height=250"); return false;\'>' $file '</a></div>';
          }
        }


  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    PHP Code:
    <?php
    $ext 
    'xml';
    $dirname "pages/"
    $files scandir($dirname);

    foreach(
    $files as $file){
      if((
    $file != ".") and ($file != "..")){
        
    $fileChunks explode("."$file);
        if(
    $fileChunks[1] == $ext){
          
    $title $file;
          
    $doc = new DOMDocument();
          if(
    $doc && $doc->load($file)){
            
    $xpath = new DOMXPath($doc);
            
    $query "*//field";
            
    $entries $xpath->query($query);
            if(
    $entries->length 0){
              
    $title $entries->item(0)->nodeValue;
            }
          }
          
          echo 
    '<div class="info"><a href="#" onclick=\'open("pages/' $file '","","width=300,height=250"); return false;\'>' $title '</a></div>
    '
    ;
        }
      }
    }
    the xpath expresion from $query must point to your field and you must change it.
    post a xml example if you have problem with that.

    best regards

  • The Following 2 Users Say Thank You to oesxyl For This Useful Post:

    dustywb (10-31-2008), itsallkizza (10-31-2008)

  • #7
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Just wanted to thank your last post because of the scandir function... I'd never seen that before and it's gonna save me a buttload of coding

  • #8
    New Coder
    Join Date
    Nov 2004
    Posts
    55
    Thanks
    8
    Thanked 2 Times in 1 Post
    First off let me appologize I feel like I'm being a pain, but I can't seem to get this to work. You have been a big help so far and I can't thank you enough. Hopefully I will understand this stuff eventually, reading and trying but for now most of it doesn't make sense Anyways heres what the xml files look like:

    <info>
    <name>Some Guy</name>
    <phone>555-555-5555</phone>
    <address>1313 Elm St</address>
    <city>Sometown</city>
    <state>OK</state>
    <zip>77777</zip>
    <email>email@mail.net</email>
    <website>www.codingforums.com</website>
    </info>


    Quote Originally Posted by oesxyl View Post
    try this:
    PHP Code:
    <?php
    $ext 
    'xml';
    $dirname "pages/"
    $files scandir($dirname);

    foreach(
    $files as $file){
      if((
    $file != ".") and ($file != "..")){
        
    $fileChunks explode("."$file);
        if(
    $fileChunks[1] == $ext){
          
    $title $file;
          
    $doc = new DOMDocument();
          if(
    $doc && $doc->load($file)){
            
    $xpath = new DOMXPath($doc);
            
    $query "*//field";
            
    $entries $xpath->query($query);
            if(
    $entries->length 0){
              
    $title $entries->item(0)->nodeValue;
            }
          }
          
          echo 
    '<div class="info"><a href="#" onclick=\'open("pages/' $file '","","width=300,height=250"); return false;\'>' $title '</a></div>
    '
    ;
        }
      }
    }
    the xpath expresion from $query must point to your field and you must change it.
    post a xml example if you have problem with that.

    best regards

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by dustywb View Post
    First off let me appologize I feel like I'm being a pain, but I can't seem to get this to work. You have been a big help so far and I can't thank you enough. Hopefully I will understand this stuff eventually, reading and trying but for now most of it doesn't make sense
    it's no problem,
    to extract field name with value "Some Guy", xpath expression is /info/name
    Code:
    <?xml version="1.0"?>
    <info>
    	<name>Some Guy</name>
    	<phone>555-555-5555</phone>
    	<address>1313 Elm St</address>
    	<city>Sometown</city>
    	<state>OK</state>
    	<zip>77777</zip>
    	<email>email@mail.net</email>
    	<website>www.codingforums.com</website>
    </info>
    PHP Code:
    <?php
    $ext 
    'xml';
    $dirname "pages/"
    $files scandir($dirname);

    foreach(
    $files as $file){
      if((
    $file != ".") and ($file != "..")){
        
    $fileChunks explode("."$file);
        if(
    $fileChunks[1] == $ext){
          
    $title $file;
          
    $doc = new DOMDocument();
          if(
    $doc && $doc->load($file)){
            
    $xpath = new DOMXPath($doc);
            
    $query "/info/name";
            
    $entries $xpath->query($query);
            if(
    $entries->length 0){
              
    $title $entries->item(0)->nodeValue;
            }
          }
          
          echo 
    '<div class="info"><a href="#" onclick=\'open("pages/' $file '","","width=300,height=250"); return false;\'>' $title '</a></div>
    '
    ;
        }
      }
    }
    Last edited by oesxyl; 11-01-2008 at 01:49 AM.

  • Users who have thanked oesxyl for this post:

    dustywb (11-01-2008)

  • #10
    New Coder
    Join Date
    Nov 2004
    Posts
    55
    Thanks
    8
    Thanked 2 Times in 1 Post

    Smile

    WOOHOO OMFG IT WORKS YOU ARE AWESOME I BOW BEFORE YOU... Lol but seriously thanks a ton, it works great!


    Quote Originally Posted by oesxyl View Post
    it's no problem,
    to extract field name with value "Some Guy", xpath expression is /info/name
    Code:
    <?xml version="1.0"?>
    <info>
    	<name>Some Guy</name>
    	<phone>555-555-5555</phone>
    	<address>1313 Elm St</address>
    	<city>Sometown</city>
    	<state>OK</state>
    	<zip>77777</zip>
    	<email>email@mail.net</email>
    	<website>www.codingforums.com</website>
    </info>
    PHP Code:
    <?php
    $ext 
    'xml';
    $dirname "pages/"
    $files scandir($dirname);

    foreach(
    $files as $file){
      if((
    $file != ".") and ($file != "..")){
        
    $fileChunks explode("."$file);
        if(
    $fileChunks[1] == $ext){
          
    $title $file;
          
    $doc = new DOMDocument();
          if(
    $doc && $doc->load($file)){
            
    $xpath = new DOMXPath($doc);
            
    $query "/info/name";
            
    $entries $xpath->query($query);
            if(
    $entries->length 0){
              
    $title $entries->item(0)->nodeValue;
            }
          }
          
          echo 
    '<div class="info"><a href="#" onclick=\'open("pages/' $file '","","width=300,height=250"); return false;\'>' $title '</a></div>
    '
    ;
        }
      }
    }

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by dustywb View Post
    WOOHOO OMFG IT WORKS YOU ARE AWESOME I BOW BEFORE YOU... Lol but seriously thanks a ton, it works great!
    you are welcome,

    best regards


  •  

    Posting Permissions

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