Hi all, Im a php newbie and have a problem with my bike adverts page, basically it outputs an SQL query so that the results show 3 per page, but the next page link doesnt seem to work, its sticks on page 1!
<?php
$sortorder = "DESC";
$sortfield = "timestamp";
if(isset($_GET["sortorder"])) {
$sortorder = $_GET["sortorder"];
}
if(isset($_GET["sortfield"])) {
$sortfield= $_GET["sortfield"];
}
$username="user";
$password="pass";
$database="sellbike";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//---------Work out number of pages-----------------
if (!($limit)){
$limit = 3;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults=mysql_query("SELECT * FROM bikeinfolive");
$numrows=mysql_num_rows($numresults); // Number of rows returned from above query.
$pages = intval($numrows/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($numrows%$limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $numrows;} // If last results page, last result equals total number of results.
?>
<DIV class="mainbox" id=float-centre>
<p style="color:#00749e; font-size:10px" align="right"><b>Listings <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
</b></p>
<div style="margin-left:10px">
<form method="get">
<select name="sortorder">
<option value="ASC">Ascending</option>
<option value="DESC">Descending</option>
</select>
<select name="sortfield">
<option value="cc">cc</option>
<option value="price">Price</option>
</select>
<input type="submit" value="Sort"></form>
</div>
<?
//-------------Generate results--------------------
$results = mysql_query("SELECT * FROM bikeinfolive ORDER BY $sortfield $sortorder LIMIT $page, $limit");
while ($i = mysql_fetch_array($results))
{
?>
<!--START OF AD-->
<div id="adbox">
<!--photo-->
<div id="ad1">
<a href="upload/<?=$i["photo"]?>" onclick="return popup(this.href,'Photo');"><img src="upload/<?=$i["photo"]?>" width="160" height="120"></a>
<div id="ad1a">
<a href="upload/<?=$i["photo2"]?>" onclick="return popup(this.href,'Photo');"><img src="upload/<?=$i["photo2"]?>" width="40" height="30"></a>
</div>
<div id="ad1b">
<a href="upload/<?=$i["photo3"]?>" onclick="return popup(this.href,'Photo');"><img src="upload/<?=$i["photo3"]?>" width="40" height="30"></a>
</div>
<div id="ad1c">
<a href="upload/<?=$i["photo4"]?>" onclick="return popup(this.href,'Photo');"><img src="upload/<?=$i["photo4"]?>" width="40" height="30"></a>
</div>
</div>
<!--bikespec-->
<div id="ad2">
<b><span style="font-size:16px; color:#C00"><?=$i["manuf"]?> <?=$i["model"]?></span><br>
<?=$i["yr"]?> (<?=$i["reg"]?> reg)<br>
<?=$i["cc"]?>cc<br>
<?=$i["miles"]?>miles</b>
</div>
<!--price/contact-->
<div id="ad3">
<b><span style="font-size:16px">£<?=$i["price"]?></span></b>
<br>
<b>Name:</b> <?=$i["name"]?><br>
<b>Email:</b> <?=$i["email"]?><br>
<b>Phone:</b> <?=$i["tel"]?>
</div>
<!--descrp-->
<div id="ad4">
<?=$i["desc"] ?>
</div>
</div>
<!--END AD-->
<?
}
?>
<p align="center">
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"forsale_v1.4.php?sortorder=$sortorder&sortfield=$sortfield&page=$back_page&limit=$limit\">back</a> \n");}
for ($x=1; $x <= $pages; $x++) // loop through each page and give link to it.
{
$ppage = $limit*($x - 1);
if ($ppage == $page){
echo("<b>$x</b>\n");} // If current page don't give link, just text.
else{
echo("<a href=\"forsale_v1.4.php?sortorder=$sortorder&sortfield=$sortfield&page=$ppage&limit=$limit\">$x</a> \n");}
}
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"forsale_v1.4.php?sortorder=$sortorder&sortfield=$sortfield&page=$next_page&limit=$limit\">next</a>");}
?>
</p>