2011-06-16 63 views
0

首先 - PHP noob所以請不要太多地放在我的代碼。我更多是一個編輯器而不是完整的開發...使用selectbox過濾結果,然後分頁(問題)

我有一個分頁結果問題,我使用selectbox進行篩選。客戶希望能夠選擇職位,以便顯示與選擇相關的職位空缺。我有這個默認設置來顯示數據庫中的所有作業。然後當用戶選擇一個選項時,頁面上的內容應該改變。該頁面應該限制爲每頁5個作業。

我有這部分工作。當用戶選擇一個選項時,我已經包含了該作業的相關代碼,該代碼根據選擇從數據庫中選擇作業。

這是vancancies.php

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<select name="jobtype" onchange="if(this.options.selectedIndex>0) window.location.href = 'vacancies.php?option='+this.options [this.options.selectedIndex].value"> 
    <option value="">Select a Job Title</option> 
    <option value="support">Support Workers</option> 
    <option value="care">Carers</option> 
    <option value="nurse">Nurses</option> 
    <option value="all">All</option> 

</select> 
</form> 
<?php 
$_GET['option']; 
$jobtype = $_GET['option']; 
    if ($jobtype == "") 
    include("alljobs.php"); 
    elseif ($jobtype == "support") 
    include("supportjobs.php"); 
    elseif ($jobtype == "care") 
    include("carerjobs.php"); 
    elseif ($jobtype == "nurse") 
    include("nursejobs.php"); 
?> 

根據什麼選擇的是我選擇了包括文件,該文件會從DB獲得的相關數據。也許不理想,但它的工作原理。

我遇到的問題是當選擇「支持工作人員」時,有15個結果。前5個顯示在第1頁上,但轉到第2頁後顯示第2頁的所有結果。

這是我的代碼supportjobs.php,這是我在選擇支持時加入的。

<?PHP 

include('xxxx.php'); 

    $tableName="jobs"; 
    $targetpage = "vacancies.php?option=support"; 
    $limit = 5; 

    $query = "SELECT COUNT(*) as num FROM jobs WHERE title LIKE '%" . $jobtype . "%'"; 
    $total_pages = mysql_fetch_array(mysql_query($query)); 
    $total_pages = $total_pages[num]; 

    $stages = 3; 
    $page = mysql_escape_string($_GET['page']); 
    if($page){ 
     $start = ($page - 1) * $limit; 
    }else{ 
     $start = 0; 
     } 

//-query the database table 
//$sql="SELECT * FROM jobs WHERE title LIKE '%" . $search . "%'"; 

    // Get page data 
    $query1 = "SELECT * FROM jobs WHERE title LIKE '%" . $jobtype . "%' ORDER BY id DESC 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)) // Not enough pages to breaking it up 
     { 
      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)) // Enough pages to hide a few? 
     { 

// Beginning only hide later pages 
      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>"; 
      } 

// Middle hide some front and some back 
      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>"; 
      } 

      // End only hide early pages 
      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 "<p>We have <span class='highlight'>$total_pages</span> vacancies currently available.<p>"; 
// pagination 
echo $paginate; 

?> 

<?php 

     function makeClickableLinks($makeemail) { 
     $makeemail = eregi_replace('([_\.0-9a-z-][email protected]([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', 
      '<a href="mailto:\\1">\\1</a>', $makeemail); 
     return $makeemail; 
     } 


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

?> 
    <a name="<?PHP echo '' . $row["ID"] . '';?>"></a> 
    <h4><?PHP echo '' . $row["title"] . '';?></h4> 
    <?PHP $makeemail = nl2br($row["description"]); ?> 
    <p><?PHP echo makeClickableLinks($makeemail);?></p> 
    <p><span class="highlight">Location:</span> <?PHP echo '' . $row["location"] . '';?> 
    <br> 
    <!--<p><?PHP echo '' . $row["duration"] . '';?></p>--> 
    <span class="highlight">Salary:</span> <?PHP echo '' . $row["salary"] . '';?></p> 
    <hr/> 
<?PHP 
} 
echo $paginate; 
mysql_close($DB); 

?> 

我有點不確定如何得到這個工作。我已經嘗試在目標網址中使用硬編碼'vacancies.php?option = support'來查看是否會轉到支持結果的第2頁,但它什麼也沒有返回。

任何輸入將不勝感激。非常感謝。

+0

首先,你的代碼似乎是容易受到SQL incection - >'LIKE '% 「$ jobtype。」 %'' – roberkules 2011-06-16 14:13:44

+0

好不容易纔弄明白。或者至少讓它工作。我只需要改變?在分頁鏈接到&例如'$ targetpage **&** page = $ counter。不過,任何反饋意見都可以幫助我學習更好的實踐。非常感謝。 – mrgrill 2011-06-16 14:44:38

+0

好的。謝謝。我該如何解決SQL注入的問題? – mrgrill 2011-06-16 14:47:05

回答

0

嘗試改變這些變量:

$start = (($page - 1) * $limit) + 1; // 1, 6, 11.. 

    $lastpage = $page * $limit; // 5, 10, 15..