I recently wrote a class to generate and output one of these. It takes two arguments, the name of the field you are are reading from, and the name of the table.
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
Current Code
{
public $Chars = array();
public $Alphabet = array();
public $TableName;
public $ColumnName;
public function __construct($table_name, $column_name)
{
$this->TableName = $table_name;
$this->ColumnName = $column_name;
$this->GetChars();
$this->PopulateAlphabetArray();
}
private function GetChars()
{
$sql = "SELECT DISTINCT LEFT(`{$this->ColumnName}`, 1) as first_letter FROM `{$this->TableName}`";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result))
{
$this->Chars[] = strtoupper($row[0]);
}
}
private function PopulateAlphabetArray()
{
$this->Alphabet = range("a", "z");
}
public function CreateAlphabetNavigationBar()
{
foreach ($this->Alphabet as $letter)
{
if (in_array(letter, $this->Chars))
{
$navi[] = ''.$letter.'';
}
else
{
$navi[] = $letter;
}
}
echo implode(" | ", $navi);
}
}
Example usage:
mysql_connect('localhost', 'adfasdfasdfasdfasdfasdffasdffasdfasdf', 'safsdfadsdfasdfasdfasfdas');
mysql_select_db('test');
$abc = new AbcBar('links', 'name');
$abc->CreateAlphabetNavigationBar();
If you have questions, comments, or suggestions, feel free to comment or post in the thread I created for it on www.devnetwork.net/.PHPDN Forums Thread
No comments:
Post a Comment