开发者

Using selectbox to filter results and then paginate (problem)

First off - PHP noob so please don't lay into my code too much. I'm more an editor rather than full on dev...

I have an issue with paginating results which I filter using a selectbox. The client wants to be able to select a job title so that the vacancies that are displayed relevant to the selection. I have this set up by default to show all jobs in the DB. Then when the user chooses an option the content on the page should change. The page should limit to 5 jobs per page.

I have this part working. When a user selects an option I have then included the relevant code for that job which selects the jobs from the database based on the selection.

This is in 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");
?>

Depending on what is selected I have chosen to include a file which will get the relevant data from the DB. Maybe not ideal, but it works.

The problem I have is when 'Support Workers' is selected the are 15 results. The first 5 are shown on page one, but going to page 2 it goes to page two of the ALL results.

This is my code for supportjobs.php which is I include when support is selected.

<?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 $m开发者_C百科akeemail; 
       } 


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

?>

I'm a bit unsure how to get this working. I have tried hard coding 'vacancies.php?option=support' into the target URL to see if this will then go to page 2 of the support results, but it returns nothing.

Any input would be greatly appreciated. Many thanks.


Try changing these variables:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