Forum Moderators: coopster

Message Too Old, No Replies

compare two arrays or looping thru two arrays at the same time?

how do i make alphabetical navigation link correctly?

         

Lrrr

4:23 pm on Jun 29, 2009 (gmt 0)

10+ Year Member



I'm trying to make a navigation bar of letters "A ¦ B ¦ D ¦ E ¦ F ¦ ... ¦ Z". If a listing exists that begins with a letter, it should be a link (with a parameter that feeds a MySQL SELECT statement -- i.e. ?letter=S) If no listings exist for a certain letter, it should be shown, but should not be a link.

(I'm pulling the data from a MySQL database before making an array.)

Any Ideas?

I've looked around the pages on php.net on array_diff, array_filter, array_unique and array_merge, but I'm not seeing a direction that looks promising.

rocknbil

4:55 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Lrrr.

If no listings exist for a certain letter, it should be shown, but should not be a link.

This infers you're connecting with the database and running a query on the data for every.single.navigation, is this correct?

I'd approach this a little differently. You have some function that inserts/adds/modifies/deletes records. Whatever that function is, and wherever it occurs, you would also update a small table, 26 records deep, A-Z.

letter ¦ records
A ¦ 3
B ¦ 24
C ¦ 0
........

So you'd just query this small table rather than searching/counting your records.

Alternatively, this "dataset" is so small you could store it in a text file and include it.

So,

foreach ($letter as $key=>$value) {
$nav .= ($value>0)?" <a href=\"$link?L=$key\">$key</a> ¦ ":" $key ¦ ";
}

You'd have to add an if or do something to lose the last pipe, but those are the cliff notes . . .