2014-10-16 50 views
0

請檢查此代碼以解決問題。搜索數據並將數據保存在字段中

  1. 當我按名稱或年齡搜索數據時,我會在頁面的前10行中得到結果,但是當我移動到下一頁以查看結果時,它會返回到所有結果。我怎樣才能去下一頁沒有我輸入的名字或年齡沒有我的數據沒有刪除 或我在其中搜索的網頁停留。
  2. 當我搜索我有年齡空列問題的所有數據,如果年齡爲空並沒有出現我需要看到所有的數據,如果是空或不是,我進入int類型年齡

<table class="tab"> 
 
     <thead> 
 
\t <tr> 
 
\t <th colspan="5" class="table-title"> 
 
\t <h3>Search</h3> 
 
\t </th> 
 
\t </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"> 
 
\t </td> 
 
    <td class="tdd"> 
 
\t <input type="submit" name="submit" value="Search"> 
 
\t </td> 
 
    </tbody> 
 
    </table> 
 
    </form> 
 
    <br> 
 
    </body> 
 
<?php 
 
\t if (!empty($_POST['name'])){ 
 
\t $name=$_POST['name']; 
 
\t } 
 
\t if (!empty($_POST['age'])){ 
 
\t  $age=$_POST['age']; 
 
\t } 
 

 
$con=mysqli_connect("localhost","name","password","db"); 
 
$ch = 'SET CHARACTER SET utf8'; 
 
\t 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 
 
\t \t \t \t \t \t \t \t \t full_name LIKE '%$name%' AND 
 
\t \t \t \t \t \t \t \t \t 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 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t full_name LIKE '%$name%' AND 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t age LIKE '%$age%' 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t ORDER BY ID ASC LIMIT $start, $limit"); 
 
    
 
echo'<table class="table-fill">'; 
 
\t 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)) 
 
\t { 
 

 
\t echo '<tr class="data">'; 
 
    \t echo '<td class="data">'; 
 
\t echo $x->ID; 
 
\t echo "</td>"; 
 
    \t echo '<td class="data">'; 
 
\t echo $x->full_name; 
 
\t echo "</td>"; 
 
    \t echo '<td class="data">'; 
 
\t echo $x->status; 
 
\t echo "</td>"; 
 
\t 
 
    \t echo "</tr>"; 
 
\t \t } 
 
\t \t print "</table>"; 
 
mysqli_close($con); 
 
?> 
 
<?=$pagination ?>

回答

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將b直到會話結束(通常在瀏覽器關閉時)。

相關問題