homepage Welcome to WebmasterWorld Guest from 50.19.172.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Alphabetical Pagination Using Arrays
Sp4rkyM4rk



 
Msg#: 4191632 posted 9:16 pm on Aug 24, 2010 (gmt 0)

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>';

}
else
{

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. :)

 

rocknbil

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



 
Msg#: 4191632 posted 4:19 am on Aug 25, 2010 (gmt 0)

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>';
$class='link';
}
else {
$link = $letters;
$class='plaintext';
}
echo "<span class=\"$class\">$link</span>";
}

Sp4rkyM4rk



 
Msg#: 4191632 posted 3:03 pm on Aug 25, 2010 (gmt 0)

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.

rocknbil

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



 
Msg#: 4191632 posted 3:54 pm on Aug 25, 2010 (gmt 0)

I don't see why this

&amp;letter=0

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,

page.php#0

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>

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

Sp4rkyM4rk



 
Msg#: 4191632 posted 5:20 pm on Aug 25, 2010 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved