Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alphabet Links 2

Status
Not open for further replies.

MrBelfry

IS-IT--Management
Joined
May 21, 2003
Messages
289
We've all seen those sites that have an alphabet with links to documents beginning with that letter like:

a b c d etc

Most examples I've seen like this often have dead links. I am writing a script for reading album reviews and if i don't have any reviews for albums beginning with the letter a then I still want the letter to display but not as a link. I use a mysql backend and am struggling to come up with any ideas about how to fo this. Can you help?

MrBelfry
 
I've done this:

Code:
		$sSQL = "SELECT *
				 FROM contact
				 WHERE contact_user_id = ".$sPersInfo["id"]."
				 ORDER BY contact_class, contact_lname, contact_fname";
$objResultSet = mysql_query($sSQL);
$i = 0;
while($row = mysql_fetch_row($objResultSet))
{
    $cInitArray[$i] = strtoupper(substr($row[3], 0, 1));
    $i++;
}			for($i=0; $i<sizeof($cInitArray); $i++)
			{
				echo &quot;<a href='#&quot;.$cInitArray[$i].&quot;'>&quot;.$cInitArray[$i].&quot;</a>&nbsp;&nbsp;&quot;;
			}

While this will show only the letters that do exist, it might give you a good starting point.

Take Care,
Mike
 
you can put the entire alphabet in an array and loop through that array. if letter array=letter from database then print url else print letter from array

sorry I can't write the exact code out of my head but I think you can work it out
 
Hey guys thanks for your help. I orignially had the same idea as hos2 but can up with what i think is a simpler solution (simpler because I could work it out!) based on Michaels post:

The code looks like this (note: i've not put the links in yet and my array $review is essentially the same as $cInitArray above).
Code:
for($i=48; $i<=90; $i++){ 
  $d = chr($i);
  if(in_array($d, $review)){
    if(is_numeric($d)){
      if($tmp==0){
	echo &quot;#&quot;;
	$tmp=1;
       }
     else {
	echo $d;
     }
   }
 }

see it in action at
MrBelfry
 
whoops

realised that the way above prints aload of ascii characters I don't want! I've changed it to deal with numbers first and then lower case letters as follows:

Code:
for($i=48; $i<=57; $i++){ 
				//$d = chr($i);
				if(in_array(chr($i), $review)){
					$echohash = true;					
					break;
				}else{
					$echohash = false;
				}
			}
			
			if($echohash){
				echo &quot;<li><a href=\&quot;browsereview.php?browse=#\&quot; title=\&quot;Browse all reviews where the album doesn't begin with a number\&quot;>#</a> </li>&quot;;
			}else{
				echo &quot;<li># </li>&quot;;
			}
			
			for($i=97; $i<123; $i++){
				if(in_array(chr($i), $review)){
					echo &quot;<li><a href=\&quot;browsereview.php?browse=&quot; . chr($i) . &quot;\&quot; title=\&quot;Browse all reviews where the album begins with the letter &quot; . chr($i) . &quot;\&quot;> &quot; . chr($i) . &quot;</a> </li>&quot;;
				}else{
					echo &quot;<li>&quot; . chr($i) . &quot; </li>&quot;;
				}
			}

If anybody can think of a more effecient way of doing this then let me know as this seems like a lot of code!

MrBelfry
 
That looks good, nice job! However, I'd change the title for the numbers from: Browse all reviews where the album doesn't begin with a number
to: Browse all reviews where the album begins with a number

:) Good work :)

Take Care,
Mike
 
Spotted that mistake 5 minutes after I uploaded!

Thanks for all your ideas guys. Have a great christmas and a star each for a present

MrBelfry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top