PDA

View Full Version : Finding the last item in a list using PHP


Martin
June 25th, 2007, 00:02
So in the absence of the rather useful CSS property ":last-child", what I'm in need of is a PHP way of finding out which item in a list is the last one, in order to add a class of "last" to that item.

Any help would be much appreciated. :)

inimino
June 25th, 2007, 00:08
It would help if you showed some code, i.e. how you are generating this list.

Martin
June 25th, 2007, 00:13
It's being generated via a WordPress plugin. I'm sure this won't be helpful, but the code for that is:


<?php txfx_wp_subpages(1, true, true, '<ul>', '</ul>', true); ?>


the parameters of which are as follows:

Show 1 page deep, show the parent, show the siblings, wrap the list in <ul></ul>, and echo the results.

inimino
June 25th, 2007, 00:27
That is very helpful. Since the output is being generated directly by a plugin (Tempus Fugit) your best approach is to modify the plugin. To do that you would need to find the function txfx_wp_subpages, which undoubtedly contains a loop which you can then modify to put a "last-item" class on the final <li>.

Martin
June 25th, 2007, 02:00
OK, here's the entirety of the plugin:


<?php
/*
Plugin Name: Subpage Listing
Plugin URI: http://txfx.net/code/wordpress/subpage-listing/
Description: Displays a directory-like listing of subpages where &lt;!--%subpages%--&gt; exists in the content of pages. It will be displayed if a page is blank. <code>txfx_wp_subpages()</code> can be used to display subpages in the sidebar. See this plugin's site for details.
Author: Mark Jaquith
Version: 0.6.1
Author URI: http://txfx.net
*/

/* Copyright 2005 Mark Jaquith (email: mark.gpl@txfx.net)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

function txfx_wp_subpage_display($text='', $depth=5, $show_parent=false, $show_siblings=false) {

if ( !is_page() ) return $text;

global $posts, $post;
$post = $posts[0];

if ( strpos($text, '<!--%subpages') !== false || is_array($text) || '' == $text || '' == $post->post_content || $post->post_content == "<br />\n" ) {

if ( !is_array($text) && strpos($text, '<!--%subpages') !== false ) {
preg_match('#<!--%subpages\(?([^)]*?)\)?%-->#i', $text, $matches);
if ( strlen($matches[1]) )
$params = explode(',', $matches[1]);
if ( isset($params[0]) ) $depth = $params[0];
if ( isset($params[1]) ) $show_parent = $params[1];
if ( isset($params[2]) ) $show_siblings = $params[2];
}

if ( $depth > 0 )
$subpage_text = wp_list_pages('child_of=' . $post->ID . '&depth=' . $depth . '&echo=0&title_li=0');

if ( $show_parent && $post->post_parent ) {
$parent = &get_post($post->post_parent);
$before = '<li class="page_item">&uarr;<a href="' . get_page_link($parent->ID) . '">' . wp_specialchars($parent->post_title) . '</a><ul>';
$after = '</ul></li>';
}

if ( $show_siblings ) {
$siblings = wp_list_pages('child_of=' . $post->post_parent . '&depth=1&echo=0&title_li=0');
if ( strpos($subpage_text, '</li>') !== false )
$subpage_text = preg_replace('#<li (.*?) href="' . get_permalink() . '"(.*?)</li>#i', '<li $1 href="' . get_permalink() . '"' . '$2<ul>' . $subpage_text . '</ul></li>', $siblings);
else
$subpage_text = preg_replace('#<li (.*?) href="' . get_permalink() . '"(.*?)</li>#i', '', $siblings);
}

// for the preformatted plugin, which will have wrapped the tag in a paragraph
$text = preg_replace('#<p><!--%subpages(.*?)%--></p>#i', '<!--%subpages$1%-->', $text);

if ( strpos($subpage_text, '</li>') === FALSE ) { // no subpages or siblings
if ( !$show_parent || !$post->post_parent )
return $text;
} else { // subpages or siblings exist
$output = "\n $subpage_text \n";
}

$output = $before . $output . $after; // add parent stuff

if ( !is_array($text) ) // if this is not called via txfx_wp_subpages()
$output = '<ul>' . $output . '</ul>';
else
return $output;

if ( strpos($text, '<!--%subpages') !== false )
return preg_replace('#<!--%subpages(.*?)%-->#', str_replace('$', '\$', $output), $text);
return $output;
}
return $text;
}


function txfx_wp_subpages($depth=5, $show_parent=false, $show_sibling=false, $before='<ul>', $after='</ul>', $echo=true) {
$subpages = txfx_wp_subpage_display(array(), $depth, $show_parent, $show_sibling);

if ( !$subpages )
return false;

$output = $before . $subpages . $after;

if ( !$echo )
return $output;
echo $output;
}


function txfx_wp_subpage_display_js() {
global $post_status;
if( strpos($_SERVER['REQUEST_URI'], 'page-new.php') || (strpos($_SERVER['REQUEST_URI'], 'post.php') && ($post_status == 'static')) ) : ?>
<script type="text/javascript">
<!--
function txfx_insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}

document.getElementById("quicktags").innerHTML += "<input type=\"button\" class=\"ed_button\" id=\"txfx_subpages\" value=\"Subpage List\" onclick=\"txfx_insertAtCursor(document.post.content, '\\n\\n<!--%subpages%-->\\n\\n');\" />";
//-->
</script>

<?php endif;
}


// For the quicktag button
add_filter('admin_footer', 'txfx_wp_subpage_display_js');

// doing it this way for compatibility with the Preformatted plugin
add_filter('init', create_function('$a', 'add_filter(\'the_content\', \'txfx_wp_subpage_display\', 9);'));

?>


I think I roughly know the section I'd need to change if I knew the syntax of what I need to do...heh

inimino
June 25th, 2007, 03:00
Hm, that code is a bit painful to look at.

You would need to do something with those two preg_replace() lines:

$subpage_text = preg_replace('#<li (.*?) href="' . get_permalink() . '"(.*?)</li>#i', '<li $1 href="' . get_permalink() . '"' . '$2<ul>' . $subpage_text . '</ul></li>', $siblings);

Currently it's doing the same thing on every <li>, that needs to be rewritten so that it does each <li> separately, keeps track of how many there are, and does something special on the last one. Alternately, you could add another step after this one that finds the last <li> and manipulates it, perhaps using explode() or strrpos().

Martin
June 25th, 2007, 03:23
Thanks, I appreciate the pointers, though I may leave it for when I've got a free 4 months or so to learn PHP. :)