整个搜索功能用php的JSON 数据实现
json字符串可以访问https://www.bejson.com/jsoneditoronline/,该网址可以格式化json数据,进行修改。
定义
$s='{"all":[{"academy":"a","name":"信院","content":[{"grade":"1","class":"0","name":"大一","subject":[{"name":"英语","url":""},{"name":"单变量微积分","url":""},{"name":"多变量微积分","url":""},{"name":"线性代数","url":""},{"name":"力学与热学","url":""},{"name":"大学物理基础实验","url":""},{"name":"电路基本理论","url":""},{"name":"电路基本理论实验","url":""},{"name":"计算机程序设计","url":""},{"name":"思想道德基础与法律基础","url":""},{"name":"马克思主义基本原理","url":""},{"name":"数学分析1","url":""},{"name":"数学分析2","url":""}]},{"grade":"2","class":"0","name":"大二","subject":[{"name":"概率论与数理统计","url":""},{"name":"复变函数","url":""},{"name":"电磁学","url":""},{"name":"线性电子线路","url":""},{"name":"数字逻辑电路","url":""},{"name":"大学物理综合实验","url":""},{"name":"线性电子线路实验","url":""},{"name":"数据结构及其算法","url":""},{"name":"中国近现代史纲要","url":""},{"name":"随机过程","url":""},{"name":"光学与原子物理","url":""},{"name":"信号与系统","url":""},{"name":"微机原理与系统","url":""},{"name":"数理方程","url":""},{"name":"数字逻辑电路实验","url":""},{"name":"重要思想概论","url":""}]},{"grade":"3","class":"6","name":"大三","subject":[{"name":"电磁场理论","url":""},{"name":"现代通信原理","url":""},{"name":"信息论A","url":"subject.php?subject=信息论A"}]},{"grade":"3","class":"10","name":"大三","subject":[{"name":"运筹学基础","url":""},{"name":"自动控制原理","url":""}]},{"grade":"3","class":"23","name":"大三","subject":[{"name":"信号与系统实验","url":""},{"name":"非线性电子线路","url":""}]},{"grade":"3","class":"33","name":"大三","subject":[{"name":"英语","url":""},{"name":"单变量微积分","url":""}]},{"grade":"4","class":"6","name":"大四","subject":[{"name":"英语","url":""},{"name":"单变量微积分","url":""}]},{"grade":"4","class":"10","name":"大四","subject":[{"name":"英语","url":""},{"name":"单变量微积分","url":""}]},{"grade":"4","class":"23","name":"大四","subject":[{"name":"英语","url":""},{"name":"单变量微积分","url":""}]},{"grade":"4","class":"33","name":"大四","subject":[{"name":"英语","url":""},{"name":"单变量微积分","url":""}]}]},{"academy":"b","name":"计院","content":[]},{"academy":"c","name":"数院","content":[]}]}';
这个看起来很长,但是用上面的网站编辑会很容易
如图所示
之后
$data=json_decode($s);
就得到PHP变量。
在表单中加入action="s.php" method="post"语句可以上传输入的字符串再跳转到s.php。
之后加入如下代码即可完成搜索(注:表单中input的名字为"s"):
搜索“<?php echo $_POST['s'];?>”的结果:
<ul>
<?php
foreach($data->all as $v_a) //遍历数据
{
foreach($v_a->content as $v_b)
{
foreach($v_b->subject as $v_c)
{
if(strstr($v_c->name,$_POST['s'])) //子串匹配
{
echo '<li><a href="'.$v_c->url.'">'.$v_a->name.'-'.$v_b->name.'-'.$v_c->name.'</a></li>';
}
}
}
}
?>
搜索“信”后结果
可见没有连接数据库依然完成了搜索。