<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>
paginating search result
Started by balamberas, Jan 13 2010 04:07 PM
5 replies to this topic
#1
Posted 13 January 2010 - 04:07 PM
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+
|
|
|
#2
Posted 15 January 2010 - 05:15 AM
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
Posted 15 January 2010 - 10:18 AM
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
Posted 15 January 2010 - 10:43 AM
I Believe that
should be
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.
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
Posted 15 January 2010 - 12:07 PM
thanks, that solved it.
#6
Posted 15 January 2010 - 12:19 PM
No problem, glad I could help


Sign In
Create Account

Back to top









