php下一页空白搜索结果

时间:2014-05-07 16:48:44

标签: php search pagination

我的默认加载页面可以使用next和prev结果..但是对于搜索结果,我只能获得第一页然后空白用于下一页。假设总搜索结果行为9,它显示3页的下一个链接。但当我点击下一个链接时,它在页面2上没有查询结果。这是我的代码:

echo '<table><tr><td>';
echo '<form method="post" name="frmSearch" action="mypage.php>
Search by name 
<input type="text" name="txtSearch">
<input type="submit" name="submit" value="Search">
</form>';

$per_page = 5; 
$page = 1;

if (isset($_GET['page'])) 
{
$page = intval($_GET['page']); 
if($page < 1) $page = 1;
}

$start_from = ($page - 1) * $per_page; 
$Prev_Page = $page - 1;
$Next_Page = $page + 1;

if (!$_POST){
//$sql = "SELECT * FROM mytable LIMIT $start_from, $per_page";
//$totalr = mysql_query("SELECT COUNT(*) FROM mytable");
//$totalr = mysql_fetch_row($totalr);
//$totalr = $totalr[0];
//$total_pages = $totalr / $per_page;
//$total_pages = ceil($total_pages);

//$listresult = mysql_query($sql);
//$total = mysql_num_rows($listresult);
} 
else {
     if ($_POST['txtSearch']!="") {$cond1 = " name LIKE      
'%".$_POST['txtSearch']."%' ";} else {$cond1 = 1; }

$sql = "SELECT * FROM mytable WHERE ".$cond1." LIMIT $start_from, $per_page";

$totalr = mysql_query("SELECT COUNT(*) FROM mytable WHERE name LIKE   
'%".$_POST['txtSearch']."%' ");
$totalr = mysql_fetch_row($totalr);
$totalr = $totalr[0];

$total_pages = $totalr / $per_page;
$total_pages = ceil($total_pages); 

$listresult = mysql_query($sql);
$total = mysql_num_rows($listresult);
}

if($total == 0){
echo "<center>No records found.</center>";
}

echo "<span style='float:right;margin-top:8px;'>[ <a href='new.php'>Add New</a>    
]</span></td></tr>";
echo "<table>

<tr>
<th>ID</th>
<th>Name</th>
</tr>
";

while ($_POST = mysql_fetch_assoc($listresult)){ 

echo "<tr>";
echo "<td>" . $_POST['id'] . "</td>";
echo "<td>" . $_POST['name'] . "</td>";
echo "</tr>";
}

echo "</table>";
echo "</table>";

echo $totalr." result";
echo "<br>";

if ($Prev_Page) {

echo " <a href ='{$_SERVER['PHP_SELF']}?page=$Prev_Page'><< Prev</a> ";
}

if ($totalr > $per_page) {
for($i = 1; $i  <= $total_pages; ++$i)
{
echo "<a href='{$_SERVER['PHP_SELF']}?page=$i'>$i</a> &nbsp;| &nbsp;";
}
}

if ($page!=$total_pages) {
echo " <a href='{$_SERVER['PHP_SELF']}?page=$Next_Page'>Next >></a> ";        
}

1 个答案:

答案 0 :(得分:0)

也许你需要使用SELECT COUNT(*)FROM mytable WHERE name LIKE'%$ search%'...记得逃避$ _POST ['txtSearch']