我在文件中有一系列选择框:
<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个单独的&#34;车道&#34; (这是为了学生排队)。当用户更改任何这些框中的选择并点击提交时,它需要更新&#34; lane&#34;在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更新为&#34;已关闭&#34;,每当用户打开表单时,默认情况下该通道需要设置为关闭。
答案 0 :(得分: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 - &gt;数组函数,可以在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>
这会回答你的问题吗?