Jump to content

Help limiting search


cs1h

Recommended Posts

Hi,

 

I have a search result page that is meant to limit the results to 5 per page and then you click next to view the next results (and next for more results and previous for the previous page).

 

The problem is that when you perform the search it works fine but when you want to go to the next page nothing happens. I think the reason is that it looses the search criteria.

 

The script is,

 

<?php
$page = $_REQUEST['page'];
$query = $_POST['query'];

$targetb = $_POST['menuFilesDMA'];
$targetb = str_replace(' ','_', $targetb);

mysql_connect("localhost","xxx","xxx"); 

mysql_select_db("real") or die("Unable to select database"); 

// check to see if $page is set
if (!$page) {
$page = 1;
}

// Change $query to a request super global after the first page.
if($page > 1){
$query = $_REQUEST['query'];
}


//set up some limits
$limit = $page * 5;
$limit = $limit - 5;

//get the count from the database table
$targetb = mysql_real_escape_string($targetb);

$type = mysql_real_escape_string($_POST['Type']);

$keywords = preg_split("/[\s,]+/", trim($_POST['keyword']));
$keys = "";
foreach($keywords as $K)
{
$keys .= "AND (Abstract LIKE '%$K%' OR town LIKE '%$K%')";
}

$sql_num = "SELECT * FROM jobs WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') $keys";
$result = mysql_query($sql_num)or die(mysql_error()); 
$num = mysql_num_rows($result);

// query your database for the final results
$sql_results = "SELECT * FROM jobs WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') $keys ORDER BY id DESC LIMIT $limit,5";
$resulta = mysql_query($sql_results)or die(mysql_error());
  
if($num < 5){
$through = $num;
} else {
$through = $limit + 5;
}
if ($through > $num){
$through = $total;
}

if($page > 1){
$from = $limit +1;
} else {
$from = $limit;
}
if($from == 0){
$from = $from +1;
}

echo "<p align=\"center\">";
if ($page > 1) {
echo "<a
href=\"$PHP_SELF?query=$query&page=".($page -1)."\">Previous</a>  ";
}

if (($num > 5) && (($limit + 5) < $num)) {
echo "<a href=\"$PHP_SELF?query=$query&page=".($page +1)."\">Next</a>";
}

// Build the list from the $sql query above and display results

while($row = mysql_fetch_array($resulta)) {
$Country = $row['country']; 
$Type = $row['type'];
$More = $row['id'];
$Title = $row['Title'];
$Abs = $row['Abstract'];
$Auth = $row['town'];
echo "<p align=\"left\"><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
  <tr>
    <td width=\"14\" height=\"28\" background=\"line_left_corner_top.png\"> </td>
    <td height=\"28\" colspan=\"4\" background=\"line_top.png\"> </td>
    <td height=\"28\" background=\"line_right_corner_top.png\"> </td>
  </tr>
  <tr>
    <td width=\"14\" rowspan=\"3\" background=\"line_left.png\"> </td>
    <td width=\"250\" height=\"21\"><span class=\"style42\">$Title</span></td>
    <td width=\"154\" height=\"21\"> </td>
    <td width=\"14\"> </td>
    <td width=\"128\" height=\"128\" rowspan=\"3\"><img src=\"/searchthumbs/$Country$Type.png\" width=\"128\" height=\"128\" /></td>
    <td width=\"14\" rowspan=\"3\" background=\"line_right.png\"> </td>
  </tr>
  <tr>
    <td height=\"86\" colspan=\"2\"><span class=\"style41\">$Abs</span></td>
    <td width=\"14\"> </td>
  </tr>
  <tr>
    <td width=\"250\" height=\"19\"><span class=\"style43\">$Auth</span></td>
    <td width=\"154\" height=\"19\" align=\"left\" valign=\"bottom\"><span class=\"style39\">>></span> <span class=\"style40\"><a href=more2.php?id=$More>Read More</a> </span> <span class=\"style39\">>></span></td>
    <td width=\"14\"> </td>
  </tr>
  <tr>
    <td width=\"14\" height=\"19\" background=\"line_left_corner.png\"> </td>
    <td height=\"28\" colspan=\"4\" background=\"line_base.png\"> </td>
    <td width=\"14\" height=\"19\" background=\"line_right_corner.png\"> </td>
  </tr>
   <tr>
    <td width=\"14\" height=\"19\"> </td>
    <td height=\"21\" colspan=\"4\"> </td>
    <td width=\"14\" height=\"19\"> </td>
  </tr>
</table><p align=\"center\">";

}

if ($page > 1) {
echo "<a
href=\"$PHP_SELF?query=$query&page=".($page -1)."\">Previous</a>  ";
}

if (($num > 5) && (($limit + 5) < $num)) {
echo "<a href=\"$PHP_SELF?query=$query&page=".($page +1)."\">Next</a>";
}

?>

 

