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

×
×
  • Create New...