0

我在这样的文件中有一系列选择框:

<form method="post" action="update.php">

<table>
<tr>
    <td>Lane 1:</td>
    <td>
        <select name="lane1Status">
            <option value="closed">Closed</option>
            <option value="cue">Cue</option>
            <option value="registration">Registration</option>
            <option value="finAid" selected>Financial Aid & Scholarships</option>
            <option value="admisssions">Admissions</option>
        </select>
    </td>
</tr>

有 8 条独立的“通道”(这是学生排队用的)。当用户更改任何这些框中的选择并点击提交时,它需要在 xml 文件中更新该“通道”,如下所示:

<?xml version='1.0'?>
<update>
<Lane1Status>registration</Lane1Status>
<Lane2Status>registration</Lane2Status>
<Lane3Status>registration</Lane2Status>
<Lane4Status>registration</Lane2Status>
<Lane5Status>registration</Lane2Status>
<Lane6Status>registration</Lane2Status>
<Lane7Status>registration</Lane2Status>
<Lane8Status>registration</Lane2Status>
</update>

设置该值后,当用户拉起页面时,它又需要成为该特定通道的默认选择。因此,如果 Lane1Status 更新为“关闭”,则每当用户打开表单时,该通道需要默认设置为关闭。

4

1 回答 1

0

首先,让我们确保我们清楚包含我们数据的 XML 文件。这应该是这样的:

   <?xml  version="1.0" ?>
   <lanes>
       <lane>
           <order>1</order>
           <status>registration</status>
       </lane>
       <lane>
           <order>2</order>
           <status>registration</status>
       </lane>
       <lane>
           <order>3</order>
           <status>registration</status>
       </lane>
       <lane>
           <order>4</order>
           <status>registration</status>
       </lane>
       <lane>
           <order>5</order>
           <status>registration</status>
       </lane>
   </lanes>

接下来,我们需要考虑如何使用 PHP 将这些信息加载到页面上。因此,我们需要知道这个 XML 文件的位置,并且知道如果您移动它或更改文件名,您将需要更改您的 PHP 代码。此外,无需设置单独的页面来处理所有更新。所以,让我们在同一个页面上处理这一切——我们将调用一个文件 myProcess.php

首先,我们需要创建全局变量并检查是否有 POST 数据需要处理:

     <?php
     $file = "myFile.xml";
     $arrXml = array();
     $count = 5; // number of lanes

     if (count($_POST) > 0) {
             // we're returning to the page with
             // form data and need to update our XML
             updateXMLFile();
             unset($_POST);
     }

     // regardless we want to load the XML
     readXMLFile();
     ?>

接下来,我们需要设置我们的功能:

         function readXMLFile() {
         global $file;
         global $arrXml;
         global $count;

         $xmlStr = file_get_contents($file);
         $arrXml = xml2array($xmlStr);
     }

     function updateXMLFile() {
        global $file;
        global $arrXml;
        global $count;

        $string = '<?xml version="1.0" ?><lanes>';
        for ($i = 1; $i <= $count;$i++)
        {
            $string .= "<lane><order>$i</order>";
            $string .= "<status>".$_POST['lane'.$i.'status']."</status></lane>";
        }
        $string .= "</lanes>";

        $string = utf8_encode($string);
        file_put_contents($file, $string);
    }

这是一个很棒的 XML -> 数组函数,可以在 PHP 文档的注释中找到:

    function xml2array($xml){ 
        $opened = array(); 
        $opened[1] = 0; 
        $xml_parser = xml_parser_create(); 
        xml_parse_into_struct($xml_parser, $xml, $xmlarray); 
        $array = array_shift($xmlarray); 
        unset($array["level"]); 
        unset($array["type"]); 
        $arrsize = sizeof($xmlarray); 
        for($j=0;$j<$arrsize;$j++){ 
           $val = $xmlarray[$j]; 
           switch($val["type"]){ 
               case "open": 
                  $opened[$val["level"]]=0; 
               case "complete": 
                  $index = ""; 
                  for($i = 1; $i < ($val["level"]); $i++) 
                      $index .= "[" . $opened[$i] . "]"; 
                  $path = explode('][', substr($index, 1, -1)); 
                  $value = &$array; 
                  foreach($path as $segment) 
                     $value = &$value[$segment]; 
                  $value = $val; 
                  unset($value["level"]); 
                  unset($value["type"]); 
                  if($val["type"] == "complete") 
                    $opened[$val["level"]-1]++; 
                  break; 
              case "close": 
                  $opened[$val["level"]-1]++; 
                  unset($opened[$val["level"]]); 
                  break; 
           } 
       } 
       return $array; 
   } 

