Using a simple technique it was fairly straightforward to to get counts for each Letter in only one additional query. I got the idea from
http://bytes.com/topic/mysql/answers/455190-creating-z-list-query.
Using that I have modified the script to support different styles for letters which have no articles. I have hacked this script quite a bit. I will only list relevant portions so I don't confuse anyone too much
Here is how i set-up an array which stores letter counts for letters with results. For 2 articles with a B and 5 with a D you would get: array( ['B'] => 2, ['D'] => 5 ). Letters with no articles won't be in this array.
$table = $modx->getFullTableName("site_content");
$sql = "SELECT left(pagetitle, 1) as letter, count(left(pagetitle, 1)) AS letterCount FROM {$table} WHERE parent = {$startid} AND published=1 AND deleted=0 {$privateweb} {$hidemenu} GROUP BY letter";
$result = $modx->db->query($sql);
$letterArray = array();
if( $modx->db->getRecordCount( $result ) > 0 )
while( $row = $modx->db->getRow( $result ) )
$letterArray[$row['letter']] = $row['letterCount'];
Way to the bottom I use this to add a css class. 0 results get class count0 1 results gets count1 added and so on.
for ($i = 0; $i < $lcount; $i++) {
if ($i < $lcount -1) {
$atozletters .= '<span class="azalpha count'.count($letterArray[$alphabet[$i]]).'"><a href="' . $modx->makeURL($docid, '', 'letter=' . $alphabet[$i] . '') . '">' . $alphabet[$i] . '</a></span><span class="azsep"> | </span>';
} else {
$atozletters .= '<span class="azalpha count'.count($letterArray[$alphabet[$i]]).'"><a href="' . $modx->makeURL($docid, '', 'letter=' . $alphabet[$i] . '') . '">' . $alphabet[$i] . '</a></span>';
}
}
My css to gray out letters without results and remove underline from the link:
#content span.count0 a
{
color: #bababa;
text-decoration: none;
}
[ed. note: dorstox last edited this post 14 years, 6 months ago.]