2013-10-15 182 views
0

我使用分頁腳本查看搜索結果, 但這個當我點擊下一頁時打破了結果,因爲沒有在另一頁上傳遞變量。分頁和搜索引擎

我在頁面「index.php」中創建搜索表單。 和「search.php」查看結果。

請修改我的代碼! 非常感謝

(對不起,壞的壞的英語不好(我不使用翻譯))

「的index.php」

<div id="main" style="width: 1024px; margin: 0 auto; background: #eee;border: solid 1px #000"> 

    <img style="margin:30px 300px;" alt="logoAifa" src="https://www.agenziafarmaco.gov.it/areariservata/sites/all/themes/genesis/genesis_AIFA/img/logo.png"/> 
    <a style="font-family: Times;font-size: 24px; font-weight: bold; margin: 30px 390px">Asset Management</a> 
    <form style="margin: 30px 30px 30px 30px" method="get" action="php/search.php"> 
     <table> 
      <tr> 
       <td><input type="text" placeholder="Cerca" name="cerca"></td><td><input type="submit" value="go"></td> 
       <td><a style="font-family: Times;font-size:14px;color: blue;text-decoration: underline;">Ricerca Avanzata</a></td> 

      </tr> 
     </table> 


    </form> 

    <div id="db"> 


     <?php 
    include('php/conn.php');  
    session_start(); 
etc... 

「的search.php」

<?php 
    include('conn.php');  
    session_start(); 
    $cerca = $_REQUEST['cerca']; 
    $_SESSION['cerca'] = $cerca; 






$cerca = mysql_escape_string(stripslashes($cerca)); 



$tableName="asset";  
    $targetpage = "search.php";  
    $limit = 50; 

    $query = "SELECT COUNT(*) as num FROM $tableName WHERE Nome LIKE '%$cerca%' OR Cognome LIKE '%$cerca%' OR Utilizzo LIKE '%$cerca%' OR IDAIFA LIKE '%$cerca%' OR Tipologia LIKE '%$cerca%' OR Modello LIKE '%$cerca%' OR Stato LIKE '%$cerca%' "; 
    $total_pages = mysql_fetch_array(mysql_query($query)); 
    $total_pages = $total_pages['num']; 

    $stages = 3; 

    $page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 0; 
    if($page){ 
     $start = ($page - 1) * $limit; 
    }else{ 
     $start = 0; 
     } 

    // Get page data 
    $query1 = "SELECT * FROM $tableName WHERE Nome LIKE '%$cerca%' OR Cognome LIKE '%$cerca%' OR Utilizzo LIKE '%$cerca%' OR IDAIFA LIKE '%$cerca%' OR Tipologia LIKE '%$cerca%' OR Modello LIKE '%$cerca%' OR Stato LIKE '%$cerca%' 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)) 
     { 
      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)) 
     { 

      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>";  
      } 

      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>";  
      } 

      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>";  


} 
echo $total_pages.' Results'; 
// pagination 

?> 

<?php 
echo "<table border='1' style='margin:0 auto;'>"; 

echo "<tr>", 
"<td colspan='3'> Azioni </td> <td> Nome </td> <td> Cognome </td> <td> IDAIFA </td> <td> Utilizzo</td> <td> Tipologia</td> <td>Marca</td> <td>Modello</td> <td>Stato</td>", 
"</tr> \n"; 


     while($dati = mysql_fetch_array($result)) 
     { 

     echo "<tr>"; 
echo "<td> <a href='detailasset.php?ID=$dati[ID]'><img src='../images/eye.png'></a> </td>"; 
echo "<td> <a href='modasset.php?ID=$dati[ID]'><img src='../images/edit.png'></a> </a> </td>"; 
echo "<td> <a href='deleteasset.php?ID=$dati[ID]?confirm=true' class='confirm'> <img src='../images/delete.png'></a></td>"; 

print "<td> $dati[Nome] </td>"; 
print "<td> $dati[Cognome] </td>"; 
print "<td> $dati[IDAIFA] </td>"; 
print "<td> $dati[Utilizzo] </td>"; 
print "<td> $dati[Tipologia] </td>"; 
print "<td> $dati[Marca] </td>"; 
print "<td> $dati[Modello] </td>"; 
print "<td> $dati[Stato] </td>"; 

echo "</tr> \n"; 

     } 

    echo "</table></div>\n"; 
    echo $paginate; 
    ?> 

</div> 

回答

1

我想你想說的是,搜索字詞沒有正確傳遞?

從我在代碼中可以看到的,變量$cerca是搜索項。

你可以使用的$_GET['cerca']代替$_REQUEST

而且更重要的是,我不認爲你可以在查詢做到這一點:

$query = "SELECT COUNT(*) as num FROM $tableName WHERE Nome LIKE '%$cerca%' 

你應該分裂了LIKE部分,如本

$query = "SELECT COUNT(*) as num FROM $tableName WHERE Nome LIKE '%" . $cerca . "%' 

這樣做你所做的所有不同的時間LIKE

或者我認爲這是你問的是什麼...

+0

現在我用$ _GET [「CERCA」]而不是$ _REQUEST,但同樣的錯誤,而不是在第2頁工作,例如 – Dabidx88

+0

你嘗試把用額外的引號(「)查詢? LIKE'%「。$ cerca。」%' – Pompey

+0

我把額外的引號的查詢,但不工作!沒有什麼變化 – Dabidx88