使用选择框筛选结果,然后分页(问题)

时间:2011-06-16 14:04:33

标签: php pagination drop-down-menu

首先关闭 - PHP noob所以请不要过多地放入我的代码。我更喜欢编辑而不是完全开发...

我在使用精选框过滤分页结果时出现问题。客户希望能够选择职位,以便显示与选择相关的职位空缺。我默认设置此项以显示数据库中的所有作业。然后,当用户选择选项时,页面上的内容应该更改。页面应限制为每页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");
?>

根据所选内容,我选择包含一个文件,该文件将从数据库中获取相关数据。也许不理想,但它的确有效。

我遇到的问题是当选择“支持工作者”时,结果为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-]+@([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'硬编码到目标URL中,看看是否会转到支持结果的第2页,但它什么都不返回。

任何输入都将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试更改这些变量:

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

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