现在,我们已经完成了构建表单的所有设置。我假设您知道如何创建 HTML 页面,所以我将只关注表单部分。

    <form name="myForm" method="POST" action"myProcess.php">
    <?php
        for($i = 1;$i <= $count;$i++)
        {
            echo '<label for="lane'.$i.'status">Lane '.$i.': </label>';
            echo '<select name="lane'.$i.'status">';
            if ($arrXml[$i-1][1]['value'] == "closed")
                echo '<option value="closed" selected="selected">Closed</option>';
            else
                echo '<option value="closed">Closed</option>';
            if ($arrXml[$i-1][1]['value'] == "cue")
                echo '<option value="cue" selected="selected">Cue</option>';
            else
                echo '<option value="cue">Cue</option>';
            if ($arrXml[$i-1][1]['value'] == "registration")
                echo '<option value="registration" selected="selected">Registration</option>';
            else
                echo '<option value="registration">Registration</option>';
            if ($arrXml[$i-1][1]['value'] == "finAid")
                echo '<option value="finAid" selected="selected">Financial Aid & Scholarships</option>';
            else
                echo '<option value="finAid">Financial Aid & Scholarships</option>';
            if ($arrXml[$i-1][1]['value'] == "admissions")
                echo '<option value="admissions" selected="selected">Admissions</option>';
            else
                echo '<option value="admissions">Admissions</option>';            
            echo '</select>';
        }
    ?>
    <input type="submit" value="Submit" />
    </form>

因此,将所有这些放在一起,myProcess.php 如下所示:

    <?php
     $file = "myFile.xml";
     $arrXml = array();
     $count = 5; // number of lanes

     if (count($_POST) > 0) {
             // we're returning to the page with
             // form data and need to update our XML
             updateXMLFile();
             unset($_POST);
     }

     // regardless we want to load the XML
     readXMLFile();

     function readXMLFile() {
         global $file;
         global $arrXml;
         global $count;

         $xmlStr = file_get_contents($file);
         $arrXml = xml2array($xmlStr);
     }

     function updateXMLFile() {
        global $file;
        global $arrXml;
        global $count;

        $string = '<?xml version="1.0" ?><lanes>';
        for ($i = 1; $i <= $count;$i++)
        {
            $string .= "<lane><order>$i</order>";
            $string .= "<status>".$_POST['lane'.$i.'status']."</status></lane>";
        }
        $string .= "</lanes>";

        $string = utf8_encode($string);
        file_put_contents($file, $string);
    }

    function xml2array($xml){ 
        $opened = array(); 
        $opened[1] = 0; 
        $xml_parser = xml_parser_create(); 
        xml_parse_into_struct($xml_parser, $xml, $xmlarray); 
        $array = array_shift($xmlarray); 
        unset($array["level"]); 
        unset($array["type"]); 
        $arrsize = sizeof($xmlarray); 
        for($j=0;$j<$arrsize;$j++){ 
           $val = $xmlarray[$j]; 
           switch($val["type"]){ 
               case "open": 
                  $opened[$val["level"]]=0; 
               case "complete": 
                  $index = ""; 
                  for($i = 1; $i < ($val["level"]); $i++) 
                      $index .= "[" . $opened[$i] . "]"; 
                  $path = explode('][', substr($index, 1, -1)); 
                  $value = &$array; 
                  foreach($path as $segment) 
                     $value = &$value[$segment]; 
                  $value = $val; 
                  unset($value["level"]); 
                  unset($value["type"]); 
                  if($val["type"] == "complete") 
                    $opened[$val["level"]-1]++; 
                  break; 
              case "close": 
                  $opened[$val["level"]-1]++; 
                  unset($opened[$val["level"]]); 
                  break; 
           } 
       } 
       return $array; 
    } 

    ?>

    <html>
    <head></head>
    <body>
    <form name="myForm" method="POST" action"myProcess.php">
    <?php
        for($i = 1;$i <= $count;$i++)
        {
            echo '<label for="lane'.$i.'status">Lane '.$i.': </label>';
            echo '<select name="lane'.$i.'status">';
            if ($arrXml[$i-1][1]['value'] == "closed")
                echo '<option value="closed" selected="selected">Closed</option>';
            else
                echo '<option value="closed">Closed</option>';
            if ($arrXml[$i-1][1]['value'] == "cue")
                echo '<option value="cue" selected="selected">Cue</option>';
            else
                echo '<option value="cue">Cue</option>';
            if ($arrXml[$i-1][1]['value'] == "registration")
                echo '<option value="registration" selected="selected">Registration</option>';
            else
                echo '<option value="registration">Registration</option>';
            if ($arrXml[$i-1][1]['value'] == "finAid")
                echo '<option value="finAid" selected="selected">Financial Aid & Scholarships</option>';
            else
                echo '<option value="finAid">Financial Aid & Scholarships</option>';
            if ($arrXml[$i-1][1]['value'] == "admissions")
                echo '<option value="admissions" selected="selected">Admissions</option>';
            else
                echo '<option value="admissions">Admissions</option>';            
            echo '</select>';
        }
    ?>
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>

这回答了你的问题了吗?

于 2012-06-27T22:33:38.480 回答