今日php实现显示数据分页,竟遇到类似如下问题
Notice: Undefined index: content in D:\Apache2.2\htdocs\pagefenye.php on line 18
琢磨半天,终得解决。
问题原因:因有post参数,点击诸如“下一页”等时,参数传递己变为get,而非post。
解决方案:step1 使用$_REQUEST代替$_POST,$_REQUEST既可用于POST方法传递来的参数
也可用于GET方法传递的参数
step2 对form的post参数在换页时要重新传递
例子:
form:
<form method="post" action="searchresult.php">
<table class="core" style="text-indent: 2em;">
<tbody>
<tr> <td> <p><input type="radio" name="extent" value="category">By category. <select style="font-family:Tahoma; font-size:12px; width:250px;" name="ca tegory"> <option value="Aero Animal">Aero Animal</option> <option value="Aero Fungi">Aero Fungi</option> <option value="Aero Insect">Aero Insect</option> <option value="Aero Mite">Aero Mite</option> <option value="Aero Plant">Aero Plant</option> <option value="Contact">Contact</option> <option value="Food Animal">Food Animal</option> <option value="Food Fungi">Food Fungi</option> <option value="Food Plant">Food Plant</option> <option value="Gliadin">Gliadin</option> <option value="Protozoan">Protozoan</option> <option value="Venom/Salivary">Venom/Salivary</option> <option value="Worm">Worm</option> </select> </p> <p><input type="radio" name="extent" value="species">By species. <select style="font-family:Tahoma; font-size:12px; width:250px;" name="sp ecies"> <option value="other">Other</option> <option value="Actinidia">Actinidia</option> <option value="Alternata">Alternata</option> <option value="Anisakis simplex">Anisakis simplex</option> <option value="Aspergillus">Aspergillus</option> <option value="maize">Maize</option> <option value="Actinidia">Actinidia</option> <option value="Alternata">Alternata</option> <option value="Anisakis simplex">Anisakis simplex</option> <option value="Aspergillus">Aspergillus</option> <option value="Buckwheat">Buckwheat</option> <option value="Carpinus">Carpinus</option> <option value="cattle">Cattle</option> <option value="Cedar">Cedar</option> <option value="Chironomus">Chironomus</option> <option value="Cockroach">Cockroach</option> <option value="Grass">Grass</option> <option value="Hazelnut">Hazelnut</option> <option value="Hevea brasiliensis">Hevea brasiliensis</option> <option value="Maize">Maize</option> <option value="Malus">Malus</option> <option value="Mite">Mite</option> <option value="Olive">Olive</option> <option value="Peanut">Peanut</option> <option value="Pendula">Pendula</option> <option value="Potato">Potato</option> <option value="Roundworm">Roundworm</option> <option value="Soybean">Soybean</option> <option value="Tomato">Tomato</option> <option value="Tropicalis">Tropicalis</option> <option value="Wheat">Wheat</option> </select> </p> <p><input type="radio" name="extent" value="all" checked="checked">List all.</p> </td> </tr> <tr> <td> <input name="B1" type="submit" value="Search" /> <input name="B2" type="reset" value="Reset" /> <input name="absolutepage" type="hidden" value=""/> </td> </tr> </tbody> </table> </form> searchresult.php部分代码:注意蓝色标出部分 <?php $data=$_REQUEST['extent']; $data1=$data; $absolutepage=0; $data2=""; $data3=""; //echo $data; if ($data=="category"){ $data=$_REQUEST['category']; $data2=$data; $db=mysql_connect("localhost","root"); mysql_select_db("test",$db); $result=mysql_query("select * from allergendata where Category='$data'", $db); $pagesize=10; $totalNum=mysql_num_rows($result); $pageCount=(int)(($totalNum-1)/$pagesize)+1; $absolutepage=$_REQUEST['absolutepage']; (!$absolutepage)?($absolutepage=1):$absolutepage; ($absolutepage<=$pageCount)?$absolutepage:($absolutepage= $pageCount); $f_pageNum=$pagesize*($absolutepage-1); $result=mysql_query("select *from allergendata where Category='$data' limit $f_pageNum,$pagesize",$db); $i=$f_pageNum+1; //echo $data; }elseif($data=="species"){ $data=$_REQUEST['species']; $data3=$data; $db=mysql_connect("localhost","root"); mysql_select_db("test",$db); $result=mysql_query("select * from allergendata where Species= '$data'",$db); $pagesize=10; $totalNum=mysql_num_rows($result); $pageCount=(int)(($totalNum-1)/$pagesize)+1; $absolutepage=$_REQUEST['absolutepage']; (!$absolutepage)?($absolutepage=1):$absolutepage; ($absolutepage<=$pageCount)?$absolutepage:($absolutepage= $pageCount); $f_pageNum=$pagesize*($absolutepage-1); $result=mysql_query("select *from allergendata where Species='$data' limit $f_pageNum,$pagesize",$db); $i=$f_pageNum+1; //echo $data; }else{ $db=mysql_connect("localhost","root"); mysql_select_db("test",$db); $result=mysql_query("select * from allergendata",$db); $pagesize=10; $totalNum=mysql_num_rows($result); $pageCount=(int)(($totalNum-1)/$pagesize)+1; $absolutepage=$_REQUEST['absolutepage']; (!$absolutepage)?($absolutepage=1):$absolutepage; ($absolutepage<=$pageCount)?$absolutepage:($absolutepage= $pageCount); $f_pageNum=$pagesize*($absolutepage-1); $result=mysql_query("select *from allergendata limit $f_pageNum, $pagesize",$db); $i=$f_pageNum+1; } echo "<table boder=1 cellspacing=0 cellpadding=15>\n"; echo "<tr align='center'><td>Total records:</td><td>$totalNum</td> </tr>"; echo "</table>\n"; echo "<table boder=1 cellspacing=0 cellpadding=15>\n"; echo "<tr align='center' bgcolor=gray><td>No.</td><td>Name</td> <td>UniPro Acc</td><td>Taxonomic Name</td> <td>Common Name</td><td>Category</td> <td>Species</td><td>Epitope</td><td>Sugar</td> <td>Description</td></tr>\n"; //$i=1; while($myrow=mysql_fetch_row($result)){ //echo $myrow[10]; printf("<tr align='center'><td>%d</td><td><a href='$myrow[10]' target=_blank>%s</a></td> <td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td> <td>%s</td> <td>%s</td><td>%s</td></tr>\n",$i,$myrow[1],$myrow[2], $myrow[3],$myrow[4] ,$myrow[5],$myrow[6],$myrow[7], $myrow[8],$myrow[9]); $i++; } $p=($absolutepage>1)?($absolutepage-1):1; //echo $p; $q=($absolutepage<=($pageCount-1))?($absolutepage+1):$absolutepage; //echo $q; echo "<tr><td>Records:</td><td>$totalNum</td><td>Page:</td> <td>$absolutepage/$pageCount</td> <td><a href='?extent=$data1&category=$data2&species= $data3&absolutepage=$p'> previous</a></td> <td><a href='?extent=$data1&category=$data2&species= $data3&absolutepage=$q'>next</a> </td></tr>"; mysql_free_result($result); mysql_close($db); echo "</table>\n"; ?>