请查看此代码以解决问题。
<table class="tab">
<thead>
<tr>
<th colspan="5" class="table-title">
<h3>Search</h3>
</th>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="searchform">
<tr class="trr">
<th class="thh">
Full Name
</th>
<th class="thh">
Age
</th>
</tr>
</thead>
<tbody>
<tr class="trr">
<td class="tdd">
<input type="text" autocomplete="off" style="width:150px;" name="name">
</td>
<td class="tdd">
<input type="text" autocomplete="off" style="width:50px;" name="age">
</td>
<td class="tdd">
<input type="submit" name="submit" value="Search">
</td>
</tbody>
</table>
</form>
<br>
</body>
<?php
if (!empty($_POST['name'])){
$name=$_POST['name'];
}
if (!empty($_POST['age'])){
$age=$_POST['age'];
}
$con=mysqli_connect("localhost","name","password","db");
$ch = 'SET CHARACTER SET utf8';
mysqli_query($con,$ch);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$tbl_name="table"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$resul = mysqli_query($con,"SELECT ID,full_name,age FROM arrested WHERE
full_name LIKE '%$name%' AND
age LIKE '%$age%'");
$num_rows = mysqli_num_rows($resul);
$total_pages =$num_rows;
/* Setup vars for query. */
$targetpage = "detainees.php"; //your file name (the name of this file)
$limit = 10;
//how many items to show per page
if(isset($_GET['page']))
{$page = $_GET['page'];}
else{$page =0;}
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">Previous</a>";
else
$pagination.= "<span class=\"disabled\">Previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "<b class='dot'> . . . </b>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "<b class='dot'> . . . </b>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "<b class='dot'> . . . </b>";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "<b class='dot'> . . . </b>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">Next</a>";
else
$pagination.= "<span class=\"disabled\">Next</span>";
$pagination.= "</div>\n";
}
/* Get data. */
$resul = mysqli_query($con,"SELECT ID,full_name,age FROM arrested WHERE
full_name LIKE '%$name%' AND
age LIKE '%$age%'
ORDER BY ID ASC LIMIT $start, $limit");
echo'<table class="table-fill">';
echo'<thead><tr class="data">
<th class="text-left">ID</th>
<th class="text-left">Full Name</th>
<th class="text-left">Age</th>
</tr></thead>
<tbody class="table-hover">';
while($x= mysqli_fetch_object($resul))
{
echo '<tr class="data">';
echo '<td class="data">';
echo $x->ID;
echo "</td>";
echo '<td class="data">';
echo $x->full_name;
echo "</td>";
echo '<td class="data">';
echo $x->status;
echo "</td>";
echo "</tr>";
}
print "</table>";
mysqli_close($con);
?>
<?=$pagination ?>
答案 0 :(得分:0)
使用会话($ _SESSION),如下例所示:
<?php
session_start();
if (isset($_POST['var']) && !empty($_POST['var']))
{
$var = $_POST['var'];
$_SESSION['var'] = $var;
}
elseif (isset($_SESSION['var']))
{
$var = $_SESSION['var'];
}
?>
$ _SESSION中设置的变量将一直保留到会话结束(通常是在浏览器关闭时)。