首页  >  文章  >  后端开发  >  PHP搜索结果分页显示有关问题。

PHP搜索结果分页显示有关问题。

WBOY
WBOY原创
2016-06-13 12:27:115815浏览

PHP搜索结果分页显示问题。。
首页显示没问题,翻页显示数据没有。。。代码

	include_once("script/conn.php");<br />    class Page{<br />        private $curPage;//当前页面<br />        private $totalPages;//数据总共分多少页显示<br />        private $dispNum;//每页显示的数据条数<br />        private $queryStr;//查询的SQL语句<br />        private $limitStr;//查询语句后面的limit控制语句<br />		private $currentPage;//获取当前页面<br />        public function __construct($queryStr='',$dispNum=3){    <br />            $result = mysql_query($queryStr);<br />            $totalNum = mysql_num_rows($result);<br />            $this->dispNum = $dispNum;<br />			$currentPage = basename ( $_SERVER["PHP_SELF"] );	//返回路径中的文件名部分<br />            $this->totalPages = ceil($totalNum / $dispNum);<br />            $this->queryStr = $queryStr;<br />            $temp = (isset($_GET["curPage"]) ? $_GET["curPage"] : 1);<br />            $this->setCurPage($temp);<br />            $this->showCurPage();<br />            $this->showFoot();<br />        }        <br />        private function showCurPage(){<br />            $this->limitStr = ' LIMIT '.(($this->curPage - 1)* $this->dispNum).','.$this->dispNum;<br />            $result = mysql_query($this->queryStr.$this->limitStr);<br />            while($row = mysql_fetch_assoc($result)){<br />				echo "<img  src=".$row['Image']." / alt="PHP搜索结果分页显示有关问题。" >";<br />				echo "<br/>";<br />            }<br />        }<br />        private function setCurPage($curPage){<br />                if($curPage < 1){<br />                    $curPage = 1;<br />                }<br />                else if($curPage > $this->totalPages){<br />                    $curPage = $this->totalPages;<br />                }<br />                    $this->curPage = $curPage;<br />        }<br />        private function showFoot(){<br />            echo '<a href="?curPage=1">首页</a>丨';<br />            echo '<a href="?curPage='.($this->curPage - 1).'">上一页</a>丨';<br />			echo '<a href="?curPage='.($this->curPage + 1).'">下一页</a>丨';<br />            echo '<a href="?curPage='.$this->totalPages.'">尾页</a>丨';<br />			echo "共有".$this->totalPages."页丨";<br />			echo "当前第 <font color=red>".$this->curPage."</font> 页";<br />        }<br />  }<br />$keyword=$_POST['search'];<br />$sql=mysql_query("select * from didian where name like '%$keyword%'");<br />[email&#160;protected]_fetch_object($sql);<br />if(!$raw){<br />	echo "<font color='red'>您搜索的信息不存在,请使用类似的关键字进行检索!</font>";<br />}
如果单独这样输出是没问题的。。但加上这条的话翻页就没数据显示了..这是搜索框判断条件
switch($keyword){<br />	case '桂林':$pages = new Page('SELECT * FROM `image_guilin`', 3);break;<br />	case '海南':$pages = new Page('SELECT * FROM `image_hainan`', 3);break;<br />	case '北京':$pages = new Page('SELECT * FROM `image_beijing`', 3);break;<br />	case '九寨沟':$pages = new Page('SELECT * FROM `image_jiuzhaigou`', 3);break;<br />	case '太行山':$pages = new Page('SELECT * FROM `image_taihengshan`', 3);break;<br />	case '香格里拉':$pages = new Page('SELECT * FROM `image_xianggelila`', 3);break;<br />	case '小兴安岭':$pages = new Page('SELECT * FROM `image_xiaoxinganling`', 3);break;<br />	case '西双版纳':$pages = new Page('SELECT * FROM `image_xishuangbanna`', 3);break;<br />}

后面是html搜索框跳转
<div id="search_box"><br />		<form id="search_form" method="post" action="search.php"> <br />		<input type="text" id="search" value="城市、景点" onFocus="this.value=''"   style="max-width:90%" name="search"/><br />		<input type="submit" id="s_search" value="搜索" name="Submit" onClick="return check(form)"/><br />		</form> <br />	</div>
不知道为什么这样。。求教。。。
------解决思路----------------------
$keyword=$_POST['search'];
1.你获取参数的名称是search,不是keyword.
2.因为分页是用的是get,所以不能获取。

改成这样就可以了
<br /><br /><?php<br />include_once("script/conn.php");<br />    class Page{<br />        private $curPage;//当前页面<br />        private $totalPages;//数据总共分多少页显示<br />        private $dispNum;//每页显示的数据条数<br />        private $queryStr;//查询的SQL语句<br />        private $limitStr;//查询语句后面的limit控制语句<br />        private $currentPage;//获取当前页面<br />        private $keyword;// 关键字<br />        public function __construct($queryStr='', $keyword='', $dispNum=3){    <br />            $result = mysql_query($queryStr);<br />            $totalNum = mysql_num_rows($result);<br />            $this->dispNum = $dispNum;<br />            $this->keyword = $keyword;<br />            $currentPage = basename ( $_SERVER["PHP_SELF"] );    //返回路径中的文件名部分<br />            $this->totalPages = ceil($totalNum / $dispNum);<br />            $this->queryStr = $queryStr;<br />            $temp = (isset($_GET["curPage"]) ? $_GET["curPage"] : 1);<br />            $this->setCurPage($temp);<br />            $this->showCurPage();<br />            $this->showFoot();<br />        }        <br />        private function showCurPage(){<br />            $this->limitStr = ' LIMIT '.(($this->curPage - 1)* $this->dispNum).','.$this->dispNum;<br />            $result = mysql_query($this->queryStr.$this->limitStr);<br />            while($row = mysql_fetch_assoc($result)){<br />                echo "<img  src=".$row['Image']." / alt="PHP搜索结果分页显示有关问题。" >";<br />                echo "<br/>";<br />            }<br />        }<br />        private function setCurPage($curPage){<br />                if($curPage < 1){<br />                    $curPage = 1;<br />                }<br />                else if($curPage > $this->totalPages){<br />                    $curPage = $this->totalPages;<br />                }<br />                    $this->curPage = $curPage;<br />        }<br />        private function showFoot(){<br />            echo '<a href="?search='.$this->keyword.'&curPage=1">首页</a>丨';<br />            echo '<a href="?search='.$this->keyword.'&curPage='.($this->curPage - 1).'">上一页</a>丨';<br />            echo '<a href="?search='.$this->keyword.'&curPage='.($this->curPage + 1).'">下一页</a>丨';<br />            echo '<a href="?search='.$this->keyword.'&curPage='.$this->totalPages.'">尾页</a>丨';<br />            echo "共有".$this->totalPages."页丨";<br />            echo "当前第 <font color=red>".$this->curPage."</font> 页";<br />        }<br />  }<br />$keyword=$_REQUEST['search'];<br />$sql=mysql_query("select * from didian where name like '%$keyword%'");<br />[email&#160;protected]_fetch_object($sql);<br />if(!$raw){<br />    echo "<font color='red'>您搜索的信息不存在,请使用类似的关键字进行检索!</font>";<br />}<br />?><br /><br />

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn