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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help me better understand XML

    I created a xml file called text.xml
    I created a xsl file called text.xsl

    I open the browser localhost/text.xml and everything is fine.

    My question is, I want to use php to retrieve my data from mysql server. then put the data into xml file. so, I have to generage a xml file with the data I just retrieved. What if there is 10 users visiting my site. do I have to produce 10 xml files?

    I know there is a better way to do that, I just don't know how.
    Last edited by weronpc; 03-08-2004 at 02:55 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know much about PHP/XML, but I do know about PHP/HTML, and I'm assuming it's the same idea.

    Your XML file will actually be a PHP file, in that you will have PHP code embedded in your XML code. For example, if you need some database stuff for some following bit of XML code, you'd write the appropriate PHP code just before to get all the data together and use PHP echo statements to put your database data into the XML tags.

    xml_file.php:
    PHP Code:
    <xml>
     <?php
      $db 
    mysql_connect(...);
      
    mysql_select_db(...);

      
    $result mysql_query("select some_data from some_table");
      while(
    $row mysql_fetch_row($result)) {
     
    ?>

     <some_data>
      <?php echo $row[0]; ?>
     </some_data>

     <?php
      
    }//while
     
    ?>
    </xml>
    Does that sort of clear it up for you?

    If anyone else has additional comments, I'm sure they will post them to help you out.

    Sadiq.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    do I have to produce 10 xml files?
    PHP Code:
    you can either do a simple cache of the XML file ..

    <?
    header
    'content-type:text/xml' ) ;
    if(
    file_exists('blah.xml')){
        
    readfile('blah.xml');
    }else{
        
    //generate output from DB and save as blah.xml
    }
    ?>

    then whenever the data in the database changes ,
    delete blah.xml forcing a rewrite on the next page view...

    OR (and personally I think the above is preferable ?)

    <?
    header
    'content-type:text/xml' ) ;
    //build and output data
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To: Firepages
    }else{

    //generate output from DB and save as blah.xml

    }
    Do you think this will be fast? if I want search data, it will search in a text file.

    To: Sad69
    Your solution to me looks like only good for one user, what if 50 users visiting the site doing different queries? do I produce 50 xml files?

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    heh, I guess I don't know enough about xml... so I'm sorry I can't answer that.

    But in my mind I still think you only need the one file. But leave me outt a this one, I shouldn't have butted in!

    Sadiq.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    searching an XML file when you have a database available seems pointless to me unless you want to search the XML file clientside ,
    regardless you still end up with a blah.xml to work with.

    so you could alter to...

    PHP Code:
    assuming $var && $var1 are search parameters or identifiers
    <?
    $requested 
    "cache.$var.$var1.xml";
    if(!
    file_exists($requested)){
        
    //grab your content from the DB
        //build your xml file
        
    $xmlfile ='<'.'?xml';  $xml .='/*etc*/';
        
    //etc .. & save it
        
    $fp=fopen($requested,'w');
        
    fputs($fp,$xml);
        
    fclose($fp);
    }
    //output
    header'content-type:text/xml' ) ;
    readfile($requested);
    ?>
    and yes you may end up with a cache of several XML files [perhaps named to suit the queryparameters ... cache.$var.$var1.xml where $var & $var1 are search parmeters] but there is nothing you can do about that , at least they only get created once the first time someone does that particular query (and again whenever the database content changes)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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