Page 1 of 1

problem with page links in pagination

Posted: Wed Oct 08, 2008 10:12 pm
by cali_dotcom
hi, i just wrote a program for my website with a search function. everything works fine but i have a problem with the links to the other result pages. this is the code

Code: Select all

 
<?php
 
$errors = array();
if ($_POST)
{
 
    $mysql = mysql_connect("localhost", "root", "") or die("couldn't connect to server");
    $db = mysql_select_db("jobpage", $mysql) or die("couldn't connect to database");
 
    $keyword = $_REQUEST["keyword"];
    $place = $_REQUEST["place"];
    $cat_id = $_REQUEST["cat_id"];
    $limit = $_REQUEST['limit'];
    $page = $_REQUEST['page'];
 
    //set defaults for $limits, $page
    if (!($limit) || (is_numeric($limit) == "false") || ($limit < 10) || ($limit > 50)) {
        $limit = 10;
    }
    
    if (!($page) || (is_numeric($page) == "false") || ($page > $numrows) || ($page <= 0)) {
        $page = 1;
    }
    
        // set the lower limit where the records would start to display
    
    $start_limit = $page * $limit - $limit;
    //build search query first against only the keyword--
    $search_db = "SELECT * FROM jobs WHERE job_desc like \"%$keyword%\"";
    $search_res = mysql_query($search_db) or die(mysql_error());
 
    $numrows = mysql_num_rows($search_res);
    if ($numrows == 0) die ("no matches met your criteria");
 
    // set the total number of pages required 
    $total_pages = ceil($numrows/$limit);
       
    $search_db = "SELECT * FROM jobs WHERE job_desc like \"%$keyword%\" LIMIT $start_limit, $limit";
    $search_res = mysql_query($search_db) or die(mysql_error()); 
    
    // display what the person searched for
    $display .=" <p>You searched for: "" . $keyword . ""</p>";
 
    // begin to show results set
    $b = $start_limit + $limit;
    $start_limit++;
    if ($numrows < $limit){
        $display .= "<p>Showing results $start_limit to $numrows of $numrows</p>";
    }
    else{
        $display .= "<p>Showing results $start_limit to $b of $numrows</p>";
 
    }
    //first build the top part of the table
    $display .= "<TABLE BORDER = '1'>";
    $display .= "<TR>";
    $display .= "<TH>Job Title</TH><TH>Company</TH><TH>Description</TH><TH>Pay</TH><TH>Location</TH><TH>Date</TH>";
    $display .= "</TR>";
    // now you can display the results returned
    while ($row = mysql_fetch_array($search_res))
    {
        $title = $row["job_title"];
        $desc = $row["job_desc"];
        $qua = $row["job_qua"];
        $pay = $row["job_pay"];
        $req = $row["job_req"];
        $date = $row["post_date"];
        $comp = $row["comp_name"];
        $loc = $row["job_loc"];
 
 
        $display .= "<TR>";
        $display .= "<TD> $title</TD><TD> $comp </TD><TD> $desc </TD><TD> $pay </TD><TD> $loc </TD><TD> $date </TD>";
        $display .= "</TR>";
        //$count++;// total number of records shown which will be the set_limit, starting point of our limit clause
    }
    $display .= "</TABLE>";
    
    $previous_page = $page -1;
    if ($previous_page >=1){
        $display .= "<b><<</b> <a href=".$_SERVER["PHP_SELF"]."?keyword=$keyword&limit=$limit&page=$prev_page><b>Prev.</b></a>";
        
    }
    
    //loop through other pages
    for ($a = 1; $a <= $total_pages; $a++){
        if ($a == $page){
            $display .= "<b>$a | </b>";
        }
        else {
            $display .= "<a href='".$_SERVER["PHP_SELF"]."?keyword=$keyword&limit=$limit&page=$a'> $a </a> |";
        }
    }
    
    
    
    $next_page = $page +1;
    if ($next_page <=$total_pages){
        $display .= "<a href=".$_SERVER["PHP_SELF"]."?keyword=$keyword&limit=$limit&page=$next_page><b>Next</b></a> > >";
    }
 
}
else {
    header("location: sbdjobs.html");
}
 
 
?>
note: one lines 82, 92 and 100 where i put the links to the other pages, i also tried: href='job_search.php'?keyword.......
the idea is that when one clicks on the links, a new query would be built so the variables required for the query are passed in the link.

if anybody knows what's wrong, please help me out
thanks

ALSO: does anyone know how i can modify this code to show details when a user click on the results

Re: problem with page links in pagination

Posted: Thu Oct 09, 2008 4:33 am
by aceconcepts
Can you send me a url?

If not what is actually the problem?

Also, you can write the url variables like this:

Code: Select all

 
$display .= '<b>&laquo;</b> <a href="' . $_SERVER["PHP_SELF"] . '?keyword=' . $keyword .'&limit=' . $limit . '&page=' . $prev_page . '"><b>Prev.</b></a>';
 
 
Single quotes and double quotes are entirely different - which helps. You were using double quotes within double quotes which often makes things tricky - you'd basically have to escape each instance e.g. <a href=\"...\">

Let me know if you have any other problems :D

Re: problem with page links in pagination

Posted: Thu Oct 09, 2008 5:53 pm
by cali_dotcom
thanx ace,
but the problem still persists. the problem is with the links on lines 82, 92 and 100.
the links do appear but when they are clicked on, and i already corrected them as suggested, the variables are not passed on to the script and the script is not run when the next, prev or other page number links are clicked on.hence the results shown remain the same.
i really dont know what's wrong, i'm losing my mind on this.

Re: problem with page links in pagination

Posted: Fri Oct 10, 2008 5:05 am
by aceconcepts
Change links to:

Code: Select all

 
$display .= '<b>&laquo;</b> <a href="?keyword=' . echo $keyword .'&limit=' . echo $limit . '&page=' . echo $prev_page . '"><b>Prev.</b></a>';
 

Re: problem with page links in pagination

Posted: Sat Oct 11, 2008 7:32 pm
by cali_dotcom
thanx man,

the problem was $page and $limit were always set to the default of 1 an 10 respectively because the conditions is_numeric($page) and is_numeric($limit) was always returning false. i put these conditions in just incase someone typed in the values in the url as security measure. as soon as i took them out all was fine. do you know any way i could block values that have been type directly into the url.

thanks..

Re: problem with page links in pagination

Posted: Sat Oct 11, 2008 7:43 pm
by aceconcepts
You could try:

Code: Select all

 
if(is_int($value) && $value>0)
{
//$value must equal a whole number
}
else
{
//invalid
}
 
Hope this works :D