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

<?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: &quot;" . $keyword . "&quot;</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>&lt;&lt;</b> <a href=".$_SERVER["PHP_SELF"]."?keyword=$keyword&amp;limit=$limit&amp;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&amp;limit=$limit&amp;page=$a'> $a </a> |";
        }
    }
    
    
    
    $next_page = $page +1;
    if ($next_page <=$total_pages){
        $display .= "<a href=".$_SERVER["PHP_SELF"]."?keyword=$keyword&amp;limit=$limit&amp;page=$next_page><b>Next</b></a> &gt; &gt;";
    }
 
}
else {
    header("location: sbd.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.

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

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

Recommended Answers

All 5 Replies

the problem is with the links on lines 82, 92 and 100.
the links do appear but when they are clicked on, the variables are not passed on to the script, hence i get a "page not found" page instead of a new query to be built and the script be run again.
i really dont know what's wrong, i'm losing my mind on this.

the problem is with the links on lines 82, 92 and 100.
the links do appear but when they are clicked on, the variables are not passed on to the script, hence i get a "page not found" page instead of a new query to be built and the script be run again.
i really dont know what's wrong, i'm losing my mind on this.

I'm pretty sure you need to replace "&amp;" with "&" in the links

I'm pretty sure you need to replace "&amp;" with "&" in the links

i did just that and now i have a different problem. when you click on the prev, next or the other page links, nothing happens(the page still shows the first result page) although you can see that the url has changed and the page number in the url is now different. that means to me that a new query is not being run. do you know what the problem could be?
here is the new code:

<?php



    $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 = $_GET["keyword"];
    $place = $_REQUEST["place"];
    $cat_id = $_REQUEST["cat_id"];
    $limit = $_GET["limit"];
    $page = $_GET["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());

    //get all columns



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

    
        
    $db = "SELECT * FROM jobs WHERE job_desc like \"%$keyword%\" LIMIT $start_limit, $limit";
    $res = mysql_query($db) or die(mysql_error());

    //get the number of rows to be displayed
    $numrows1 = mysql_num_rows($res);
    	
	
    // display what the person searched for
    $display .=" <p>You searched for: &quot;" . $keyword . "&quot;</p>";

    // begin to show results set
    $b = $start_limit + $limit;
    $start_limit= $start_limit + 1;
	if ($numrows1 < $limit){
		$display .= "<p>Showing results $start_limit to $numrows1 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($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>";
    
    $prev_page = $page -1;
    if ($prev_page >=1){
    	$display .= '<b>&laquo;</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.'"><b>'.$a.'</b></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>&gt; &gt;';
		
	}

	






?>

I think $numrows around line 22 when you are checking the value of $page is not defined so the value of $page will always be 1.

I think $numrows around line 22 when you are checking the value of $page is not defined so the value of $page will always be 1.

thanks rob,
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 this condition 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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.