从PHP循环中生成XML文件 [英] Generate an XML file out of a PHP loop
问题描述
我正在为FAQ进行实时搜索,为此,我需要一个XML文档,用于存储实时搜索建议.
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.
问题在于FAQ会经常更新,我不想每次都碰代码.
The problem is that the FAQ will be updated often and I don't want to have to touch the code every time.
因此,我试图制作一个PHP文件,当我们打开它时会生成一个新的XML文件.由于不同文件夹中有许多常见问题解答项目,因此我需要脚本自动在特定目录中查找所有常见问题解答项目. FAQ项存储在TXT文件中,该文件存储在使用数字作为文件名(1、2、3等)的目录中,因此我可以轻松地添加/修改/删除它们.
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.
路径如下所示: faqs/professionals/1/question.txt 或 faqs/professionals/2/question.txt 用于第二个常见问题解答项目或 faqs/students/1/question.txt 用于另一个主题中的常见问题解答项目.
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.
例如,如果我想添加一个新的常见问题解答项目,我只需创建一个文件夹,假设为"3",然后在其中添加2个文本文件( question.txt 和 answer.txt ).然后,我将运行PHP脚本,以便XML文件将考虑到新的常见问题解答项目"3"来更新建议.
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".
我能够创建一个基于TXT文件内容构建XML文件的脚本.但是,我无法使其检测"所有文件夹(FAQ项所在的位置)并将它们添加到XML文件中.
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.
该脚本基于以下页面: w3schools.com/php/php_ajax_livesearch.asp
The script is based on this page: w3schools.com/php/php_ajax_livesearch.asp
这是代码:
<?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")
?>
一旦该脚本开始运行,当我们单击建议时,我将尝试使实时搜索在文本输入下方显示answer.txt的内容.但这已经是另一个问题了……
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. :)
推荐答案
我认为问题在于,当您构建第一个$pages
数组时,您会使用dirname($x)
.这是目录的目录名称.因此,glob()返回的路径为faq/professionals/1
,并以该目录名命名,最后得到faq/professionals
.
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
.
所以我认为您需要更改的只是URL的设置...
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
);
这篇关于从PHP循环中生成XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!