Hi,
I had a website built a while back and since then I have started learning php & mysql and have grasped how most things work but I can't figure out how to alter this code to get the desired result in this instance.
At the moment a search results page lists the first 10 records and all the pages with results. Originally this wasn't an issue but as the database has been growing I am now left with a huge amount of page numbers before the actual data. I would like to change it so it lists 10 pages and has a next/previous first and last link. Screenshot attached. Hope someone can help. Thanks in advance...G
Here's the code and
/*****************************************************************************************************
//
// A function to display a list of search results
//
//*****************************************************************************************************
function display_results($properties, $url, $sort, $pre_sort, $order, $link = "", $return = 1) {
global $get, $post, $request, $cookie, $user;
$perpage = 10;
$searchbutton = $request['searchbutton'];
$prepage = $request["prepage"];
$my_favour = array();
$my_favour = get_favour($user);
$num = count($properties);
if ($num>1)
printf("%s properties have been found in our database.", $num);
else {
if ($num==1)
printf("%s property has been found in our database.", $num);
else
printf("No property is found.");
}
$pages = ceil($num / $perpage);
if (empty($searchbutton))
$searchbutton = 1;
if (!preg_match("/\D/",$searchbutton))
$prepage = $searchbutton;
if ($searchbutton=="prev") {
$prepage--;
if ($prepage<1)
$prepage=1;
}
if ($searchbutton=="prev 5"){
$prepage=$prepage-5;
if ($prepage<1)
$prepage=1;
}
if ($searchbutton=="next"){
$prepage++;
if ($prepage>$pages)
$prepage=$pages;
}
if ($searchbutton=="next 5"){
$prepage=$prepage+5;
if ($prepage>$pages)
$prepage=$pages;
}
$temp = (int) $searchbutton;
if ($temp>=1 && $temp<=($pages+1)) {
$prepage = $temp;
}
$searchbutton=$prepage;
$startpage = ($searchbutton-1)*$perpage+1;
if (empty($startpage))
$startpage=1;
$endpage = $startpage + $perpage;
if ($endpage>$num)
$endpage = $num+1;
if ($order)
$try = "t";
else
$try = "";
for ($j=1;$j<=8;$j++) {
if ($j==$sort) {
if ($order)
$fg[$j] = '<img src=./images/sortup.gif border="0">';
else
$fg[$j] = '<img src=./images/sortdown.gif border="0">';
}
else
$fg[$j] = "";
}
$return_link = sprintf("reu=%s?submit=%s&prepage=%s&searchbutton=%s&%s&sort=%s&u=%s&d=%s%s", $url, "page", $prepage, $prepage, $links, $sort, $pre_sort, $try, $link);
$return_link = "&".str_replace("&", "*", $return_link);
//********************************* Navigation *********************************
if ($pages>1) {
printf('<div align="center">');;
printf('<br><a href="%s?submit=%s&prepage=%s&searchbutton=%s&%s&sort=%s&u=%s&d=%s%s"><<</a> ', $url, "page", $prepage, "prev", $links, $sort, $pre_sort, $try, $link);
for ($j=0;$j<$pages;$j++) {
if (($j+1)==$prepage)
printf('[ <b>%s</b> ]', ($j+1));
else
printf('[ <a href="%s?submit=%s&prepage=%s&searchbutton=%s&%s&sort=%s&u=%s&d=%s%s">%s</a> ]', $url, "page", $prepage, ($j+1), $links, $sort, $pre_sort, $try, $link, ($j+1));
}
printf(' <a href="%s?submit=%s&prepage=%s&searchbutton=%s&%s&sort=%s&u=%s&d=%s%s">>></a>', $url, "page", $prepage, "next", $links, $sort, $pre_sort, $try, ($j+1), $link);
printf("</div><br>");
}
else
printf("<br><br>");
//********************************* Navigation *********************************
?>
<table width="100%" border="1" cellspacing="3" cellpadding="3" class="searchTable">
<colgroup>
<col style="width: 1%"/>
<col style="width: 24%;"/>
<col style="width: 15%;"/>
<col style="width: 15%;"/>
<col style="width: 10%;"/>
<col style="width: 10%;"/>
<col style="width: 10%;"/>
<col style="width: 10%;"/>
<col style="width: 5%;"/>
</colgroup>
<tr>
<th scope="col" class="barHead"> </th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=1&u=<?= $sort?>&d=<?= $try?><?= $link?>');">Title <?= $fg[1] ?></th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=2&u=<?= $sort?>&d=<?= $try?><?= $link?>');">Price <?= $fg[2] ?></th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=3&u=<?= $sort?>&d=<?= $try?><?= $link?>');">Region <?= $fg[3] ?></th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=4&u=<?= $sort?>&d=<?= $try?><?= $link?>');">District <?= $fg[4] ?></th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=5&u=<?= $sort?>&d=<?= $try?><?= $link?>');">Suburb <?= $fg[5] ?></th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=6&u=<?= $sort?>&d=<?= $try?><?= $link?>');">Land Size <?= $fg[6] ?></th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=7&u=<?= $sort?>&d=<?= $try?><?= $link?>');">Floor Size <?= $fg[7] ?></th>
<th scope="col" class="barHead" onclick="sort('<?= $url ?>?c=t&sort=8&u=<?= $sort?>&d=<?= $try?><?= $link?>');">Bed Rm#<?= $fg[8] ?></th>
</tr>
<?php
for ($i=$startpage;$i<$endpage;$i++) {
$property = $properties[$i];
if (is_expired($property['listed_date']))
$ss = '<img src="./images/expired.gif" border="0" alt="expired"/>';
else {
if (in_array($property['id'], $my_favour))
$ss = '<img src="./images/star.gif" border="0" alt="favour"/>';
else
$ss = "";
}
printf('<tr>'."\n");
printf('<td>%s</td><td><a href="%s">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>',
$ss, sprintf('viewProperty.php?pid=%s&r=%s%s',$property['id'], $return, $return_link), $property['title'], d_price($property['price']),
get_value("location", $property['region']), get_value("location", $property['district']), get_value("location", $property['suburb']),
$property['land_size']." ".get_value("unit", $property['land_size_unit']),
$property['floor_size']." ".get_value("unit", $property['floor_size_unit']),
d_bed($property['bed']));
printf('</tr>'."\n");
}
?>
</table>
<?php
}
function get_property_list($user, $sort, $order) {
switch ($sort) {
default:
case 1:
$sql = sprintf("select * from property where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
case 2:
$sql = sprintf("select * from property where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
case 3:
$sql = sprintf("select property.*, location.title as l_title
from property
left join location on location.id = property.region
where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
case 4:
$sql = sprintf("select property.*, location.title as l_title
from property
left join location on location.id = property.district
where user_id=%s%s", $user['id'], append_order($sort, $order));
//$sql = sprintf("select *, (select title from location where location.id=property.district) as l_title from property where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
case 5:
$sql = sprintf("select property.*, location.title as l_title
from property
left join location on location.id = property.suburb
where user_id=%s%s", $user['id'], append_order($sort, $order));
//$sql = sprintf("select *, (select title from location where location.id=property.suburb) as l_title from property where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
case 6:
$sql = sprintf("select * from property where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
case 7:
$sql = sprintf("select * from property where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
case 8:
$sql = sprintf("select * from property where user_id=%s%s", $user['id'], append_order($sort, $order));
break;
}
query($sql);
$cou = 1;
while ($r=result()) {
$properties[$cou] = $r;
$cou++;
}
return $properties;
}
function append_order($sort, $order) {
if ($order)
$oo = "Asc";
else
$oo = "Desc";
switch ($sort) {
default:
case 1:
$sql = sprintf(" order by title %s", $oo);
break;
case 2:
$sql = sprintf(" order by price %s, title", $oo);
break;
case 3:
$sql = sprintf(" order by l_title %s, title", $oo);
break;
case 4:
$sql = sprintf(" order by l_title %s, title", $oo);
break;
case 5:
$sql = sprintf(" order by l_title %s, title", $oo);
break;
case 6:
$sql = sprintf(" order by land_size_search %s", $oo);
break;
case 7:
$sql = sprintf(" order by floor_size_search %s", $oo);
break;
case 8:
$sql = sprintf(" order by bed %s", $oo);
break;
}
return $sql;
}