i m developing the site where i wanted to number the search result pages .
like 1 2 3 4 5 next lastpage

my code is working if i want to dispaly all from tahe database
but my code is not working properly if i wnat to show the search result and numbering those pages.if any body could help. it would be great
thanks

i have tried this code.
i m not getting the numbers properly for search result.
i think i have to somthing with this code. i want the search result which seraches only locality
$query = "SELECT COUNT(locality) AS numrows FROM randoms where locality like '$locality%'";
my problem is i m getting all data from that database which is not like locality.
if i remeove the numbering script it is working great.
but is i insert the numbering code then i m not getting the page with searched locality .

It sounds like you are having more of an SQL issue rather than a PHP issue. If you only want the first ten results (for use in the first page), use the following query:

SELECT * FROM randoms WHERE locality LIKE '$locality%' LIMIT 10

This will return all the data for the first 10 rows.

Use the following query to generate the third page:

SELECT * FROM randoms WHERE locality LIKE '$locality%' LIMIT 20, 10

This use of the LIMIT directive tells MySQL to skip the first 20 records and return the following 10 records.

Does this address your situation?

this is my code. i want numbers to my serach result page.i m geting the numbers sucess :-| fully if i want all the data from the database.
but my code is not working if i want to get serach result with page numbers. yes i think the issue with sql. but i thought many php programers go through this coding as this is the basic coding.
but i dont know how come it is not working with me

<?
include 'db_connect.php';
$rowsPerPage = 3; 
$type=$_POST['TypeID'];
$locality=$_POST['locality'];
$city=$_POST['City'];
$state=$_POST['State'];
$price=$_POST['price'];
$price2=$_POST['price1'];
print "the $locality";

// by default we show first page 
$pageNum = 1; 

// if $_GET['page'] defined, use it as page number 
if(isset($_GET['page'])) 
{ 
    $pageNum = $_GET['page']; 
} 

// counting the offset 
$offset = ($pageNum - 1) * $rowsPerPage; 
$result=mysql_query("select * FROM newlists  where `locality` like '%$locality' limit $offset,$rowsPerPage")or 

die("no");
//$result1=mysql_query("select COUNT() as numrows from newlists where locality like '%$locality'");
$num=mysql_num_rows($result);

if($num!=0)
{

//mysql_close();
include 'in4.html';
print "<b>";?>
<p>

<TABLE 
      style="BORDER-RIGHT: green 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: green 1px solid; PADDING-LEFT: 0px; 

PADDING-BOTTOM: 0px; MARGIN: 5px 5px 0px; BORDER-LEFT: green 1px solid; WIDTH: 680px; PADDING-TOP: 0px; 

BORDER-BOTTOM: green 1px solid; BACKGROUND-COLOR: white; align: top" 
      cellSpacing=0>
<tr vspace=2 hspace=2>

<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:#ff6600" ><font color=white size=2 />Type</font></td>

<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:#ff6600"><b><font color=white size=2 />Date of Post</font></td>

<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:#ff6600"><b><font color=white size=2 />Locality</font></td>
<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:#ff6600"><b><font color=white size=2 />Price</font></td>
<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:#ff6600"><b><font color=white size=2 />Details</font></td>
<?
$i=0;
while($i<$num)
{print "<tr>";
$id=mysql_result($result ,$i, "id");
$type=mysql_result($result,$i,"type");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");

$locality=mysql_result($result,$i,"locality");
$price=mysql_result($result,$i,"price");
$total=mysql_result($result ,$i,"totsqft");
$sqft=mysql_result($result ,$i,"sqft");
$rooms=mysql_result($result ,$i,"rooms");
$bathrooms=mysql_result($result,$i,"bathrooms");
$floors=mysql_result($result,$i,"floors");
$cont=mysql_result($result ,$i, "info");
$price=mysql_result($result ,$i, "price");
$des=mysql_result($result, $i, "description");
$reg=mysql_result($result, $i, "regdate");
//print $price;
?>
<b>
<TD align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:white"><FONT  SIZE=2 COLOR=MAROON/><? echo $type ?></FONT></TD>
<TD align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:white"><FONT FACE=AERIAL SIZE=2 COLOR=MAROON align=center/><? echo $reg ?></FONT></TD>

<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:white"><font face=aerial size=2 COLOR=MAROON/><?echo $locality?></font></td>
<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:white"><font face=aerial size=2 COLOR=MAROON/><?echo  $price?></font></td>
<td align=center style="border-right:green  1px solid; border-left:green 1px solid;border-bottom:green 1px 

solid;background-color:white"><font face=aerial size=2 COLOR=MAROON/>
<a href=details.php?id=<?echo $id?>&city=<?echo $city?>&state=<?echo $state?>>KJA<?echo $id?></a><?



PRINT "</TR>";
$i++;

}
print "</table>";
print "</center>";
// how many rows we have in database 
//print "tthee $locality";
$locality=$_POST['locality'];
print "THE $locality";


$query1   = "SELECT COUNT(id) as numrows from newlists where locality like '%$locality%'"; 
$result1  = mysql_query($query1) or die('Error, query failed'); 
$row     = mysql_fetch_array($result1, MYSQL_ASSOC); 
$numrows = $row['numrows'];
// how many pages we have when using paging? 
$maxPage = ceil($numrows/$rowsPerPage);
print $maxPage; 

// print the link to access each page 
$self = 'search.php';

$nav = ''; 
for($page = 1; $page <= $maxPage; $page++) 
{ 
    if ($page == $pageNum) 
    { 
        $nav .= " $page ";   // no need to create a link to current page 
    } 
    else 
    { 
        $nav .= " <a href=\"$self?page=$page\">$page</a> "; 
    }         
} 

// creating previous and next link 
// plus the link to go straight to 
// the first and last page 
print "<center>";

if ($pageNum > 1) 
{ 
    $page = $pageNum - 1; 
    $prev = " <a href=\"$self?page=$page\">[Prev]</a> "; 

    $first = " <a href=\"$self?page=1\">[First Page]</a> "; 
}  
else 
{ 
    $prev  = '&nbsp;'; // we're on page one, don't print previous link 
    $first = '&nbsp;'; // nor the first page link 
} 

if ($pageNum < $maxPage) 
{ 
    $page = $pageNum + 1; 
    $next = " <a href=\"$self?page=$page\">[Next]</a> "; 

    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; 
}  
else 
{ 
    $next = '&nbsp;'; // we're on the last page, don't print next link 
    $last = '&nbsp;'; // nor the last page link 
} 

// print the navigation link 
echo $first . $prev . $nav . $next . $last;

//mysql_close();?></center><?
include 'in3.html';
mysql_close();
die("");

}
else
{
include 'in1.html';?>
<font size=2 color="red">No matching found</font><?include 'in.html';
}
die("");

?>

Edited 3 Years Ago by pyTony: fixed formating

This article has been dead for over six months. Start a new discussion instead.