Open/read multiple subfolders and read all files in them, problem! (php + domxml)

Discussion in 'PHP' started by leet, May 20, 2009.

  1. #1
    I'm trying to code a simple CMS of my own based on XML for my senior project. I'm halfway done. I'm currently stuck on this one function where I need to open and read multiple folders, then read all the files in them so I can put them in my domxml_open_file() function like domxml_open_file($file) - I have different folders such as Housing_Stuff, College_Stuff etc. and .xml files named after their IDs in them such as 1.xml, 2.xml. I tried various methods to read multiple folders and stuff but can't get it to work and it is frustrating me to an extreme. Here are the codes:

    
    session_start();
    
    function printNow($array){
    	if(count($array) <= 1){
    		for ($i = 0; $i<count($array); $i++){
    			$node = $array[$i];
    			$value = $node->get_content();
    		}
    		return $value;
    	} 
    	
    }
    
    function folderlist(){
      $nowDir = './admin/';
      $ignoreDir[] = '.';
      $ignoreDir[] = '..';
      $ignoreDir[] = 'xmldata';
      $ignoreDir[] = 'images';
      $ignoreDir[] = 'backup';
      $ignoreDir[] = 'jscripts';
       if (is_dir($nowDir)){
           if ($dh = opendir($nowDir)){
               while (($folder = readdir($dh)) !== false){
                   if (!(array_search($folder,$ignoreDir) > -1)){
                     if (filetype($nowDir . $folder) == "dir"){
                           $dirList[$nowDir . $folder]['name'] = $folder;
                       }
                   }
               }
               closedir($dh);
           }
       }
    return($dirList);
    }
    
    $folders = folderlist();
      foreach ($folders as $folder){
        $folderName = $folder['name'];
    		 }
    
    $read = opendir("./admin/$folderName/");
    
    	$fileCount = 0;
    while ($file = readdir($read) and $fileCount < 5){
    	if (eregi("^\\.\\.?$", $file)) {
    		continue;
    	}
    
    $open = "./admin/$folderName/".$file;
    $xml = domxml_open_file($open);
    
     // Pull the stuff we need and build the links 
    
     $identify = $xml->root();
    
     $id = $identify->get_attribute("id");
    
     $d0cument = $xml->root();
     $hl_array = d0cument->get_elements_by_tagname("headline");
     $hline = printNow($hl_array);
    
     $d0cument = $xml->root();
     $sd_array = $d0cument->get_elements_by_tagname("summ");
     $sd = printNow($sd_array);
    
     $d0cument = $xml->root();
     $aa_array = $d0cument->get_elements_by_tagname("author");
     $aa = printNow($aa_array);
    
     $d0cument = $xml->root();
     $ada_array = $d0cument->get_elements_by_tagname("date");
     $ada = printNow($ada_array);
    
    $fileCount++;
    
    }
    
    <center><table width="625" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="25" background="<?echo$sitetheme;?>/images/orta.gif"><b><h3><? echo $lang['article'];?></b></h3></font>
    </td>
      </tr>
    </table>
    </center>
    <?
    
    echo '<div id="div">';
    echo '<ul id="sidebarnav_Main">';
    echo "<li><a href=article.php?id=".$id.">".$ada." > ".$folderName." > ".$hline."</a></li></ul><div>";
    
    ?>
    
    PHP:
    The output of this is:

    > College_Stuff >

    with the link not having any of the properties I want it to have such as $id etc. And the $folderName function apparently just gets the name of one folder, when there are 2 other folders it should be taking the names of. When I change the $folderName with an actual folder name such as Housing_Stuff for these 2 lines:

    $read = opendir("./admin/$folderName/");

    $open = "./admin/$folderName/".$file;

    The functions work fine and the script pulls the properties of the file but it only pulls 2.xml, and there is another .xml file in that folder named 1-1242743475.xml. I'm totally stuck and in need of help. Hopefully I was able to explain the problem well enough.
     
    leet, May 20, 2009 IP
  2. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    I would approach that completely different.
    Using simple process.

    // each folder with its own variable name.
    $collegstuff = "folder/Collge_stuff.txt";
    $housingstuff = "folder/Housing_stuff.txt";
    etc.

    then open them each.
    $fp = fopen($collegstuff, "r") or die("Couldn't open $collegstuff");
    etc.

    and then turn the text in those files into variables.
    $collegestuff_content =" (the files contents)" ;
    etc.

    look at the fopen etc commands.
    Pull the text in from each file and convert it to a PHP variable.
    then you can place the entire text of each file nicely by using its variable name like

    echo $collegestuff_contents;

    in the area of files, folders, or webspages ypu want it displayed.
     
    ezprint2008, May 21, 2009 IP
  3. leet

    leet Notable Member

    Messages:
    3,423
    Likes Received:
    369
    Best Answers:
    0
    Trophy Points:
    250
    #3
    Thanks for the suggestion.

    I thought about that, however, then I'll have to do some major editing on admin panel as well for category adding function.
     
    leet, May 21, 2009 IP