Jump to content

paginating search result

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
balamberas

balamberas

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, i have a problem with the prev 1 2 3 4 next. when i click on page 2 the url shows search.php?start=20, while i want it to show result 20+


<header>

<?php

include ("connect.php");

 

$per_page = 20;

$start = $_GET ['start'];

$record_count = mysql_num_rows (mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR

`location` LIKE '%$keyword%'"));

$max_pages = $record_count / $per_page; // may come out as a decimal

if(!$start){$start =0;}

$get = mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR

`location` LIKE '%$keyword%' ORDER BY date_posted DESC LIMIT $start, $per_page");

?>

 </header>

<body>

<?php

 

include ('connect.php');

 

error_reporting(E_ALL);

ini_set('display_errors', '1');

 


$x=0;

$construct=' ';

$foundnum=0;  

$search = $_GET['search'];

$prev = $start - $per_page;

$next = $start + $per_page;

 

if(isset($_GET['keyword'])){

 $keyword = $_GET['keyword'];

 

}else{

 

$keyword = $search;

 

}

 

if (strlen($search)<=2)

 

   echo "search term to short.";

 else  

{

  echo " You searched for <b>$search</b><hr size='1'>";

 

//connect to our database

 

 $search_exploded = explode(" ",$search);

 

foreach($search_exploded as $keyword)

 

{

 

// construct query

 

$x++;

if ($x==1)

    $construct .= " location LIKE '%$keyword%'";  

    else

    $construct .= " OR location LIKE '%$keyword%'";

 

      }

// echo out construct

 

 $construct = "SELECT * FROM flats WHERE $construct";

 $run = mysql_query($construct);

 $foundnum = mysql_num_rows($run);

 

 

if ($foundnum==0)

  echo "No results found.";

else

{

   echo "$foundnum result found!<p>";

 

 

 while ($runrows = mysql_fetch_assoc($run))

 

{

 

// get data

 

   $select = $runrows['type'];

   $title = $runrows['title'];

   $location = $runrows['location'];

   $rent = $runrows['rent'];

   $description = $runrows['description'];

   $contactEmail = $runrows['contactEmail'];

   $number = $runrows['number'];

 

echo "

 

    $title

    <br>

    $select

    <br>

    $rent

    <br>

    $location

    <br>

    $description

    <br>

    $contactEmail

    <br>

    $number

   <hr>";

 

}      

 

 

      }

 }   

 

 

 

?>

 

</td>

</tr>

 

     <tr align="center">

     <td>

 

      <?php

 

      if($start>0){

      echo "<a href='search.php?start=$prev'> Prev </a>";}

      $i=1;

      for ($x=0;$x<$record_count;$x=$x+$per_page)

      {

       if($start!=$x){

       echo "<a href='search.php?start=$x'>$i | </a>";

      }

       else{

      echo "<a href='search.php?start=$x'><b> $i | </b></a>";}

       $i++;

       }

      if($start<$record_count-$per_page){echo "<a href='search.php?start=$next'> Next </a>";}

      ?>

 

    </td>

    </tr>

   </table>

</body>





#2
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
I am not seeing your problem, the code you posted will show results 20 - 40 on page 2. Is that not what is happening?

#3
balamberas

balamberas

    Newbie

  • Members
  • Pip
  • 4 posts
No, what happens now is that it shows the same result as page 1-20. say i search for keyword "peppar", and i have 15 result found. instead of just showing page one, i get page 1 2 3 4 5 6 7 displayed and no matter what number i click the same result is shown. dont know why tho.

#4
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
I Believe that
for ($x=0;$x<$record_count;$x=$x+$per_page)
{
       if($start!=$x){
       echo "<a href='search.php?start=$x'>$i | </a>";
}

should be
for ($x=0;$x<[B]$max_pages[/B];$x=$x+$per_page)
{
       if($start!=$x){
       echo "<a href='search.php?start=$x'>$i | </a>";
}

The way you have it now will show a new page for every result, its showing the same thing for every page because the limit falls out of the scope of the results.

#5
balamberas

balamberas

    Newbie

  • Members
  • Pip
  • 4 posts
thanks, that solved it.

#6
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
No problem, glad I could help