Help with snippet pdoAtoZ

This is a very nice snippet that outputs an A to Z list of resources. The benefit over SiteAtoZ is the use of templates for output and less html/css coded into the php.

See: Creating a Fast Loading A to Z List of Pages in MODX | PMACS Web Team MODX Blog | Perelman School of Medicine at the University of Pennsylvania

The problem with the snippet is they hard coded the page name in the php. I would like to explore making the snippet usable on any modx resource. Here is the section of the code that needs to be changed:

foreach ($alphaGroups as $alpha => $alphaGroup) {
	if (count($alphaGroup) == 0) {
		array_push($alphaIndex, $alpha);
	} else {
		array_push($alphaIndex, '<a href="a-to-z-list.html#' . $alpha . '">' . $alpha . '</a>');
		$formattedGroup = '';
		$formattedGroup .= '<h3 id="' . $alpha . '">' . $alpha . '</h3>'; 
		foreach ($alphaGroup as $item) {
			$formattedGroup .= $pdo->getChunk($itemTpl, $item);
			// uncomment the following line to see the item printed out in json format
			//$formattedGroup .= json_encode($item, JSON_PRETTY_PRINT);
		$output .= '<hr>' . $formattedGroup;

This is the line that needs editing:

array_push($alphaIndex, '<a href="a-to-z-list.html#' . $alpha . '">' . $alpha . '</a>');

I used

array_push($alphaIndex, '<a href="[[~[[*id]] ]]#' . $alpha . '">' . $alpha . '</a>');

Anybody have suggestions to change the this code and improve the code. Is there a better replacement for the link template tag.




You could also use the function makeUrl:


Or if you want to be able to set the id as a snippet property, then something like this:

//[[pdoAtoZ? &id=`12` ...]]
$id = $modx->getOption('id', $scriptProperties, $modx->resource->get('id'));
$url = $modx->makeUrl($id);
1 Like

You might give this a try:

$page = "";
array_push($alphaIndex, '<a href="{$page}#' . $alpha . '">' . $alpha . '</a>');  

/* existing code */
return $output;

That should display the AtoZ list on any page with the snippet tag.

FWIW, as long as you’re messing with the code, I’d replace the line above with this:

$alphaIndex[] =  '<a href="{$page}#' . $alpha . '">' . $alpha . '</a>';  

It’s shorter code, and should be about 50% faster than calling a function over and over.


Thank you Bobray and HalftrainedHarry. Great suggestions. I hope to make some changes to this snippet. I am testing out your suggested modifications for my current project.

My long term goal is to create output as follows

    ... list of links by letter ...

Letter top link>

<ul class="list-Class">
          ... list of item displayed by template ...

Above group repeated for each letter group

I have asked the Univ of Penn people If I may modify and share with MODX community.


1 Like