Jump to content

Help how to remove the last tag in a loop...


Zahari M.
 Share

Recommended Posts

Hi Guys!

Hoping some kind soul could help me out with my poor php abilities!

Ok... so my site is almost there. I have gotten nearly everything to work and am on the home stretch.

So... I have been trying to create a couple of functions that lists "page excerpts" for Child Pages and excerpts for "Pages Tagged with Tag X"...

Here is what it generally looks like...

Screen_Shot_2013_09_09_at_8_09_41_PM.png

What I would like to do is use a <hr /> tag to create a horizontal divider beneath each excerpt as you see above, BUT, not display one after the last excerpt.

So.... one of my functions goes like this:


    function html_TaggedPages(){
    $page = wire('page'); 
 

	 	$tps = wire('pages')->find("tags=$page,sort=sort");
	    
	  	$out = "<div class='posts'>";

	    foreach ($tps as $tp) {
		   $out .= "<p class='title'><a href='{$tp->url}'>{$tp->title}</a></p>";
		    $out .= "<p class='summary'>{$tp->summary}</p> " . "<hr />"; 
	    }

	$out = rtrim($out , " <hr />" ) ;

    $out .= " </div>";

	
    return $out;
    }

So I was hoping that, and this is the first time I'm using rtrim, that it would remove that last <hr />.

In fact it does and the function does work. But if we look at the source code of the page, the last closing paragraph tag is getting a bit chopped off too! The end tag gets rendered as </p

So... can anyone show me how I can rework this function to remove that last <hr /> and keep the last </p> tag intact? I've never used a counter before but suspect I might have to go that way. Any help much appreciated!

Cheers guys!
 

Link to comment
Share on other sites

This should work:

foreach ($tps as $tp) {
    $out .= "<p class='title'><a href='{$tp->url}'>{$tp->title}</a></p>";
    $out .= "<p class='summary'>{$tp->summary}</p> ";
    $out .=  $p === $tps->last() ?  "<hr />" : ""; // oops, see corrections on next post
}
 
  • Like 1
Link to comment
Share on other sites

Hi diogo!

Thanks for the reply and your help!

At first there were no horizontal dividers displayed at all!

But it got me thinking and after tweaking it and rearranging it.... it now works!

Yay!


        foreach ($tps as $tp) {
        $out .= "<p class='title'><a href='{$tp->url}'>{$tp->title}</a></p>";
        $out .= "<p class='summary'>{$tp->summary}</p> ";
        $out .=  ($tp === $tps->last()) ?  "" : "<hr />";
        }

Thanks a lot diogo for setting me on the right path!

Many thanks!

Cheers


 

Link to comment
Share on other sites

This would work:

$out = "<p class='summary'>{$tp->summary}</p> <hr/>"; 
$out = rtrim($out , "<hr/>" );
If you remove the space in the "<hr />" and the space before the " <hr/>" in the trim it works. Keep in mind, rtrim removes chars and not tags/strings, so what you specify in the second argument is actually a chars list and not a string to be removed. Also if doing this way, a space after </p> is required to make it work also.
 
Better stick with other methods in this case.
  • Like 1
Link to comment
Share on other sites

What about doing it in css instead of a php function?

.post {
   border-bottom: 1px solid #ccc;
}

.post:last-child {
   border-bottom: none;
}

Isn't that much much easier?

If you do this :

.post {

   border-top: 1px solid #ccc;

}

.post:first-child {

   border-top: none;

}

then ie7 is supported.

Somehow IE7 understands first-child, but not last-child

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...