Generate an XML file out of a PHP loop

痴心易碎 提交于 2019-12-24 18:25:11

问题


I am building a livesearch for a FAQ and in order to do so, I need an XML document where the livesearch suggestions will be stored.

The problem is that the FAQ will be updated often and I don't want to have to touch the code every time.

Therefore, I am trying to make a PHP file that generates a new XML file when we open it. Since there are many FAQ items in different folders, I need that the script automatically finds all the FAQ items in a certain directories. The FAQ items are stored in TXT files which are stored in directories that use numbers as filenames (1, 2, 3, etc) so I can add/modify/delete them with ease.

A path would look like this: faqs/professionals/1/question.txt or faqs/professionals/2/question.txt for a second FAQ item or faqs/students/1/question.txt for a FAQ item in a different topic.

For example, if I wanted to add a new FAQ item, I would just create a folder, let's say "3", and add 2 text files inside (question.txt and answer.txt). Then, I would run the PHP script so the XML file would update the suggestions, taking into account the new FAQ item "3".

I was able to make a script that builds the XML file based on the content of the TXT file. However, I am not able to make it "detect" all the folders (where the FAQ items are) and add them to the XML file.

The script is based on this page: w3schools.com/php/php_ajax_livesearch.asp

Here is the code:

<?php 
foreach (glob("../faqs/professionals/*",GLOB_ONLYDIR) as $x) {
  $pages = array(); 
  $pages [] = array( 
  'title' => file_get_contents($x.'/question.txt'), 
  'url' => dirname($x),
  );   
}

  $doc = new DOMDocument(); 
  $doc->formatOutput = true; 

  $r = $doc->createElement( "pages" ); 
  $doc->appendChild( $r ); 

  foreach( $pages as $link ) 
  { 
  $b = $doc->createElement( "link" ); 

  $title = $doc->createElement( "title" ); 
  $title->appendChild( 
  $doc->createTextNode( $link['title'] ) 
  ); 
  $b->appendChild( $title ); 

  $url = $doc->createElement( "url" ); 
  $url->appendChild( 
  $doc->createTextNode( $link['url'] ) 
  ); 
  $b->appendChild( $url ); 

  $r->appendChild( $b ); 
  } 

  echo $doc->saveXML(); 
  $doc->save("links.xml") 
  ?>

Once this script will be working, I will try to make the livesearch display the content of answer.txt below the text input when we click on a suggestion. But this is already another problem...

Any help will be greatly appreciated. :)


回答1:


I think the problem is that when your building up the first $pages array, your using dirname($x). This is taking the directory name of the directory. So glob() is returning a path of faq/professionals/1 and your taking the directory name of this, you end up with faq/professionals.

So I think all you need to change is the setting of the url...

$pages [] = array(
   'title' => file_get_contents($x.'/question.txt'),
   'url' => $x
);


来源:https://stackoverflow.com/questions/48669292/generate-an-xml-file-out-of-a-php-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!