搜索数据并将数据保存在字段中


search data and keep data in Field

请查看此代码以解决问题。

  1. 当我按姓名或年龄搜索数据时,我会在页面的前10行中得到结果,但当我转到下一页查看结果时,它会返回到所有结果。我如何在没有我的数据的情况下转到下一页?我输入的数据是姓名或年龄而没有删除或者我在里面搜索的页面
  2. 当我搜索所有数据时,如果age为null,我在age中的null列没有出现问题,我需要查看所有数据,如果它为null或否,我输入类型int表示age

<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 ?>

使用会话($_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中设置的变量将一直保留到会话结束(通常是在浏览器关闭时)。