分页搜索结果

时间:2016-05-20 19:39:38

标签: php pagination

我正在设计一个房地产网站,我的第一页中有一个搜索框,让我的用户可以决定他们想要什么,例如他们可以缩小他们的租房或购买选择或其他东西。我的代码工作得很好,我的分页工作到了均匀分解页面,并计算需要的页数,并且分页的第一页显示没有错误。但是当我点击下一个或点击第X页时我的问题就开始了。当我选择除第1页以外的其他页面时,我没有显示任何结果。我认为php不记得发布的表格。这是我的代码:

的config.php

<?php
$db_host="localhost";
$db_user="root";
$db_pass="";
$db_name="realstate";
$db_table="agahi";
$connect = mysqli_connect("localhost", "root", "", "realstate");
mysqli_query($connect,"SET NAMES 'utf8'");
mysqli_query($connect,"SET character_set_connection='utf8'");
if(mysqli_connect_errno()){
    die("unable to connect".mysqli_connect_errno());
}
$setting = array(
'paged_item' => 2
);
?>

的search.php

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
 <style>
@charset "uft-8";
@import url('reset.css');
body {

font-family: Verdana, Helvetica, Arial, sans-serif;
background-color: #e2edff;
line-height: 125%;
padding: 15px;
font-size=13px;

}
</style>        
<link rel="stylesheet" href="paged_style.css">
</head>
<body>

            <form action="" method="post">    
<select name="valueToSearch1" style="width: 136px;">
    <option value="" disabled selected hidden>choose</option>
    <option name="" value="buy">buy</option>
    <option name="" value="rent">rent</option>
</select>
<div dir="rtl" style="margin-top:10px; style="text-align: right"  >     
    <input type='submit' name='search' value='Search' ></br></br></br>
</div>
            </form>
<?php
include("config.php");
if(isset($_POST['search']))
{
    $valueToSearch1 = $_POST['valueToSearch1'];


@$page = mysql_real_escape_string($_GET['page']);


if(empty($page)){

    $page = 1;
}
$start = ($page - 1) * $setting['paged_item'];

$quer = "SELECT*FROM ".$db_table." WHERE 
 `case` LIKE '%".$valueToSearch1."%' ORDER BY ID ASC LIMIT $start, ".$setting['paged_item']."";
 $query=mysqli_query($connect,$quer)
or die(mysqli_error());
//pagination
$total = "SELECT*FROM ".$db_table." WHERE 
`case` LIKE '%".$valueToSearch1."%'";
$totalll = mysqli_query($connect,$total)
or die(mysqli_error());
$count = mysqli_num_rows($totalll);
echo $count;
if($count - $setting['paged_item'] > 0){
    $paged_total = ceil($count / $setting['paged_item']);
    echo $paged_total;
    $paged_last = $paged_total;
    echo $paged_last;
    $paged_middle = $page + 4;
    $paged_start = $paged_middle - 4;

    if($page > 1){

        $paged_result = '<div class="paged-link"><a href="search.php?page=1" title="first page"</a></div>'."\n";                            
    }
    else{
        $paged_result = '<div class="paged-link-off">first</div>'."\n";                                 
    }

    if($page > 1){
        $paged_perv = $page - 1;
        $paged_result .= '<div class="paged-link"><a href="searchch.php?page='.$paged_perv.'" title="provious page"</a></div>'."\n";
    }
    else{
        $paged_result .= '<div class="paged-link-off">previous</div>'."\n";
    }

    for ($i=$paged_start-2; $i<=$paged_middle; $i++){
        if ($i > 0 && $i <= $paged_last){

            if($i == $page){
                $paged_result .= '<div class="paged-link-selected"><a href="search.php?page='.$i.'" title="page '.$i.'">'.$i.'</a></div>'."\n";
            }

            else{
                $paged_result .= '<div class="paged-link"><a href="search.php?page='.$i.'" title="page '.$i.'">'.$i.'</a></div>'."\n";
            }
        }
    }

    if($page <= $paged_last - 1){
        $paged_next = $page + 1;
        $paged_result .= '<div class="paged-link"><a href="search.php?page='.$paged_next.'" title="next page"</a></div>'."\n";
    }
    else{
        $paged_result .= '<div class="paged-link-off">Next</div>'."\n";
    }

    if($page <= $paged_last - 1){
        $paged_result .= '<div class="paged-link"><a href="search.php?page='.$paged_last.'" title="final page"</a></div>'."\n";
    }
    else{
        $paged_result .= '<div class="paged-link-off">final</div>'."\n";
    }
    $paged_result .= '<div class="paged-link-info">&raquo; page: '.$page.' out of '.$paged_total.'</div>'."\n";

    echo $paged_result; 
}   
}
?>
<table >


      <!-- populate table from mysql database -->
                <?php while($row = mysqli_fetch_array($query)):?>
                <tr>

                            <th> <?php echo $row['case'];?> 

                </tr>
                <?php endwhile;?>
            </table>

    </body>
