Hi,
This code is working fine but I want the results to be displayed in alphabetical order - I know "order by contact.contact_name asc" should go in there somewhere.
<?php
include("Connections/conn.php");
// The basic SELECT statement
$select = 'SELECT contact.id, contact.contact_name, contact.title, contact.name, contact.title_2, contact.name_2, contact.res_add, contact.postal_add, contact.phone, contact.mobile, contact.fax, contact.email, contact.orgid, contactregion.regionid';
$from = ' FROM contact LEFT JOIN contactregion ON contact.id = contactregion.contactid';
$where = ' WHERE 1=1';
//CK -- THE FIRST TIME THEY INITIATE THE SEARCH THE VARIABLES COME VIA $_POST ARRAY, BUT FROM THE NAVIGATION LINKS THEY COME FROM THE $_GET ARRAY. THIS SETS THEM ACCORDINGLY
if (empty($_POST))
{
$rid = $_GET['reg'];
$oid = $_GET['oid'];
}
else
{
$rid = $_POST['reg'];
$oid = $_POST['oid'];
}
//CK -- ADDED EXTRA PROTECTION IN CASE SOMEONE TRIES TO DO SOMETHING NASTY THROUGH THE URL
if ($rid != '') { // A region is selected
$where .= " AND contactregion.regionid='$rid'";
}
if ($oid != '') { // An organisation is selected
$where .= " AND contact.orgid='$oid'";
}
//CK -- IF A CONTACT HAD MORE THAN ONE REGION, DUPLICATES WOULD BE SHOWN....THE FOLLOWING LINE PREVENTS THIS
$where .= " GROUP BY contact.id";
// set number of results to display per page (in this case, 10 per page)
$pagelimit = "10";
//run query
$contacts = @mysql_query($select . $from . $where);
if (!$contacts) {
echo '</table>';
exit('<p>Error retrieving contacts from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}
// count number of matches
$totalrows = mysql_num_rows($contacts);
if ($totalrows > 0) {
// determine how many pages there will be by using ceil() and dividing total rows by pagelimit
$pagenums = ceil ($totalrows/$pagelimit);
// if no value for page, page = 1
if ($_GET['page']==''){
$page='1';
}
else
$page = $_GET['page'];
// create a start value
$start = ($page-1) * $pagelimit;
// blank matches found
echo "</br><b>" . $totalrows . " matches found</b><br>\n";
// Showing Results 1 to 10 (or if you're page limit were 15) 1 to 15, etc.
$starting_no = $start + 1;
if ($totalrows - $start < $pagelimit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
$end_count = $start + $pagelimit;
}
echo "Results $starting_no to $end_count shown.<br>\n";
// create dynamic next, previous, and page links
/* lets say you're set to show 10 results per page and your script comes out with 12 results.
this will allow your script to say next 2 if you're on the first page and previous 10 if you're on the second page. */
if ($totalrows - $end_count > $pagelimit) {
$var2 = $pagelimit;
} elseif ($totalrows - $end_count <= $pagelimit) {
$var2 = $totalrows - $end_count;
}
$space = " ";
$strquery = $select . $from . $where . " LIMIT ".$start." ,".$pagelimit;
//echo $strquery; // check the correct query is being written
$contacts = mysql_query("$strquery") or die('Database error: '.mysql_error());
$pubname = ereg_replace('\\[(B|EB|I|EI|L|L=|L=[-_./a-z0-9!&%#?+,\'=:;@~]+|EL|E)?(]|$)', '', $pubname);
while ($contact = mysql_fetch_array($contacts)) {
$id = $contact['id'];
$contact_name = htmlspecialchars($contact['contact_name']);
$contact_name = ereg_replace('\\[(B|EB|I|EI|L|L=|L=[-_./a-z0-9!&%#?+,\'=:;@~]+|EL|E)?(]|$)', '', $contact_name);
echo "<a href=\"details.php?id=$id\">$contact_name</a><br>\n";
}
echo "<p> </p><a href=\"contact.php\">Search again</a></table>";
// previous link (if you're on any page besides the first, create previous link)
if ($page>1) {
echo "« <a href='" . $PHP_SELF . "?page=".($page-1)."&oid=$oid&reg=$rid' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
}
// dynamic page number links
for ($i=1; $i<=$pagenums; $i++) {
if ($i!=$page) {
echo " <a href='" . $_SERVER['PHP_SELF'] . "?page=$i&oid=$oid&reg=$rid' class=main>$i</a>";
}
else {
echo " <b>[".$i."]</b>";
}
}
// next link (if the page you are on is less than the total amount of page numbers, there are more pages left)
if ($page<$pagenums) {
echo "" . $space . $space . $space . $space . " <a href='" . $PHP_SELF . "?page=".($page+1)."&oid=$oid&reg=$rid' class=main>Next " . $var2 . "</a> »";
}
// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results
/* now you can do whatever you'd like with this query. it will only output ten results per page.
change the $pagelimit variable to whatever to output more than 10 result per page. */
}
else
echo "<p> </p>No results found. <a href=\"contact.php\">Search again</a>";
?>