全站查询和分页显示——在线留言板2

来源:互联网 发布:怎么看台湾网站知乎 编辑:程序博客网 时间:2024/05/16 14:31

      全站搜索这里利用的技术是先把数据库的所有内容都导出,然后用字符串匹配的技术找到相应的数据存到一个数组里面去。

      还是像往常一样,自己就不要说些乱七八糟的了,直接上代码:

search模块:

<!--search.php:站内搜索函数---------------------><?php    require_once("sys_conf.inc");    /**************************************************    /*功能:查询content字段包含$keyword的所有留言记录    /*输入:查询关键字    /*输出:查询数组    /**************************************************/    function search($keyword,$content)     {         $count=count($content);         $j=0;         $ArrSearch=array(); //结果数组        for ($i=0;$i<$count;$i++)         {            if (ereg($keyword,$content[$i]))//判断cotent[$i]中是否含有$keyword关键字,如果有,则条件成立            {                 //把关键字用红颜色突出显示,并放入结果数组                $ArrSearch[$j]=str_replace($keyword,"<font color=red><b>$keyword</b></font>",$content[$i]);                $j++;             }         }         return $ArrSearch;     }    /**************************************************    /*功能:查询content字段包含$keyword的所有留言记录的rid    /*输入:查询关键字    /*输出:rid数组    /**************************************************/    function searchid($keyword,$rid,$content)     {         $count=count($content);         $j=0;         $Arrid=array(); //结果数组        for ($i=0;$i<$count;$i++)         {             if (ereg($keyword,$content[$i]))//判断cotent[$i]中是否含有$keyword关键字,如果有,则条件成立            {                 $Arrid[$j]=$rid[$i];                $j++;             }         }         return $Arrid;     }    //初始化    $one_page_line=10;     //每页的最大记录数    $content=array();      //数组,包含所有记录的content属性内容    $id=array();               //数组,包含所有记录的rid属性内容    $ArrSearch=array();    //数组,包含查询结果    $Arrid=array();            //数组,包含查询结果记录的编号    //查询数据库,获取$content值    $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);              mysql_select_db($DBNAME);    $str="select rid,content from guestbook;";    $result=mysql_query($str);    //循环将数据库中值写入数组    $i=0;    while(list($rid,$con)=mysql_fetch_row($result))    {        $content[$i]=$con;        //print($con);        $id[$i]=$rid;        $i++;    }    mysql_close($link_id);      //调用search()函数,查询关键字    if (isset($_POST["search"]) and isset($_POST["keyword"]) and $_POST["keyword"]!="")     {         $ArrSearch=search($_POST["keyword"],$content);         $Arrid=searchid($_POST["keyword"],$id,$content);    }    //输出全部数据    else    {        //print_r($content);        $ArrSearch=$content;        //print_r($ArrSearch);        $Arrid=$id;     }    //print_r($ArrSearch);    //print_r($Arrid);    $count =count($ArrSearch);//echo ($count);?>

显示模块:这里的话主要是有一些细小的知识点,需要自己来仔细研究一下。

<!--booklist.php:用户留言列表----------------------------><?php include "search.php"?><html>    <head>        <title>站内查询</title>    </head>    <body>        <?php include  "head.html"?>        <table width="68%" border="5px;">            <tr>             <td>                        <?php            $one_page_line=5;            $int_page_count=$count;    //总条数;             $int_page_num=ceil($int_page_count/$one_page_line);//总页数;//            echo $int_page_num;            echo "<font color=#CC33FF>分页:</font>";            for ($i=1;$i<=$int_page_num;$i++)             {                 if (!isset($_POST["search"]) and !isset($_POST["keyword"]))                    echo "<a href=booklist.php?page=$i>".$i."</a>&nbsp;";            }                 echo "</font>";                 if (isset($_POST["search"]) and isset($_POST["keyword"]) and $_POST["keyword"]!="")                 {                     echo "<br><center>";                    echo "<a href=booklist.php?search=".$_POST["search"]."&keyword=".$_POST["keyword"]."&page=$i>".$i."</a>&nbsp;";                    echo "下面的留言中包含关键字<font color=red><b>".$_POST["keyword"]."</b></font>共<font color=red>".$count."</font>条</center>";                }             ?>             </td>            <td><p align=right>共有<font color=red><?echo "$count"?></font></p></td>             </tr>        </table>         <table width="68%" border="0" align="center">         <?        if (!isset($_GET['page']))            $page=1;        else            $page=$_GET['page'];        $text="";        $begin_line=$int_page_count-($page-1)*$one_page_line;         if ($begin_line<$one_page_line)            $one_page_line=$begin_line;         for ($j=$begin_line;$j>($begin_line-$one_page_line);$j--)         {             echo "<tr><td align=right colspan=2><a href=replylist.php?&recordid=".$Arrid[$j-1].">查看回复</a>&nbsp;<a href=reply.php?task=reply&recordid=".$Arrid[$j-1].">回复</a>&nbsp;<a href=update.php?recordid=".$Arrid[$j-1].">编辑</a>&nbsp;<a href=delete.php?recordid=".$Arrid[$j-1].">删除</a>&nbsp;第<font color=red>$j</font>条</td></tr>";             print_r ($ArrSearch[$j-1]);        }        ?>         </table>        <p align=center><a href="add.php">插入留言</a></p>        <p align=center><a href="#" onclick=history.back()>返  回</a></p>        </body></html>
0 0
原创粉丝点击