Click to See Complete Forum and Search --> : How to "Dynamic A-Z Anchor List" based on another query


vicpal25
01-25-2005, 01:34 PM
Hello you guys!
I am retreiving close to 500 records from an access database with PHP and I wanna create a dyamic ancho link list to go to the specified member. Here is my PHP code


<?

require_once("www/connections/connpepnet.php");
$connect = @odbc_connect($dsn, $user, $passwd);
if($connect) {

$query = "SELECT CnBio_Title_1, CnBio_First_Name, CnBio_Middle_Name, CnBio_Last_Name, CnPrBs_Position, CnPrBS_Org_Name, CnPrBsAdr_Addrline1, CnPrBsAdr_Addrline2, CnPrBsAdr_City, CnPrBsAdr_State, CnPrBsAdr_ZIP, CnPrBsAdrPh_1_01_Phone_type, CnPrBsAdrPh_1_01_Phone_number, CnPrBsAdrPh_2_01_Phone_type, CnPrBsAdrPh_2_01_Phone_number, CnAdrPrfPh_1_01_Phone_number, CnAdrPrfPh_2_01_Phone_number, CnMem_1_01_Category, CnMem_1_01_AtrCat_1_01_Description FROM pepnetonlinedirectory ORDER BY CnBio_Last_Name;";
$result = odbc_exec($connect, $query);
while(odbc_fetch_row($result))
{
$title=odbc_result($result,1);
$firstname =odbc_result($result,2);
$middlename=odbc_result($result,3);
$lastname=odbc_result($result,4);
$position=odbc_result($result,5);
$orgname=odbc_result($result,6);
$address1=odbc_result($result,7);
$address2=odbc_result($result,8);
$city=odbc_result($result,9);
$state=odbc_result($result,10);
$zip=odbc_result($result,11);
$phonetype1=odbc_result($result,12);
$phonenumber1=odbc_result($result,13);
$phonetype2=odbc_result($result,14);
$phonenumber2=odbc_result($result,15);
$email1=odbc_result($result,16);
$email2=odbc_result($result,17);
$category=odbc_result($result,18);
$description=odbc_result($result,19);


print("<p>");
print("<strong>$title ");
print("$firstname ");
print("$middlename ");
print("$lastname</strong>");
if (!$position == "") {
print("<br />$position");
}
print("</br>");
print("$orgname");
print("<br/>");
print("$address1");
if (!$address2 == "") {
print("<br/>$address2");
}
print("<br/>");
print("$city,");
print("$state ");
print("$zip");
print("</br>");
print("$phonetype1 Phone: ");
print("$phonenumber1");
print("</br>");
print("$phonetype2 Phone: ");
print("$phonenumber2");
print("</p>");
if ($email1=="") {
print("$email2");
}
else {
print("$email1");
}
print("</br>");
print("$category");
print("</br>");
print("$description");
}
odbc_close($connect);
}

?>

Does anyone know or have any idea how this can be done? any tips would be gladly appreciated/ Thanks!

ShrineDesigns
01-25-2005, 03:16 PM
huh?

do you mean something like this: http://www.ani-webstation.com/index.php?cat=1&sub=0&pg=1&ppg=10

here is a simple example:<?php
$pg = (isset($_GET['pg']) && !empty($_GET['pg'])) ? $_GET['pg'] : 1; // page number
$ppg = (isset($_GET['ppg']) && !empty($_GET['ppg'])) ? $_GET['ppg'] : 10; // items per page

$db = @odbc_connect('localhost', 'root', '');

if($db === false)
{
die("unable to connect to database");
}
$sql = "SELECT * FROM table LIMIT " . ($pg - 1) * $ppg . "," . $ppg;

$result = @odbc_exec($db, $sql);

while($row = odbc_fetch_array($result))
{
list($title, $firstname, $middlename, $lastname, $position, $orgname, $address1, $address2, $city, $state, $zip, $phonetype1, $phonenumber1, $phonetype2, $phonenumber2, $email1, $email2, $category, $description) = $row;
// output results ...
}
odbc_free_result($result);

$sql = preg_replace(array("/^(SELECT)\\s+.+(FROM)/i", "/(LIMIT)\\s+\\d+,?\\d*$/i"), array("SELECT COUNT(*) FROM", ""), $sql);
$result = odbc_exec($db, $sql);
$count = odbc_result($result, 0);
odbc_free_result($result);

if($count > $ppg)
{
$page_nav = array();

for($i = 1; $i <= ceil($count / $ppg); $i++)
{
$page_nav[] = ($i == $pg) ? "<b>$i</b>" : "<a href=\"{$_SERVER['PHP_SELF']}?pg=$i&amp;ppg=$ppg\">$i</a>";
}
echo implode(' ', $page_nav);
}
odbc_close($db);
?>

vicpal25
01-25-2005, 03:28 PM
Not quite. Something that will display like:

A B C D E F G H I J K L M N O P Q R S T U V W Z Y Z

as linke and when you click they will anchor navigate to the record that has been pulled from the database. On the same as well.

ShrineDesigns
01-25-2005, 04:03 PM
oh ok<?php
$pg = (isset($_GET['pg']) && !empty($_GET['pg'])) ? $_GET['pg'] : "a";

$db = @odbc_connect('localhost', 'root', '');

if($db === false)
{
die("unable to connect to database");
}
// i don't know if ODBC supports such a sql statement
$sql = "SELECT * FROM table WHERE CnBio_Last_Name LIKE '%{$pg}' ORDER BY CnBio_Last_Name ASC, CnBio_First_Name ASC";

$result = @odbc_exec($db, $sql);

while($row = odbc_fetch_array($result))
{
list($title, $firstname, $middlename, $lastname, $position, $orgname, $address1, $address2, $city, $state, $zip, $phonetype1, $phonenumber1, $phonetype2, $phonenumber2, $email1, $email2, $category, $description) = $row;
// output results ...
}
odbc_free_result($result);

$page_nav = array();

for($i = 97; $i <= 122; $i++)
{
$l = chr($i);
$page_nav[] = ($l == $pg) ? "<b>$l</b>" : "<a href=\"{$_SERVER['PHP_SELF']}?pg=$l\">$l</a>";
}
echo implode(' ', $page_nav);
odbc_close($db);
?>