2013-10-31 110 views
4

我有一個搜索表單,我想分頁,但是當我按分頁搜索的下一個按鈕或第二頁,所有的結果搞砸了,沒有數據被取出來自查詢。我不知道確切的問題可能是,但是這是我的搜索代碼:分頁搜索php

include ('paginate.php'); //include of paginat page 

$per_page = 5; 
$find = $_POST['find']; 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find);  // number of results to show per page 
$result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%$find%'"); 
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results/$per_page);//total pages we going to have 

//------------if page is setcheck-----------------// 
if (isset($_GET['page'])) { 
    $show_page = $_GET['page'];    //it will telles the current page 
    if ($show_page > 0 && $show_page <= $total_pages) { 
     $start = ($show_page - 1) * $per_page; 
     $end = $start + $per_page; 
    } else { 
     // error - show first set of results 
     $start = 1;    
     $end = $per_page; 
    } 
} else { 
    // if page isn't set, show first set of results 
    $start = 0; 
    $end = $per_page; 
    $show_page=1; 
} 
// display pagination 
if(isset($_GET['page'])){ 
    $page = intval($_GET['page']); 
}else{ 
    $page =1; 
} 


$tpages=$total_pages; 
if ($page <= 0) 
    $page = 1; 
?> 
<?php 
        $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages; 
        echo '<div class="pagination"><ul>'; 
        if ($total_pages > 1) { 
         echo paginate($reload, $show_page, $total_pages); 
        } 
        echo "</ul></div>"; 
        // display data in table 
        echo "<table class='table table-bordered'>"; 
        echo "<thead><tr><th>Project</th> <th>Country</th> <th>Active</th> 
       </tr></thead>"; 
        for ($i = $start; $i < $end; $i++) { 
         // make sure that PHP doesn't try to show results that don't exist 
         if ($i == $total_results) { 
          break; 
         } 
        ?><form name="frmactive" method="POST" action=""> 
        <input type="hidden" name="id" value="<?php echo mysql_result($result, $i, 'p_id');?>" /> 
         <?php 
         // echo out the contents of each row into a table 
         echo '<tr><td>' . mysql_result($result, $i, 'p_name') . '</td>'; 
         echo '<td>' . mysql_result($result, $i, 'p_country') . '</td>'; 
         if (mysql_result($result, $i, 'p_isActive')=='1'){ 
         echo '<td>Active</td>'; 
         echo '<td align="center"><a href="activeproject.php?id=' . mysql_result($result, $i, 'p_id') . '">Edit</a></td>'; 
} 
else{ 
    echo'<td>Inactive</td> '; 
    echo '<td align="center"><a href="activeproject.php?id=' . mysql_result($result, $i, 'p_id') . '">Edit</a></td>'; 
         echo "</tr>"; 
        }  
        // close table> 
        } 
       echo "</table>"; 
      // pagination 
      ?> 

,這是paginate.php

function paginate($reload, $page, $tpages) { 
    $adjacents = 2; 
    $prevlabel = "&lsaquo; Prev"; 
    $nextlabel = "Next &rsaquo;"; 
    $out = ""; 
    // previous 
    if ($page == 1) { 
     $out.= "<span>" . $prevlabel . "</span>\n"; 
    } elseif ($page == 2) { 
     $out.= "<li><a href=\"" . $reload . "\">" . $prevlabel . "</a>\n</li>"; 
    } else { 
     $out.= "<li><a href=\"" . $reload . "&amp;page=" . ($page - 1) . "\">" . $prevlabel . "</a>\n</li>"; 
    } 

    $pmin = ($page > $adjacents) ? ($page - $adjacents) : 1; 
    $pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages; 
    for ($i = $pmin; $i <= $pmax; $i++) { 
     if ($i == $page) { 
      $out.= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n"; 
     } elseif ($i == 1) { 
      $out.= "<li><a href=\"" . $reload . "\">" . $i . "</a>\n</li>"; 
     } else { 
      $out.= "<li><a href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n</li>"; 
     } 
    } 

    if ($page < ($tpages - $adjacents)) { 
     $out.= "<a style='font-size:11px' href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n"; 
    } 
    // next 
    if ($page < $tpages) { 
     $out.= "<li><a href=\"" . $reload . "&amp;page=" . ($page + 1) . "\">" . $nextlabel . "</a>\n</li>"; 
    } else { 
     $out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n"; 
    } 
    $out.= ""; 
    return $out; 
} 

可否請你指出其中的錯誤正在發生?感謝您的時間

+0

使用極限。 –

+1

我以前使用過限制但我有同樣的問題 –

回答

0

試試這個(注意意見請):在查詢

<?php 
/* 
Assuming that your search form has a POST method set to it, 
be sure to do a redirect to this page and send the find parameter gotten 
from the posted form urlencoded 

ie path_to_my_file?find='.urlencode(trim($_POST['find'])) 
*/ 
include ('paginate.php'); //include of paginat page 

$per_page = 5; 
$find = strip_tags(strtoupper(trim(urldecode($_GET['find'])))); 
$page = intval($_GET['page']); 

$result_total = mysql_query("SELECT COUNT(*) AS total FROM projects WHERE p_name LIKE '%".$find."%'"); 
$tmp_total = mysql_fetch_assoc($results_total); 

$totalpages = ceil($tmp_total['total']/$per_page); 

if ($totalpages != 0 && $page > $totalpages) { 
     $page = $totalpages; 
} else if ($page < 1) { 
     $page = 1; 
} 

$offset = ($page - 1) * $per_page; 

$result = mysql_query("SELECT * FROM projects WHERE p_name LIKE '%".$find."%' LIMIT ".$offset.", ".$per_page); 

$show_page = 1; 
//You need to pass the phrase you are searching for in every link in order for the pagination to work (or user Sessions to save the keyword somewhere if you don't want it in the link) 
$reload = $reload = 'http://localhost/Personal/Stackoverflow/2/index.php?find='.urlencode($find); 

echo ' <div class="pagination"> 
      <ul>'; 

if ($totalpages > 1) { 
    echo paginate($reload, $show_page, $totalpages); 
} 

echo ' </ul> 
      </div>'; 

// display data in table 
if(mysql_num_rows($result) > 0) { 
     echo ' <table class="table table-bordered"> 
       <thead> 
       <tr> 
        <th>Project</th> 
        <th>Country</th> 
        <th>Active</th> 
       </tr> 
       </thead>'; 

     while($row = mysql_fetch_assoc($result)) { 
      echo ' <tr> 
          <td>'.$row['p_name'].'</td> 
          <td>'.$row['p_country'].'</td> 
          <td>'.((intval($row['p_isActive']) == 1) ? 'Active' : 'Inactive').'</td> 
          <td align="center"> 
           <a href="activeproject.php?id='.$row['p_id'].'">Edit</a> 
          </td> 
        </tr>'; 
     } 

     echo ' </table>'; 
} 
?>