We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36805
    • 354 Posts
    I use this snippet to show articles by an a to z listing. My client asked for an improvement which i am not sure how to implement. He wants that letters for which there are no articles to have a different css style.

    I do not want to query the database for each letter. I don't have any ideas on a better approach. Any help is most welcome.

    https://forums.modx.com/thread/35824/snippet-a-to-z-list-on-single-page
      • 3749
      • 24,544 Posts
      What version of MODX?

      For Revolution, the SiteAtoZ snippet might do that. I can't remember. I think they are not styled as links, which means you could change their characteristics with CSS.



      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODx info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 19369
        • 1,098 Posts
        You could check the number of nested elements inside each letter with jQuery and output a class if there is nothing inside.
          • 36805
          • 354 Posts
          @Bobray
          It is an Evolution site

          @microcipcip
          I used one of those before. In this case I would like to avoid fetching all documents. If all articles are fetched page load speed would suffer otherwise. The goal is to make it able to work with 100-200 articles.

          Since the snippet is Ditto based later I even want to add pagination for one letter.
            • 3749
            • 24,544 Posts
            Have you tried doing a "view source" on the page to see there's anything unique about letters with no children? If so, you could use either JS, or a plugin tied to OnWebPagePrerender to alter their look.



            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODX info for everyone: http://bobsguides.com/modx.html
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 36805
              • 354 Posts
              No they all have the same format, see code below. An added css class would be great but that's just the missing part.
              <span class="azalpha"><a href="mypage.html?letter=B">B</a></span>
                • 36805
                • 354 Posts
                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 smiley

                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.]