Zahari M. Posted September 9, 2013 Share Posted September 9, 2013 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... 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 More sharing options...
diogo Posted September 9, 2013 Share Posted September 9, 2013 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 } 1 Link to comment Share on other sites More sharing options...
Zahari M. Posted September 9, 2013 Author Share Posted September 9, 2013 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 More sharing options...
diogo Posted September 9, 2013 Share Posted September 9, 2013 Sorry, wrote it on the browser and seems that I forgot a "t" edit: ... and inverted the order on the condition Link to comment Share on other sites More sharing options...
Soma Posted September 9, 2013 Share Posted September 9, 2013 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. 1 Link to comment Share on other sites More sharing options...
3fingers Posted September 9, 2013 Share Posted September 9, 2013 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? 1 Link to comment Share on other sites More sharing options...
Zahari M. Posted September 9, 2013 Author Share Posted September 9, 2013 Thanks Soma and 3fingers! Great to know that we can achieve it this way too if we need to! Your all awesome! Cheers guys! Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 9, 2013 Share Posted September 9, 2013 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 More sharing options...
Recommended Posts