Does anyone know how this could be solved?

 

I will be very thankfull for any help,

 

Cheers,

Colin

Link to comment
https://forums.phpfreaks.com/topic/73525-help-limiting-search/
Share on other sites

pretty sure:

 

<?php
$page = $_GET['page'];
$query = $_POST['query'];

$targetb = $_POST['menuFilesDMA'];
$targetb = str_replace(' ','_', $targetb);

mysql_connect("localhost","xxx","xxx"); 

mysql_select_db("real") or die("Unable to select database"); 

// check to see if $page is set
if (!$page) {
$page = 1;
}

// Change $query to a request super global after the first page.
if($page > 1){
$query = $_GET['query'];
}


//set up some limits
$limit = $page * 5;
$limit = $limit - 5;

//get the count from the database table
$targetb = mysql_real_escape_string($targetb);

$type = mysql_real_escape_string($_POST['Type']);

$keywords = preg_split("/[\s,]+/", trim($_POST['keyword']));
$keys = "";
foreach($keywords as $K)
{
$keys .= "AND (Abstract LIKE '%$K%' OR town LIKE '%$K%')";
}

$sql_num = "SELECT * FROM jobs WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') $keys";
$result = mysql_query($sql_num)or die(mysql_error()); 
$num = mysql_num_rows($result);

// query your database for the final results
$sql_results = "SELECT * FROM jobs WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') $keys ORDER BY id DESC LIMIT $limit,5";
$resulta = mysql_query($sql_results)or die(mysql_error());
  
if($num < 5){
$through = $num;
} else {
$through = $limit + 5;
}
if ($through > $num){
$through = $total;
}

if($page > 1){
$from = $limit +1;
} else {
$from = $limit;
}
if($from == 0){
$from = $from +1;
}

echo "<p align=\"center\">";
if ($page > 1) {
echo "<a
href=\"$PHP_SELF?query=$query&page=".($page -1)."\">Previous</a>  ";
}

if (($num > 5) && (($limit + 5) < $num)) {
echo "<a href=\"$PHP_SELF?query=$query&page=".($page +1)."\">Next</a>";
}

// Build the list from the $sql query above and display results

while($row = mysql_fetch_array($resulta)) {
$Country = $row['country']; 
$Type = $row['type'];
$More = $row['id'];
$Title = $row['Title'];
$Abs = $row['Abstract'];
$Auth = $row['town'];
echo "<p align=\"left\"><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
  <tr>
    <td width=\"14\" height=\"28\" background=\"line_left_corner_top.png\"> </td>
    <td height=\"28\" colspan=\"4\" background=\"line_top.png\"> </td>
    <td height=\"28\" background=\"line_right_corner_top.png\"> </td>
  </tr>
  <tr>
    <td width=\"14\" rowspan=\"3\" background=\"line_left.png\"> </td>
    <td width=\"250\" height=\"21\"><span class=\"style42\">$Title</span></td>
    <td width=\"154\" height=\"21\"> </td>
    <td width=\"14\"> </td>
    <td width=\"128\" height=\"128\" rowspan=\"3\"><img src=\"/searchthumbs/$Country$Type.png\" width=\"128\" height=\"128\" /></td>
    <td width=\"14\" rowspan=\"3\" background=\"line_right.png\"> </td>
  </tr>
  <tr>
    <td height=\"86\" colspan=\"2\"><span class=\"style41\">$Abs</span></td>
    <td width=\"14\"> </td>
  </tr>
  <tr>
    <td width=\"250\" height=\"19\"><span class=\"style43\">$Auth</span></td>
    <td width=\"154\" height=\"19\" align=\"left\" valign=\"bottom\"><span class=\"style39\">>></span> <span class=\"style40\"><a href=more2.php?id=$More>Read More</a> </span> <span class=\"style39\">>></span></td>
    <td width=\"14\"> </td>
  </tr>
  <tr>
    <td width=\"14\" height=\"19\" background=\"line_left_corner.png\"> </td>
    <td height=\"28\" colspan=\"4\" background=\"line_base.png\"> </td>
    <td width=\"14\" height=\"19\" background=\"line_right_corner.png\"> </td>
  </tr>
   <tr>
    <td width=\"14\" height=\"19\"> </td>
    <td height=\"21\" colspan=\"4\"> </td>
    <td width=\"14\" height=\"19\"> </td>
  </tr>
</table><p align=\"center\">";

}

if ($page > 1) {
echo "<a
href=\"$PHP_SELF?query=$query&page=".($page -1)."\">Previous</a>  ";
}

if (($num > 5) && (($limit + 5) < $num)) {
echo "<a href=\"$PHP_SELF?query=$query&page=".($page +1)."\">Next</a>";
}

?>

 

should work, try it.

Link to comment
https://forums.phpfreaks.com/topic/73525-help-limiting-search/#findComment-370953
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.