【发布时间】:2015-11-09 00:28:59
【问题描述】:
您好,我这里有一个代码,它将搜索名称然后返回分页结果。我在第 19 行和第 20 行遇到错误
注意:未定义索引: 在第 19 行的 C:\xampp\htdocs\eventreg\trypaginate2.php 中提交 - 现在可以
和
注意:未定义索引: 在第 20 行的 C:\xampp\htdocs\eventreg\trypaginate2.php 中搜索 - 现在可以
但它正在工作。当我转到下一页时,它不会显示任何结果,只是一个空白页。
这是我的html代码:
<html>
<head>
<title>Title of your search engine</title>
</head>
<body>
<form action='trypaginate2.php' method='POST'>
<center>
<h1>My Search Engine</h1>
<input type='text' size='90' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>
然后这里是php代码:
<?php
if(array_key_exists('search', $_GET))
{
echo "hello";
$search = $_GET['search'];
echo "$search";
}
if(isset($_POST['submit']))
{
if(array_key_exists('search', $_POST))
{
echo "hi";
$search = $_POST['search'];
}
}
if(strlen($search)<=1)
echo "Search term too short";
else
{
echo "You searched for <b>$search</b> <hr size='1'></br>";
include("dbcon.php");
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="name LIKE '%$search_each%'";
else
$construct .="AND name LIKE '%$search_each%'";
}
echo $construct;
$constructs ="SELECT * FROM reginformation WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";
$per_page = 5;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM reginformation WHERE $construct LIMIT $start, $per_page");
/*while($runrows = mysql_fetch_assoc($getquery))
{
$title = $runrows ['regID'];
$desc = $runrows ['name'];
$url = $runrows ['address'];
echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";
}*/
if(mysql_num_rows($getquery)>0)
{
while ($row = mysql_fetch_assoc($getquery)) {
// echo data
echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr><br>";
} // end while
}
else
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('No record found.');
window.location.href='reglistresp.php';
</SCRIPT>");
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start)
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else
{
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='trypaginate2.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";
}
echo "</center>";
}
}
?>
编辑! 更改了我的 php 代码现在我在第 34 行遇到错误。错误是
注意:未定义变量:在 C:\xampp\htdocs\ 中搜索eventreg\trypaginate2.php 第 34 行
【问题讨论】:
-
$button = $_GET['submit'];和$search = $_GET ['search'];应该是$button = $_POST['submit'];和$search = $_POST['search']; -
那么如果我进入下一页,我应该如何在不使用 GET 的情况下获取数据?
标签: php pagination