0

问题:

我有一个包含 13 个 xml 文件的 zip 文件。除第一个文件外,每个文件都包含一个测验问题和几个答案。当我将文件上传到下面的脚本时,它会正确打印出问题和答案,但不是按顺序打印的。每次我上传 zip 文件时,问题的顺序都会随机变化。

问题:

有没有办法可以利用文件名告诉脚本每次都按顺序打印出问题?

压缩文件包含:

  • imsmanifest.xml
  • item101008.xml
  • item101009.xml
  • item101010.xml
  • item101011.xml
  • item101012.xml
  • item101013.xml
  • item101014.xml
  • item101015.xml
  • item101016.xml
  • item101017.xml
  • item101018.xml
  • item101019.xml

PHP脚本(警告,长脚本):

<?php
    //Initialize counter
    $i = 0;

    //Go through each file
    while (($file = readdir($dh)) !== false) 
    {
        //Skipt the first loop
        if ($i > 1)
        {
            //Ignore misc file      
            if ($file != 'imsmanifest.xml')
            {
                //Create new DOM document
                $dom= new DOMDocument();

                //Load XML file
                $dom->load('uploads/' . $file);

                //Do not preserve white space
                $dom->preserveWhiteSpace = false;

                //Check if correct answers should be displayed
                if (isset($_POST['XMLAnswers']))
                {
                    //Get correct answer
                    $correct = $dom->getElementsByTagName( "correctResponse" )->item(0)->nodeValue;

                    //Get question
                    $questions = $dom->getElementsByTagName('p')->item(0)->nodeValue;

                    //Print out question
                    echo '<h4>' . htmlspecialchars($questions) . '</h4>' . "\n";

                    //Get answers
                    $domTable = $dom->getElementsByTagName("simpleChoice");

                    //Loop through each answer
                    foreach ($domTable as $answers)
                    {
                        //Delete potential unnecessary tags
                        $pattern = array(
                                            '<p xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1">', 
                                            '</p>'
                                    );

                        //Check if answer is correct one
                        if ($correct == $answers->getAttribute('identifier'))
                        {
                            //Print out correct answer
                            echo '<span style="color:red;">' . utf8_decode(str_replace($pattern, '', innerHTML($answers))) . '</span><br />' . "\n";
                        }
                        else
                        {
                            //Print out answer
                            echo utf8_decode(str_replace($pattern, '', innerHTML($answers))) . '<br />' . "\n";
                        }
                    } 
                }
            }
        }
        //Increment counter
        $i++;
    }
?>
4

1 回答 1

2

与其使用 opendir/readdir,不如使用 scandir。请参阅http://www.php.net/manual/en/function.scandir.php。scandir 会为您返回目录中已排序的文件数组。除了用以下内容替换外部 while readdir 循环之外,您应该能够将其放入并让您的代码以最少的更改工作:

$files = scandir($dir);
foreach ($files as $file) {
   //your current code
}
于 2013-09-11T09:24:43.163 回答