xpath模糊查询

Posted on Posted in php
Tips: 本文创建于2014年5月8日,已超过 2 年,内容或图片可能已经失效!

xpath 模糊查询

其他文件与 xml增删改查?相同,以下是改动文件

list.php

[code lang="php"]
<?php
require './studentModel.class.php';
$studentModel = new studentModel ();
if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
$sn = $_POST ['sn'];
$students = $studentModel->search ( $sn );
} else {
$students = $studentModel->getList ();
}
?>
<a href="add.php">添加</a>
<form method="post" action="list.php">
学号:<input type="text" name="sn"><br /> <input type="submit" value="查询">
</form>

<table border='1'>
<tr>
<th>ID</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
<?php foreach ($students as $student):?>
<tr>
<td><?php echo $student->id?></td>
<td><?php echo $student->sn?></td>
<td><?php echo $student->name?></td>
<td><?php echo $student->sex?></td>
<td><?php echo $student->age?></td>
<td><a href="remove.php?id=<?php echo $student->id?>">删除</a> <a
href="add.php?id=<?php echo $student->id?>">编辑</a></td>
</tr>
<?php endforeach;?>
</table>
[/code]

class文件,在类文件中添加

[code lang="php"]
/**
* 根据sn的值进行模糊查询
*
* @param $sn 传
*/
public function search($sn) {
$students = array ();
// 先创建dom树
$dom = new DOMDocument ( '1.0', 'utf-8' );
$dom->load ( './students.xml' );
$xpath = new DOMXPath ( $dom );
$studentNodeList = $xpath->query ( "/students/student[contains(@sn,'$sn')]" );
foreach ( $studentNodeList as $studentNode ) {
$student = $this->element2object ( $studentNode );
$students [] = $student;
}
return $students;
}
[/code]

» 转载请注明来源:若我若鱼 » xpath模糊查询

Leave a Reply

Your email address will not be published. Required fields are marked *

fourteen + one =