I hope someone can steer me in the right direction here!

I'm pretty familiar with XML and I'm just getting comfortable with PHP, however, I'm having a hard time making them work together! 😕

Here's what I'm trying to do:

I have multiple XML files which each contain a question and an answer for a department. I want to keep them separate so that each department can add to their questions/answers as new ones come in.

<?xml version="1.0" ?> 
<howdoi>
 <questions>
   <level1>Benefits</level1>
   <level2>Time Off</level2>
   <level3>Vacations</level3>
   <dept>Human Resources</dept>
   <question>
     Can I take time off to visit my child's school?
   </question>
   <answer>
Any employee who is a parent, guardian, or grandparent with custody of a child in kindergarten or grades 1 to 12 is entitled to take up to 40 hours off each school year... blah blah blah
     </answer>
   </questions>
</howdoi>

What I'm trying to do is create a central help page that will read each of the XML files within a directory and put them into a form where people can select the questions by department and then sub-categories (picture a format like iTunes).

Ideally, this help page will dynamically pull the categories and sublevels (as well as the Q&A) from the XML documents. Each category of each level should be a link that will change the results if someone goes back and selects another category.

So if someone selected Human Resources > Benefits > Time Off > Vacations, they would see something like this:

Department.....................|...Level 1...........|...Level 2.......|...Level 3
--------------------------------|-------------------|------------------|-----------
Administration.................|...<Benefits>.....|...Retirement..|...<Vacations>

<Human Resources>.......|...Job & Pay.......|...<Time Off>..|...Jury Duty
Information Technology...|...Personal Info.|...Healthcare..|...Sick Days


Related Questions:
Can I take time off to visit my child's school?
How many vacation hours do I get a year?
Does my vacation time accrue if I don't use it?

All I can find when I search the forums for XML is how to read just one file:

$xml = simplexml_load_file('path_to_xml_file.xml');

What do I need to do for PHP to read through multiple files and grab the data I need? Can PHP do this with XML?

Thanks!
eCat

    Is no one responding because I didn't make my problem clear enough?

    Or is it just so scary that everyone is speechless? :o

    Maybe someone can just answer me this:

    Can PHP parse more than one XML document at a time? Or do I need to look to another solution (maybe AJAX?) for what I want to do?

    Thanks again!
    eCat

      I haven't tried it myself, but could you just have multiple calls to simplexml_load_file()?

      $xml[0] = simplexml_load_file('file1.xml');
      $xml[1] = simplexml_load_file('file2.xml');

      Then you can grab the information you need for all the different XML files.

        That would work, but only if the files weren't going to change on a regular basis.

        The plan is that departments will be able to continually add questions to their directory, thus adding new XML files on a weekly basis.

        If I had calls to specific XML files, I'd have to go in and edit the calls as new files came in, would I not? 😕

          Hmm, interesting dilemma. Looks like you'd need some way to create dynamic filenames. Possibly store all the XML files in a single directory, then load all the XML from that directory. You'd need to use opendir and readdir. Though, if you do this, you'll end up loading far more XML files than you need to display.

          A way around that, is to pre-filter the XML files by placing them into particular sub-directories. This would decrease the number of XML files you needed to load each time you increased the level of the sub-directories.

            Hmmmm!

            That's an interesting solution... I suppose I could force users to save their files as "departmentname1.xls, departmentname2.xls" etc. Then I might be able to create a foreach loop that looks for the files "departmentname" and "1++" until there are no more files in that department (and yes, I was going to have a separate folder/dir for each dept).

            That way I'd only have to change the code if we added new departments.

            It's food for thought anyway! Thanks wilsonodk... that's more than I had to work with before. 😃

            If anyone else has some thoughts, I'll leave the post "unresolved" for a day or so before I mark it as answered.

              Note that you don't even need to force them to save them with particular file names if you do something like this:

              if ($handle = opendir('/path/to/files')) {
                while (false !== ($file = readdir($handle))) {
                  // Check if $file is a file
                  if (is_file($file)) {
                    // Check if it's an .xml file
                    if (strpos($file,'.xml') !== false) {
                      /* This is an XML file */
                      $xml[] = simplexml_load_file('/path/to/files/'.$file);
                    }
                  }
                }
              }
              
              /* We've loaded all our XML files
              We can now loop over $xml and gather page content */
              

              Good luck to you!

                Very clever!! I like it. 🆒

                Here's what I've done so far:

                I have my class in a separate file:

                <?php
                
                class list_dir
                {
                  var $dir;
                
                function open_dir($dir) {
                  $this->dir = $dir;
                  $handle = opendir($this->dir);
                
                  if($handle) {
                
                   while(false !== ($file = readdir($handle))) {
                
                 if($file != "." && $file != "..") {
                   $files[] = $file;
                 } //close nested if
                
                   } //close while
                } //close if
                
                   sort($files);
                   foreach($files as $file) {
                     echo $file."<br>";
                   } //close foreach
                
                } //close function
                
                
                function close_dir()
                  {
                   closedir();
                  }
                }
                
                ?> 

                And my use of it here:

                <?php
                require 'list-class2.php';
                
                $list = new list_dir;
                $list->open_dir("xml/");
                $list->close_dir();
                
                ?> 

                Which works quite well except that it gives me an error when trying to closedir:

                benefits1.xml
                benefits2.xml
                tech1.xml
                tech2.xml
                admin1.xml
                admin2.xml

                Warning: closedir(): supplied argument is not a valid Directory resource in /home/ecatnet/public_html/php/list-class2.php on line 32

                I tried closedir($handle); but that didn't work either.... maybe I haven't had enough coke yet today! :eek:

                  Write a Reply...