Jump to content

How to sort $pages by the first Letter ?


iNoize
 Share

Recommended Posts

Hello, 

i need to sort $pages by the first letter an show on the page like 

A

   - Aname Page 

   - Aname Page 

   - Aname Page 

   - Aname Page 

B

   - Bname Page 

   - Bname Page 

   - Bname Page 

   - Bname Page

How can i check which is the first letter from the page.title ?? 

Tnx 

Link to comment
Share on other sites

Hi iNoize,

Checkout this example used by Ryan to view tags in his blog profile:

<?php 

/**
 * Generate a list of tags A-Z
 * 
 * Used by the /site/templats/tags.php template file.
 *
 * VARIABLES: 
 * ==========
 * $tags PageArray of tags
 * 
 * Each of the tags has a numPosts property containing the number of posts used by the tag.
 *
 */

$lastLetter = '';
$out = '';
$letters = array();

foreach($tags as $tag) {
	$letter = strtoupper(substr($tag->title, 0, 1)); 
	if($letter != $lastLetter) {
		if($lastLetter) $out .= "</ul>";
		$out .= "<h3 id='letter_$letter'>$letter</h3>"; 
		$out .= "<ul class='tags posts-group'>";
		$letters[] = $letter; 
	}
	$lastLetter = $letter; 
	$numPosts = sprintf(_n('%d post', '%d posts', $tag->numPosts), $tag->numPosts);
	$out .= "<li><a href='{$tag->url}'>{$tag->title}</a> <span class='num-posts'>$numPosts</span></li>";
}
$out .= "</ul>";

echo "<p class='jumplinks'>";
foreach($letters as $letter) {
	echo "<a href='./#letter_$letter'>$letter</a> ";
}
echo "</p>";
echo $out; 

  • Like 7
Link to comment
Share on other sites

Thank you ver much. It works fine for me. Now i need to loop the array trough three divs. 

Could you give me also some advice ? My idea was to get the count of all $pages and divide it. Then loop for every col from to. 

$lastLetter = '';
$out = '';
$letters = array();

$anzahl = $pages->count("template=kunde,   bereich=$bereich"); // here I have the count but how to llop now trough three divs? 
$kunden = $pages->find("template=kunde,  bereich=$bereich, sort=title");
						

foreach($kunden as $tag) {
$letter = strtoupper(substr($tag->title, 0, 1)); 
if($letter != $lastLetter) {
if($lastLetter) $out .= "</ul>";
$out .= "<h3 id='letter_$letter'>$letter</h3>"; 
$out .= "<ul class='tags posts-group'>";
$letters[] = $letter; 
}
$lastLetter = $letter; 
$out .= "<li><a href='{$tag->url}'>{$tag->title}</a> </li>";
								}
$out .= "</ul>";
echo "<p class='jumplinks'>";
foreach($letters as $letter) {
echo "<a href='./#letter_$letter'>$letter</a> ";
}
echo "</p>";
echo $out; 
							
Link to comment
Share on other sites

thanks for you advice. ive tried this example but its sort for me 

like this 

A A A 

B B B 

but i need 

a  d  h

b  f   i

c  g  j

I think its much more simpler. in the theory i get the count divide it trough 3 dan loop from to every time ?  ist it so ?

Link to comment
Share on other sites

//set counter
$count = 0;

//divider
//$anzahl from your $page->count()
$div_one = $anzahl / 3;  // for example 7
$div_two = $div_one * 2; // for example 14
$div_three = $anzahl;    // for example 21
//loop and check $counter

foreach ($kunden as $tag) {
  $counter++;
  switch ($counter) {
    case ($counter >= $div_one):
        echo "html output with $tags for div one";
        break;
    case ($counter >= $div_two):
        echo "html output with $tags for div two";
        break;
    case ($counter >= $div_three):
        echo "html output with $tags for div three";
        break;
  }
}

this is untested written in the browser....but it should give you a option.

regards mr-fan

Link to comment
Share on other sites

Two uses for this solution -- part of a book back matter (indexing) and newspaper/magazine column layouts.

In a responsive design, you can have a single-column for mobile, two-column for tablet, and three-column for desktop -- similar to iNoize's attached image.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Oh it was much more easier  :blush:

$letter = strtoupper(mb_substr($tag->title, 0, 1, 'UTF-8'));

works now 

Hello, 

I have a strange output problem now. ÄÜÖ dont show correctly 

It looks so 

post-957-0-57416700-1454395777_thumb.png

What happens here and how to convert to the right chars ? 

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

Tnx 

Link to comment
Share on other sites

  • 3 years later...

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
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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 louisstephens
      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?
    • By nickngqs
      How do you guys sort according to the order of page listing?

      My template is sort order none, so I assume it would return according to order of page listing.
    • By rolspace.net
      Hi all!
      I have a little problem here. I want to sort a list of addresses by streetname and then by number. First, I just had a field "prod_objekt" (address) containing both and then have a foreach loop like this: $pages->get('/produzenten/')->prod_repeater->sort('prod_objekt') as $produzent. Unfortunately, this would not sort by streetnumber if I had the same streetname but multiple numbers.
      Now, I thought I could sort by two fields. prod_object (adress) first and then by prod_hausnummer (streetnumber). But I just don't know the code for it. I tried this: $pages->get('/produzenten/')->prod_repeater->sort('prod_objekt.prod_hausnummer') as $produzent, but it wouldn't work.
      Any ideas on how to solve this?
      Thanks for your help
      Roli
    • By celfred
      Hello,
      I'm facing an issue with my page fieldtype. Pages appear in the order they were created although I'd like to see them sorted in alphabetical order (by title). The pages are well-sorted in my tree, but in that particular Page fieldtype (on another page), I can't find any place where I would decide on what field I can sort them. Am I missing something here ?
      Let me know if I'm not clear enough in my explanations.
      Thanks in advance for a clue  
×
×
  • Create New...