今日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"; ?>