Results 1 to 10 of 10

Thread: getting xml data using php querystring

  1. #1
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getting xml data using php querystring

    Hello,

    I have an XML file like this:
    Code:
    <?xml version="1.0"?>
    <document>
    <symptoms>
    <symptom>
    <type>general</type>
    <area>legs</area>
    <title>Ankle Pain</title>
    <url>/symptoms/symptom-1.html</url>
    <contentID>anklePain</contentID>
    </symptom>
    
    <symptom>
    <type>female</type>
    <area>abdomen_pelvis</area>
    <title>Bleeding After Menopause</title>
    <url>/symptoms/symptom-2.html</url>
    <contentID>BleedingAfterMenopause</contentID>
    </symptom>
    
    <symptom>
    <type>male</type>
    <area>abdomen_pelvis</area>
    <title>Blood in the Urine in Men</title>
    <url>/symptoms/symptom-3.html</url>
    <contentID>bloodintheurineinmen</contentID>
    </symptom>
    </symptoms>
    </document>
    I would like to have a PHP script that outputs the exact part of XML file when url is written by a query criteria. For example

    http://www.mydomain.com/script.php?t...abdomen_pelvis

    will output:

    Code:
    <document>
    <symptoms>
    <symptom>
    <type>male</type>
    <area>abdomen_pelvis</area>
    <title>Blood in the Urine in Men</title>
    <url>/symptoms/symptom-3.html</url>
    <contentID>bloodintheurineinmen</contentID>
    </symptom>
    </symptoms>
    </document>
    and for example if the query criteria is "male": www.mydomain.com/script.php?type=male

    the output should contain both general and male tags in xml file ( or if the criteria is "female" it should take "female" and "general" tags):

    Code:
    <document>
    <symptoms>
    <symptom>
    <type>general</type>
    <area>legs</area>
    <title>Ankle Pain</title>
    <url>/symptoms/symptom-1.html</url>
    <contentID>anklePain</contentID>
    </symptom>
    
    <symptom>
    <type>male</type>
    <area>abdomen_pelvis</area>
    <title>Blood in the Urine in Men</title>
    <url>/symptoms/symptom-3.html</url>
    <contentID>bloodintheurineinmen</contentID>
    </symptom>
    </symptoms>
    </document>
    I am new at PHP, I've tried something but I couldn't get the result I want. I don't know it is a complex problem or not. Can you please help?

    Thank you.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I'd recommend looking into the simpleXML extension. It's a little complex if you aren't familiar with PHP and Object-Oriented Programming; however, there isn't really a simpler way to deal with XML.

    You might consider using a database for this instead. If you're not dealing with a lot of entries, you won't notice much of a problem; but if your XML file gets very large, it can become difficult to manage.

    In order to read an XML file, PHP must load the entire file and create an object to hold it. This is not a very efficient way to deal with large amounts of data. A database would give you more flexibility in storing the information, and searching would be much faster as well.

  3. #3
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks traq. Do you have any idea bout the codding part for XML only? I don't want to do anything with a db yet.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Basically, SimpleXML takes the entire XML document and creates an object out of it. So, you could take the query string and look for those values:

    PHP Code:
    $xmlfile '/path/to/file.xml';

    $XML = new SimpleXMLElement($xmlfile);

    foreach(
    $XML->symptoms->symptom as $symptom){
      if(
    $symptom->type == 'male' || $symptom->type == 'general'){
        
    // print out symptoms
      
    }

    This is just a general example, it won't work as-is. It does show the drawbacks of using this method: you have to load everything and compare everything to find the one entry you want. As database finds it quickly, and you only have to deal with the one entry.

    Read through the SimpleXML examples and see if you can figure it out. If you have any specific questions, I'll be happy to help. If you need someone to do it for you, you might consider posting in the paid help forum.

  5. #5
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi traq, I checked out your website in your signature. I guess you can help me. How can I reach you for professional work and detailed information about the job?

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you can use my contact page.

    However, be aware that I will recommend the use of a database. If a database is simply not an option, we can use xml; but for this purpose a database is the better choice, by a longshot.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Just to add a comment about what traq is saying: XML can do this, but it will become sluggish at some point and increasingly more difficult to work with. So a database might be slightly harder in some sense, but it will end up being less work to deal with because it won't have the problems that XML would. (In other words, I completely agree with his recommendation.)

    If you really want to use XML, you could actually import XML into the database (that would be more work, but it's possible to transfer data between the two). So this could allow you to use your current information or whatever is required.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Just to add a comment about what traq is saying: XML can do this, but it will become sluggish at some point and increasingly more difficult to work with. So a database might be slightly harder in some sense, but it will end up being less work to deal with because it won't have the problems that XML would. (In other words, I completely agree with his recommendation.)

    If you really want to use XML, you could actually import XML into the database (that would be more work, but it's possible to transfer data between the two). So this could allow you to use your current information or whatever is required.
    Yeah I got it. I think I will use database option. Thank you.

  9. #9
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    you can use my contact page.

    However, be aware that I will recommend the use of a database. If a database is simply not an option, we can use xml; but for this purpose a database is the better choice, by a longshot.
    I sent you a message thru contact page. Did you get it?

    Thanks.

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

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
  •