hi guys i solved it the ugly way :queasy: and it's working the way i wanted :
1) however im sure there is a better way to code this , any suggestion is much welcomed:
2) having problem with number of pages. For example, the data from database should be displayed in 2 pages only , but the code runs until page 3 and 4 . In other words, page 1 and 2 display data but page 3 and 4 empty. I wondering if the pagination can show 2 pages only 😕
<?php
$gType = $_GET['type'];
$gLocation = $_GET['location'];
$gPrice = $_GET['price'];
//$gHref = $_GET['href'];
//if($gHref == '')
$gHref="ContactUs_OurTeam(2).html#";
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("a_database", $con);
/*find how many rows in the table*/
$sql = "SELECT count(*) FROM property";
$result = mysql_query($sql,$con) or trigger_error("SQL",E_USER_ERROR);
$r=mysql_fetch_row($result);
$numrows = $r[0];
//$nr = mysql_num_rows($sql); // Get total of Num rows from the database query
// number of rows to show per page
$rowsperpage = 2;
// find out total pages
$totalpages = ceil($numrows/$rowsperpage);
echo $totalpages;
// get the current page or set a default
if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage=(int) $_GET['currentpage'];
} else {
$currentpage=1;
}
echo "<br> current page: ".$currentpage."<br>";
// if current page is greater than total pages, set current page to last page
if($currentpage > $totalpages) {
$currentpage = $totalpages;
}
// if current page is less than first page
if ($currentpage < 1) {
$currentpage = 1;
}
// offset of the list based on current page
$offset = ($currentpage-1)*$rowsperpage;
// This creates the numbers to click in between the next and back buttons
$centerPages = "";
$sub1 = $currentpage - 1; // replace $pn with $currentpage
$sub2 = $currentpage - 2;
$add1 = $currentpage + 1;
$add2 = $currentpage + 2;
if ($currentpage == 1) {
$centerPages .= ' <span class="pagNumActive">' . $currentpage . '</span> ';
$centerPages .= ' <a href="'.$_SERVER['REQUEST_URI'].'¤tpage='.$add1.'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice.'">'.$add1.'</a> ';
} else if ($currentpage == $totalpages) {
$centerPages .= ' <a href="' . $_SERVER['REQUEST_URI'] . '¤tpage=' . $sub1 .'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice. '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $currentpage . '</span> ';
} else if ($currentpage > 2 && $currentpage < ($totalpages - 1)) {
$centerPages .= ' <a href="' . $_SERVER['REQUEST_URI'] . '¤tpage=' . $sub2 . '&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice.'">' . $sub2 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['REQUEST_URI'] . '¤tpage=' . $sub1 .'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice. '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $currentpage . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['REQUEST_URI'] . '¤tpage=' . $add1 .'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice. '">' . $add1 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['REQUEST_URI'] . '¤tpage=' . $add2 .'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice. '">' . $add2 . '</a> ';
} else if ($currentpage > 1 && $currentpage < $totalpages) {
$centerPages .= ' <a href="' . $_SERVER['REQUEST_URI'] . '¤tpage=' . $sub1 .'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice. '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $currentpage . '</span> ';
$centerPages .= ' <a href="'. $_SERVER['REQUEST_URI'] . '¤tpage=' . $add1 . '&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice.'">' . $add1 . '</a> ';
}
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable //akmal : no change here
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($totalpages != "1"){ //akmal : instead of $lastpage insert $totalpages
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $currentpage . '</strong> of ' . $totalpages. ' '; // instead of $pn insert $currentpage
// If we are not on page 1 we can place the Back button
if ($currentpage != 1) { // again, change the $pn here
$previous = $currentpage - 1; // no change
// $paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?currentpage=' . $previous . '"> Back</a> ';
$paginationDisplay .= ' <a href="'.$_SERVER['REQUEST_URI'].'¤tpage='.$previous.'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice.'"> Back</a>';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($currentpage != $totalpages) {
$nextPage = $currentpage + 1;
//$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?currentpage=' . $nextPage . '"> Next</a> ';
$paginationDisplay .= ' <a href="'.$_SERVER['REQUEST_URI'].'¤tpage='.$nextPage.'&gType'.$gType.'&gLocation'.$gLocation.'&gPrice'.$gPrice.'"> Next</a>';
}
}
///////////////////////////////////// END Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////////////
?>
<html>
<head>
<title>STAMC stupid Pagination</title>
<style type="text/css">
.pagNumActive {
color: #000;
border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:link {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:visited {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:hover {
color: #000;
text-decoration: none;
border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:active {
color: #000;
text-decoration: none;
border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
</style>
</head>
<body>
<div style="margin-left:64px; margin-right:64px;">
<h2>Total Items: <?php echo $numrows ; ?></h2>
</div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
<div style="margin-left:64px; margin-right:64px;">
<?php print "the table should be here :";
// query the database
if ($gType == "ALL" and $gLocation == "ALL" and $gPrice == "ALL") {
$sql = "SELECT Type,Price1,Location FROM property LIMIT $offset,$rowsperpage";
} else if($gType == "ALL" and $gLocation <> "ALL" and $gPrice <> "ALL") {
$sql = "SELECT Type,Price1,Location FROM property where location='" . $gLocation . "' and price='" . $gPrice ."' LIMIT $offset,$rowsperpage";
} else if($gType <> "ALL" and $gLocation == "ALL" and $gPrice <> "ALL") {
$sql = "SELECT Type,Price1,Location FROM property where type='" . $gType . "' and price='" . $gPrice ."' LIMIT $offset,$rowsperpage";
} else if($gType <> "ALL" and $gLocation <> "ALL" and $gPrice == "ALL") {
$sql = "SELECT Type,Price1,Location FROM property where type='" . $gType . "' and location='" . $gLocation ."' LIMIT $offset,$rowsperpage";
}
else if($gType <> "ALL" and $gLocation == "ALL" and $gPrice == "ALL") {
$sql = "SELECT Type,Price1,Location FROM property where type='" . $gType . "' LIMIT $offset,$rowsperpage";
}
else if($gType == "ALL" and $gLocation <> "ALL" and $gPrice == "ALL") {
$sql = "SELECT Type,Price1,Location FROM property where location='" . $gLocation . "' LIMIT $offset,$rowsperpage";
}
else if($gType == "ALL" and $gLocation == "ALL" and $gPrice <> "ALL") {
$sql = "SELECT Type,Price1,Location FROM property where price='" . $gPrice . "' LIMIT $offset,$rowsperpage";
}
else {
$sql = " SELECT Type,Price1,Location FROM property where type='" .$gType . "' and location='" . $gLocation . "' and price='" . $gPrice . "' LIMIT $offset,$rowsperpage" ;
}
$result = mysql_query($sql);
echo "<table border='1'> <tr> <th>Type</th> <th>Price</th> <th>Location</th> </tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><a href='$gHref'>" . $row['Type'] . "</a></td>";
echo "<td>" . $row['Price1'] . "</td>";
echo "<td>" . $row['Location'] . "</td>";
echo "</tr>"; }
echo "</table>";
?>
</div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
</body>
</html>