Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Alphabetical Pagination Using Arrays



9:16 pm on Aug 24, 2010 (gmt 0)

5+ Year Member

I'm having a little trouble making my pagination script user friendly. I have an array of characters like so:

$character_set = array('#','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');

Basically, I want to query the database using the array to check which strings in a field begin with which letters. If a string does not begin with a letter, I still want to display the letter on the page but remove the hyperlink. I also want to remove the hyperlink and make the text bold, whenever the user is on a letter which does have records beginning with it. I have tried using a foreach() loop in combination with LIKE in SQL, to get all array values, but no luck. This is my loop and query:

foreach ($character_set as $letters)
$query = mysql_query('SELECT film_title FROM ' . REVIEW_ARTICLE_TABLE . ' WHERE film_title LIKE "' . $letters . '%"');
$rows = mysql_fetch_assoc($query);

$film_title = $rows['film_title'];

// I have a feeling this is wrong

if (in_array($film_title['0'],$character_set))

echo '<span class="link"><a href="SITEURLHERE/select.php?category=0&amp;letter=' . strtolower($letters) . '">' . $letters . '</a></span>';


echo '<span class="plaintext">' . $letters . '</span>';


I can see where the issue is - the entire array is being echo'd out rather than separate values, but I'd rather be simple as possible and not have to edit the script at all, whenever I add a record which begins with a new letter.

Thanks in advance. :)


4:19 am on Aug 25, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Been a long day, this might not be the *best* solution, but it's better. Do a count instead.

$count=0; // Initialize it, just because.
foreach ($character_set as $letters) {
$query = "select count(*) from " . REVIEW_ARTICLE_TABLE . " where film_title like '$letters%'";
$result = mysql_query($query); // just changed it to describe what it does
$rows = mysql_fetch_array($result); // Array gets both associative and list
$count = $rows[0];
// I'd prefer $link = ($count > 0)?'YES:NO but it makes this site explode
if ($count > 0) {
$link = '<a href="SITEURLHERE/select.php?category=0&amp;letter=' .
strtolower($letters) . '">' . $letters . '</a>';
else {
$link = $letters;
echo "<span class=\"$class\">$link</span>";


3:03 pm on Aug 25, 2010 (gmt 0)

5+ Year Member

This works wonders, thanks a lot. :)

I've just thought of something though. What if there is a record which begins with a number? I want to link '#' to 'category=0&amp;letter=0' in the URL. I have another array set up called '$character_set_url', which contains lowercase versions of the letters and the 0, since '#' can't be used in this way in a URL.

EDIT: I think I've got it, using the str_replace() function inside the URL, so it only replaces it there.

<a href="http://SITEURL/select.php?category=0&amp;letter=' . str_replace('#','0',strtolower($letters)) . '">' . $letters . '</a>

Although that's only the replacement in the display part. I still need to check which records begin with an integer, and if they do, add a link. It has only worked using a string which begins with '#' so far, as expected. I also need to somehow pick out a value from the array, and compare it to the $_GET['letter'] variable. Where the value matches in the array, I want the class to be 'active' and link to be removed.


3:54 pm on Aug 25, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I don't see why this


doesn't work? A good way to have this is a-z, 0-9, and "other."

A thing to be aware of though, if you're using a fragment identifier,


This would mean you'd have to id the identifier like so

<a id="0"></a>
<h5 id="0">0 and The Digits</h5>

and that generally will create problems, id's and selectors should not begin with digits. You'll want to do some preprocessing like

$letter = 'anchor_'.$letter;

and output your anchors like

<a id="anchor_0"></a>

<h5 id="anchor_0">0 and The Digits</h5>


5:20 pm on Aug 25, 2010 (gmt 0)

5+ Year Member

The URL itself works, but records beginning with a number are not included. For that particular section, if the string starts with an integer, I want the results to fall under the '#' section. At the moment, this only counts records beginning with the exact symbol ('#'), as expected since it's coming straight out of the array. I have an 'all' section with the letters following it.

Featured Threads

Hot Threads This Week

Hot Threads This Month