JSHINER Posted May 6, 2008 Share Posted May 6, 2008 <?php foreach($page['categories'] as $cat) { echo '<a href="page.php?category=', $cat['id_cat'], '">', $cat['name'], '</a> | '; } ?> Displays "Category 1 | Category 2 | Category 3 |" How can I get rid of that last " | " Quote Link to comment https://forums.phpfreaks.com/topic/104389-solved-foreach-problem-simple-question/ Share on other sites More sharing options...
947740 Posted May 6, 2008 Share Posted May 6, 2008 Throw an if() statement in there to check if it is Category 3. If it is, do not echo the |, if it is not, echo the |. Quote Link to comment https://forums.phpfreaks.com/topic/104389-solved-foreach-problem-simple-question/#findComment-534409 Share on other sites More sharing options...
JSHINER Posted May 6, 2008 Author Share Posted May 6, 2008 I need it to work if there's 3 categories of 100. Just need it to not display on the last one. Quote Link to comment https://forums.phpfreaks.com/topic/104389-solved-foreach-problem-simple-question/#findComment-534411 Share on other sites More sharing options...
947740 Posted May 6, 2008 Share Posted May 6, 2008 You could check to find the total number of Categories and then use the if() statement. Quote Link to comment https://forums.phpfreaks.com/topic/104389-solved-foreach-problem-simple-question/#findComment-534419 Share on other sites More sharing options...
moselkady Posted May 6, 2008 Share Posted May 6, 2008 Maybe this works for you: <?php foreach($page['categories'] as $cat) $x[] = '<a href="page.php?category=' . $cat['id_cat'] . '">' . $cat['name'] . '</a>'; echo join(' | ', $x); ?> Quote Link to comment https://forums.phpfreaks.com/topic/104389-solved-foreach-problem-simple-question/#findComment-534434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.