gpt4 book ai didi

php - 使用 php 和 mysql 显示带分页的搜索结果

转载 作者:行者123 更新时间:2023-11-29 01:38:50 24 4
gpt4 key购买 nike

我有一个搜索文本框。如果我搜索任何术语,例如:PHP、html它将显示结果。

现在我需要的是像 1 2 3 4 5 这样的分页页面显示

我在上面浪费了 3 天,我自己尝试了很多但没有用我不知道我错了吗

没有错误出现没有下一页显示前 2 个结果的记录

需要在下一页上获取下一个结果意味着 page2 , 3 就像结果的末尾说的最后一页一样。请帮助我..我的代码在下面

<?php
require('../connect.php');
$rec_limit = 2;
if (isset($_GET['search']))
{
$searchtext= $_GET['searchtext'];
}
$sql = "SELECT * from jobpost WHERE jobtitle LIKE '%$searchtext%' LIMIT 2";
mysqli_select_db($con, 'login');
$result= mysqli_query($con, $sql);
if(!$result)
{
echo "error";
}
if(mysqli_num_rows($result) >0){
echo "Your Search Term : $searchtext ";
echo"<br>";
while($row = $result->fetch_assoc())
{

echo "<table>";
echo "<tr> <td>Job Title:</td> <td>{$row['jobtitle']} </td>
</tr>".
"<tr> <td>Job Description: </td> <td>
{$row['jobdescription']}</td> </tr> ".
"<br>";
echo "</table>";

}
}
else{
echo "Your Search Term : $searchtext ";
echo "<br>";
echo "no results found";
}
// pagination
$row = mysqli_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$data = mysqli_query($con, "SELECT * FROM jobpost WHERE jobtitle LIKE
'%$searchtext%' LIMIT $offset, $rec_limit") or die(mysqli_error());
if(mysqli_num_rows($data) >0){
while($row = mysqli_fetch_array($data))
{
/*echo "<table>";
echo "<tr > <td>Job Title:</td> <td>{$row['jobtitle']} </td>
</tr>".
"<tr > <td>Job Description: </td> <td>
{$row['jobdescription']}</td> </tr> ".
"<br>";
echo "</table>"; */
}
}

else{
echo"<a href='search.php?searchtext=$searchtext&search=search'>No images
available click here for Home </a>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "<br>";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$last\"
>Last 2 Records</a> |";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$page\"
>Next 2 Records</a>";
}
else if( $page == 0 )
{
echo "<br>";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$page\"
>1</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<br>";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$last\"
>2</a>";
}
mysqli_close($con);
?>

请帮助我..

我只想搜索文本框中的任何输入,并根据需要在分页中显示它。

最佳答案

使用这个分页代码

    include("../connect.php");
$tableName="jobpost";
$targetpage = "view_data.php";
$limit =10;
$_GET['searchtext'];


$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}


// Get page data
$query1 = "SELECT * FROM $tableName WHERE jobtitle LIKE
'%$searchtext%' LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous

if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
}else{
$paginate.= "<span class='disabled'>Previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>Next</a>";
}else{
$paginate.= "<span class='disabled'>Next</span>";
}
$paginate.= "</div>";
}

if(mysqli_num_rows($result) >0){
while($row = mysqli_fetch_array($result))
{
/*echo "<table>";
echo "<tr > <td>Job Title:</td> <td>{$row['jobtitle']} </td>
</tr>".
"<tr > <td>Job Description: </td> <td>
{$row['jobdescription']}</td> </tr> ".
"<br>";
echo "</table>"; */
}
}

在添加分页的地方添加这个

echo "<center>".$paginate."</center>";

添加样式表

.paginate {
font-family:"Times New Roman", Times, serif;
padding: 3px;
margin: 3px;
}
.paginate a {
padding:2px 5px 2px 5px;
margin:2px;
border:1px solid #999;
text-decoration:none;
color: #666;
}
.paginate a:hover, .paginate a:active {
border: 1px solid #999;
color: #0066FF;
}
.paginate span.current {
margin: 2px;
padding: 2px 5px 2px 5px;
border: 1px solid #999;
font-weight: bold;
background-color: #999;
color: #FFF;
}
.paginate span.disabled {
padding:2px 5px 2px 5px;
margin:2px;
border:1px solid #eee;
color:#DDD;
}

关于php - 使用 php 和 mysql 显示带分页的搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com