</html>

2 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>
    <head>
        <title>PHP HTML TABLE DATA SEARCH</title>
 <style>
@charset "uft-8";
@import url('reset.css');
body {
	
font-family: Verdana, Helvetica, Arial, sans-serif;
background-color: #e2edff;
line-height: 125%;
padding: 15px;
font-size=13px;

}

</style>
		
 <link rel="stylesheet" href="paged_style.css">
    </head>
    <body>
	<form action="" method="GET">    
	<select name="valueToSearch1" style="width: 136px;">
    <option value="" disabled selected hidden>choose</option>
    <option name="" value="buy">buy</option>
    <option name="" value="rent">rent</option>
</select>
<div dir="rtl" style="margin-top:10px; style="text-align: right"  >		
            <input type="submit" name="search" value="search"  ><br><br>
            </div>
			</form>
			
<?php
 error_reporting(0);

include("config.php");
if(isset($_GET['search']))
{
    $valueToSearch1 = $_GET['valueToSearch1'];


@$page = mysql_real_escape_string($_GET['page']);


if(empty($page)){
	
	$page = 1;
}
$start = ($page - 1) * $setting['paged_item'];


$quer = "SELECT*FROM ".$db_table." WHERE 
 `case` LIKE '%".$valueToSearch1."%' ORDER BY ID ASC LIMIT $start, ".$setting['paged_item']."";

 $query=mysqli_query($connect,$quer)
or die(mysqli_error());

$total = "SELECT*FROM ".$db_table." WHERE 
`case` LIKE '%".$valueToSearch1."%'";
$totalll = mysqli_query($connect,$total)
or die(mysqli_error());

$count = mysqli_num_rows($totalll);
echo $count;

if($count - $setting['paged_item'] > 0){
	
	$paged_total = ceil($count / $setting['paged_item']);
	echo $paged_total;
	
	$paged_last = $paged_total;
	echo $paged_last;

	$paged_middle = $page + 4;

	$paged_start = $paged_middle - 4;
	
	
	if($page > 1){
		$paged_result = '<div class="paged-link"><a href="search.php?page=1 &valueToSearch1='.$valueToSearch1.'" title="fiest page"</a></div>'."\n";							
	}
	else{
		$paged_result = '<div class="paged-link-off">first</div>'."\n";									
	}
	
	if($page > 1){
		$paged_perv = $page - 1;
		$paged_result .= '<div class="paged-link"><a href="search.php?page='.$paged_perv.'&valueToSearch1='.$valueToSearch1.'" title="previous page"</a></div>'."\n";
	}
	else{
		$paged_result .= '<div class="paged-link-off">pre</div>'."\n";
	}
	
	for ($i=$paged_start-2; $i<=$paged_middle; $i++){
		if ($i > 0 && $i <= $paged_last){
			
			if($i == $page){
				$paged_result .= '<div class="paged-link-selected"><a href="search.php?page='.$i.'&valueToSearch1='.$valueToSearch1.'" title="page '.$i.'">'.$i.'</a></div>'."\n";
			}
			else{
				$paged_result .= '<div class="paged-link"><a href="search.php?page='.$i.'&valueToSearch1='.$valueToSearch1.'" title="page '.$i.'">'.$i.'</a></div>'."\n";
			}
		}
	}
	
	
	if($page <= $paged_last - 1){
		$paged_next = $page + 1;
		$paged_result .= '<div class="paged-link"><a href="search.php?page='.$paged_next.'&valueToSearch1='.$valueToSearch1.'" title="next page"</a></div>'."\n";
	}
	else{
		$paged_result .= '<div class="paged-link-off">next</div>'."\n";
	}
	
	
	if($page <= $paged_last - 1){
		$paged_result .= '<div class="paged-link"><a href="search.php?page='.$paged_last.'&valueToSearch1='.$valueToSearch1.'" title="last page"</a></div>'."\n";
	}
	else{
		$paged_result .= '<div class="paged-link-off">final</div>'."\n";
	}
	$paged_result .= '<div class="paged-link-info">&raquo; page: '.$page.' out of'.$paged_total.'</div>'."\n";

	echo $paged_result;	
}	

}
?>

        
		
            <table >
                

      <!-- populate table from mysql database -->
                <?php while($row = mysqli_fetch_array($query)):?>
                <tr>

						     <th> <?php echo $row['case'];?> </th>
	                     
                </tr>
                <?php endwhile;?>
            </table>

</div>
<?php
$close = mysqli_close($connect);
?>
    </body>
</html>

答案 1 :(得分:0)

您必须将搜索到的文本发送到下一页。您可以按如下方式将参数添加到链接:

$paged_result .= '<div class="paged-link"><a href="search.php?page='.$paged_last.'&valueToSearch1='.$valueToSearch1.'" title="final page"</a></div>'."\n";

将此添加到您拥有的每个网页链接:&valueToSearch1='.$valueToSearch1