频道栏目
读书频道 > web开发 > php > 深入体验PHP项目开发
2.6 信息搜索模块
2013-04-19 10:10:30     我来说两句
收藏   我要投稿

本文所属图书 > 深入体验PHP项目开发

PHP语言是当今使用最为频繁的网络编程语言之一,一直在Web开发领域占据重要的地位。本书通过10个综合实例的实现过程,详细讲解了PHP语言在实践项目中的综合运用过程。这些项目从作者的学生时代写起,到项目经理结...  立即去当当网订购

2005年11月18日,多云,继续征程

今天同学C开始实现信息搜索模块的编码工作,我认为当信息发布后,搜索引擎就变得十分重要,信息检索是对已存在于数据库中的内容进行搜索,这是最有效的方法。当用户在众多的信息中寻找自己需要的信息时,就需要进行搜索,如图2-9所示,是搜索关键字为“公寓”的信息。


 

下面通过一段代码进行讲解如何实现搜索的过程,其代码(findinfo.php)如下:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/style.css" rel="stylesheet">
<style type="text/css">
<!--
.style12 {color: #FFFFCC}//样式
.style13 {color: #FF8502}
-->
</style>
<table width="780" border="0" align="center" cellpadding="0"
 cellspacing="0">
  <tr valign="top">
    <td colspan="2">
<?php include("top.php");?></td>
  </tr>
  <tr>
    <td width="217" valign="top" background="Images/line2.gif">
<?php include("left.php");?></td>
    <td width="586" valign="top" bgcolor="#FEFEF6">
 <table width="563" height="587" border="0" cellpadding="0"
 cellspacing="0">
      <tr>
        <td width="563" height="587" valign="top" bgcolor="#FFFFFF">
<table width="563" border="0" cellspacing="0" cellpadding="0">
            <tr>
<td width="563" height="27" bgcolor="#3983B6">&nbsp;&nbsp;&nbsp;&nbsp;
<span class="style12"><span class="style13">
</span>&nbsp;&nbsp;<strong>检索结果</strong></span></td>
            </tr>
            <tr>
              <td align="center" valign="top">
    <?php
    include("conn/conn.php");//连接数据库
    $type=$_POST[type];//获取数据类型
    $content=$_POST[content];//获取关键字
   $sql1=mysql_query("select * from tb_leaguerinfo  where checkstate=1
and type='$type' and (content like'%$content%' or title like'%$content%' or
linkman like'%$content%' or tel like'%$content%')");//检索付费的供求信息
    $info1=mysql_fetch_array($sql1);
    $sql=mysql_query("select * from tb_info  where checkstate=1 and type='$type' and (content like'%$content%' or title like'%$content%' or linkman like'%$content%' or tel like'%$content%')");//检索免费的供求信息
    $info=mysql_fetch_array($sql);
    ?>
    <?php
    if($info1){
    do{
      ?>
                  <table width="540" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td height="26">
<?php echo str_ireplace($content,
"<font color='#FF0000'>".$content."</font>",$info1[type]);?> &nbsp;
<-- !对与查询有关的关键字所匹配的信息进行描红-->
<?php echo str_ireplace($content,
"<font color='#FF0000'>".$content."</font>",$info1[title]);?>&nbsp;&nbsp;
<--!对与查询有关的关键字所匹配的时间进行描红-->
<?php echo str_ireplace($content,
"<font color='#FF0000'>".$content."</font>",$info1[edate]);?></td>
<--!对与查询有关的关键字所匹配的信息内容进行描红-->
                    </tr>
                    <tr>
                      <td height="26">&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info1[content]);?></td>
                    </tr>
                    <tr>
                      <td height="26">&nbsp;联系人:
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info1[linkman]);?>
&nbsp;&nbsp;&nbsp;<--!对与查询有关的关键字所匹配的联系人进行描红-->
联系电话:<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info1[tel]);?></td>
<--!对与查询有关的关键字所匹配的联系电话进行描红-->
                    </tr>
                    <tr>
                      <td height="3" background="Images/line1.gif"></td>
                    </tr>
                  </table>
                <?php
    }while($info1=mysql_fetch_array($sql1));
    ?>
    </td>
            </tr>
            <tr>
              <td height="140" align="center" valign="top"> <br>
    <?php
     if($info){
     do{
    ?>
                  <table width="540" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td height="26">『<?php echo str_ireplace($content,"<font color='#FF0000'>".$content."</font>",$info[type]);?>』&nbsp;
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info[title]);?>
&nbsp;&nbsp;<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>", $info[edate]);?></td>
                    </tr>
                    <tr>
                      <td height="26">&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info[content]);?></td>
                    </tr>
                    <tr>
                      <td height="26">&nbsp;联系人:
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info[linkman]);?>
&nbsp;&nbsp;&nbsp;联系电话:<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info[tel]);?></td>
                    </tr>
                    <tr>
                      <td height="3" background="Images/line1.gif"></td>
                    </tr>
                  </table>
                  <?php
     } while($info=mysql_fetch_array($sql));//搜索到了免费信息
     }
    }else{
    ?>
    <?php
     if($info){
     do{
    ?>
                  <table width="540" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td height="26">『<?php echo str_ireplace($content,
"<font color='#FF0000'>".$content."
</font>",$info[type]);?>』&nbsp;
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content. "</font>",$info[title]);?>&nbsp;&nbsp;
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content. "</font>",$info[edate]);?></td>
                    </tr>
                    <tr>
                      <td height="26">&nbsp;&nbsp;&nbsp;&nbsp;
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info[content]);?></td>
                    </tr>
                    <tr>
                      <td height="26">&nbsp;联系人:
<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info[linkman]);?>
&nbsp;&nbsp;&nbsp;联系电话:<?php echo str_ireplace($content,"
<font color='#FF0000'>".$content."</font>",$info[tel]);?></td>
                    </tr>
                    <tr>
                      <td height="3" background="Images/line1.gif"></td>
                    </tr>
                  </table>
                  <?php
     } while($info=mysql_fetch_array($sql));
     }else{
     ?>
              <table width="540" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td align="center">您检索的信息资源不存在!</td
                    </tr>
                  </table>
    <?php
     }    }
      ?>
              </td></tr>
          </table>
            <p>&nbsp;</p>
            <p>&nbsp;</p></td>
      </tr> </table> </td>
  </tr><tr>
    <td colspan="2"><?php include("bottom.php");?></td>
  </tr>
</table>

您对本文章有什么意见或着疑问吗?请到论坛讨论您的关注和建议是我们前行的参考和动力  
上一篇:2.5.3 处理免费信息
下一篇:2.7 后台管理
相关文章
图文推荐
排行
热门
最新书评
特别推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站