Jump to content
louisstephens

Listing pages under corresponding letter

Recommended Posts

I have done a bit of searching, but I can not seem to find an actual answer. I have a list of services as child pages under "Services". I can output the services just fine, but I cant wrap my head around how to group them "alphabetically" like:

Services
    A
    - Service "A" 1
    - Service "A" 2
    - Service "A" 3
    B
    - Service "B" 1
    - Service "B" 2
    - Service "B" 3
    C
    - Service "C" 1
    - Service "C" 2
    - Service "C" 3

Has anyone achieved this type of functionality before?

Share this post


Link to post
Share on other sites

If you don't want to go to deeply into writing your loop with comparing the previous letter, you could use the PageArray::groupBy method I posted:

With it, you could write something like this (i't a bit of guesswork since I don't know if the "A" "B" "C" parts are really in the title or in a different field, but you should get the gist:

<?php

echo "<h2>Services</h2>";

$children = $page->children();
$children->sort("title"); // Important for grouping to work
$grouped = $children->groupBy(function($pg) {
  // first argument ($pg) is always the page object we are examining
  $letter = preg_replace('/^Server "(.)".*$/', '$1', $pg->title);
  return array($letter);
});

foreach($grouped as $letter => $letterPages) {
  echo "<h3>$letter</h3>";
  echo "<ul>";
  foreach($letterPages as $lpg) {
    echo "<li>{$lpg->title}</li>";
  }
  echo "</ul>";
}

 

  • Like 2

Share this post


Link to post
Share on other sites

Thanks BitPoet, I will give it a shot. I guess I should have been more clear. Each service does begin with the letter, Like:

"Blowing- etc etc"
"Blowing- etc etc"

"Pest- etc etc"
"Pest- etc etc"
"Pest- etc etc"

 

Share this post


Link to post
Share on other sites

So I tried your groupBy, and I am currently getting the following:

"Blowing- etc etc"

"Blowing- etc etc"

"Blowing- etc etc"

"Pest- etc etc"

"Pest- etc etc

 

I assume that $letter is just simply finding matches to the whole string in this case and grouping them together? I am sorry, I have not used preg_replace that offten, and couldn't tell you even where to begin in editing it to get the desired result of 

B
Blowing- etc etc
Blowing- etc etc

P
Pest- etc etc
Pest- etc etc
Pest- etc etc

 

Share this post


Link to post
Share on other sites

The regex was just a shot in the dark. Since it's all about starting letters, you can change that line to

	$letter = substr($pg->title, 0, 1);

 

Share this post


Link to post
Share on other sites

This should work too.  (I used the method to output the month as a heading for a list of events sorted by start_date). What it does:

  1. Gets the first letter of the first child and output it, eg "A"
  2. Gets the next child and compares the first letter of its title with the previous child's title first letter
  3. If the same, it continues on
  4. If different, it outputs the new first letter
<h2>Services</h2>
<?php if ($page->children->count) :
    $children = $page->children('sort=title');
    foreach ($children as $child) :
        $letter = substr($child->title, 0, 1);  // get the first letter of the title
        if ($child->id == $children->first->id) : // first child ?>
            <h2><?=$letter?></h2>
        <?php else :
            $prevLetter = substr($child->prev->title, 0, 1);;
            if ( $prevLetter !== $letter) : // different first letter in title to previous sibling page ?>
                <h2><?=$letter?></h2>
            <?php endif; 
        endif; ?>
		<p><?=$child->title?></p>
    <?php endforeach;
endif; ?>

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By calago
      after the migration to version 3.0.145 our export templates all of a sudden dont work properly, 
      we have a functionality where we use a boiler template with a set of fields. for some reason since the upgrade when we select the export template to export it. it wont show any of the fields added after the migration, 

      as you can see the offer certificate remarks field has been added to the export, 
      but when we select it it wont come trough

    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By opalepatrick
      Hi, trying to order pages by modified date, date. I can do 'sort=-date_modified, sort=-date' but that will always put the modified date first regardless of actual date order. I just want to coalesce the two fields and sort. Any ideas would be appreciated.
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
×
×
  • Create New...