从SQL DB写入XML的PHP​​文件夹结构 [英] PHP Folder Structure from SQL DB written to XML

查看:115
本文介绍了从SQL DB写入XML的PHP​​文件夹结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我为这篇文章的大小道歉;我不太确定如何解释自己在做什么(我已经醒了大约19个小时).

我正在尝试使用PHP从SQL DB中提取文件夹结构,然后编写一个模仿该文件夹结构的xml文件.

因此,最终结果是一个类似于以下内容的XML文件:

Firstly, let me apologise for the size of this post; I''m not quite sure how to explain what I''m doing (and I''ve been awake for about 19 hours now).

What I am trying to do is use PHP to pull a folder structure from a SQL DB, then write an xml file that mimics the folder structure.

So the end result is an XML file that looks similar to:

<FOLDERS>
    <root id="0" parentID="-1">
        <folder1 id="1" parentID="0" />
        <folder2 id="2" parentID="0" />
        <folder3 id="3" parentID="0" />
            <folder4 id="4" parentID="3" />
            <folder5 id="5" parentID="3" />
                <folder6 id="6" parentID="5" />
        <folder7 id="7" parentID="0" />
        <folder8 id="8" parentID="0" />
    </root>
</FOLDERS>



我有一个SQL DB文件夹"表,该表具有三列[FolderID] [ParentFolderID] [FolderName].目前,这里仅包含测试数据.表格如下:



I have a SQL DB "folders" table that has three columns [FolderID] [ParentFolderID] [FolderName]. At the moment there is just test data in there. So the table looks like:

[FolderID][ParentFolderID][FolderName]
    0            -1          root
    1             0          Folder1
    2             0          Folder2
    3             0          Folder3
    4             3          Folder4
    5             3          Folder5
    6             5          Folder6
    7             0          Folder7
    8             0          Folder8



该表将随着添加和删除文件夹而定期更改.

在我的php文件中,我在php中有一个文件夹类,如下所示:



This table is going to be changed regularly as folders are added and removed.

In my php file I have a folder class in php that looks like:

private $isRoot = false;
private $pID    = &amp;quot;Parent Folder&amp;quot;;
private $fID    = &amp;quot;Folder ID&amp;quot;;
private $fn     = &amp;quot;Folder Name&amp;quot;;



带有用于设置和检索每个变量的函数.

我通过使用循环来提取SQL数据并将其存储在文件夹对象数组中:



with functions to set and retrieve each of the variables.

I am pulling the SQL data by using a loop and storing it in an array of folder objects:

$con = connect();

    $query = "SELECT * FROM structure.folders ORDER BY `ParentFolderID`";

    if ($stmt = $con->prepare($query))
    {
        $stmt->execute();
        $stmt->bind_result($fID, $pID, $folderName);

        while ($stmt->fetch())
        {
            $root = $pID == -1;

            $folderArray[$fID] = $pID;
            $folder = new folder($root,$pID,$fID,$folderName);
            $folderObjects[$fID] = $folder;
        }
        $stmt->close();
    }
    disconnect($con);



然后,我使用foreach循环遍历$ folderObjects数组,并使用xml编写器写入xml文件:



Then I loop through the $folderObjects array with a foreach loop and write the xml file with an xml writer:

foreach ($folderObjects as $folder)
{
    $root = $folder->isRoot();
    $folderName = $folder->getName();
    $pID = $folder->getParent();
    $fID = $folder->getID();
    if ($root)
    {
        $xml->startElement("FOLDERS"); // start FOLDERS
            $xml->startElement($folderName); // start of root folder element
                $xml->writeAttribute("pid", $pID); // add attributes
                $xml->writeAttribute("id", $fID); // add attributes
    continue;
    }
    $xml->writeElement($folderName);
        $xml->writeAttribute("pid", $pID); // add attributes
        $xml->writeAttribute("id", $fID); // add attributes
}
$xml->endElement(); // end root element
$xml->endDocument(); // end xml document




这将产生一个如下所示的XML文件:




This will produce an XML file that looks like:

<FOLDERS>
    <root id="0" parentID="-1" />
    <Folder1 id="1" parentID="0">
    <Folder2 id="2" parentID="0" />
    <Folder3 id="3" parentID="0" />
    <Folder4 id="4" parentID="3" />
    <Folder5 id="5" parentID="3" />
    <Folder6 id="6" parentID="5" />
    <Folder7 id="7" parentID="0" />
    <Folder8 id="8" parentID="0" />
</FOLDERS>



而不是上面的示例,其中每个文件夹都在其父文件夹下列出.

好的,这就是我想要做的和得到的.

我认为我的问题出在遍历文件夹对象数组的循环上,但是我很难理解如何遍历数组检查每个文件夹对象的父对象并将其写入父对象下的xml文件.


再次感谢您发表的冗长文章,并在此先感谢您的任何帮助和建议.



instead of the example from above where each folder is listed under it''s parent folder.

Ok, so that is what I am trying to do and what I am getting.

I think my problem is with the loop that goes through the array of folder objects, but I am having trouble getting my head around how to loop through the array checking each folder object for its parent and writing it to the xml file under its parent.


Once again, sorry for the long post and thank you in advance for any help and suggestions.

推荐答案

isRoot = false; 私人
isRoot = false; private


pID =& amp;父文件夹& amp; quot ;; 私人
pID = &amp;quot;Parent Folder&amp;quot;; private


fID =& amp;文件夹ID& amp;"; 私人
fID = &amp;quot;Folder ID&amp;quot;; private


这篇关于从SQL DB写入XML的PHP​​文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