【发布时间】:2017-02-14 11:05:19
【问题描述】:
这是我的 xml 文件和下面的 php 代码。我已经输入了一个输入类型,它将按名字搜索学生。然后将出现有关特定学生的信息,并会出现另一个按钮更新。
问题是我想在那之后修改信息。如何通过标签名称获取元素,以便修改特定学生的信息?
<students>
<student>
<firstname>John</firstname>
<lasttname>Snow</lasttname>
<student_id>160600</student_id>
<gender>male</gender>
<dob>23-06-95</dob>
<email>JohnSnow@gmail.com</email>
<mobilenumber>57675060</mobilenumber>
<address>albatros, portlouis</address>
<cohort>BSE15PT</cohort>
<programme>Software Engineering</programme>
<mode>PT</mode>
</student>
<student>
<firstname>Jey</firstname>
<lastname>Lacroix</lastname>
<student_id>150501</student_id>
<gender>M</gender>
<dob>1990-02-22</dob>
<email>Jey@hotmail.com</email>
<mobilenumber>57553536</mobilenumber>
<address>Curepipe</address>
<cohort>BSE15AFT</cohort>
<programme>software engineering</programme>
<mode>FT</mode>
</student>
</students>
<?php
if(isset($_POST['search']))
{
$xml=simplexml_load_file("studentInstance.xml") or die("Error: Cannot Create Object");
//query the document
$name = $_POST['studentname'];
//$xml = simplexml_load_string($xml);
$query = $xml->xpath("/students/student[firstname = '$name']");
$array=$query;
//echo "<pre>";
//rint_r($array);
//echo "</pre>";
$count=0;
$size=count($array);
//echo $count;
echo "<center>";
while($count!=count($array)){
foreach ($array[$count]->children() as $child) {//stores values in child
$getElementTag=$child->getName();//get tag so nom
echo '<label>'.$getElementTag.'</label>'." ";
echo '<input type="text" value= " '.$child.' " size="30"></intput>';
echo "<br>";
echo "<br>";
}
$count++;
}
echo '<input type="submit" name="modify" value="Update Record">'.'<br>';
echo "***************************";
echo "</center>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Searching</title>
</head>
<body>
<center>
<form method="POST" action="searchtest.php">
<label>Enter Student Name</label>
<input type="text" name="studentname" pattern="[A-Z][a-z]+" title="Must start with capital letters!" required><br>
<br>
<input type="submit" name="search" value="search">
</form>
</center>
</body>
</html>
【问题讨论】:
标签: php xml xpath xml-parsing