dsbpac Posted January 10, 2013 Share Posted January 10, 2013 The following code will print out each product name from start to finish. How can i make it print out the same list and let me specify how it prints out for the first and last as their own code and still keep this code as the middle? <?php foreach ($products as $row): ?> <li><a href="product_details.php?product_id=<?php echo $row['product_id']; ?>"><?php echo $row['product_name']; ?></a></li> <?php endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/272970-php-foreach-count/ Share on other sites More sharing options...
premiso Posted January 10, 2013 Share Posted January 10, 2013 (edited) <?php $cnt = count($products); $i=0; foreach ($products as $row): if ($i == 0): ?> <?php elseif (($i+1) == $cnt): ?> <?php else: ?> <li><a href="product_details.php?product_id=<?php echo $row['product_id']; ?>"><?php echo $row['product_name']; ?></a></li> <?php endif; $i++; endforeach; ?> Something like that would work, probably a better way to do it, but without an example of what you want the first and last match to display / how it is changed, that is the best I have right now. Edited January 10, 2013 by premiso Quote Link to comment https://forums.phpfreaks.com/topic/272970-php-foreach-count/#findComment-1404777 Share on other sites More sharing options...
requinix Posted January 10, 2013 Share Posted January 10, 2013 If $products is an actual array (and not a Traversable object) then you can array_shift to get the first and array_pop to get the last. <?php $first = array_shift($products); $last = array_pop($products); ?> <?php if ($first): ?> <li>First: <a href="product_details.php?product_id=<?php echo $first['product_id']; ?>"><?php echo $first['product_name']; ?></a></li> <?php endif; ?> <?php foreach ($products as $row): ?> <li><a href="product_details.php?product_id=<?php echo $row['product_id']; ?>"><?php echo $row['product_name']; ?></a></li> <?php endforeach; ?> <?php if ($last): ?> <li>Last: <a href="product_details.php?product_id=<?php echo $last['product_id']; ?>"><?php echo $last['product_name']; ?></a></li> <?php endif; ?> Quote Link to comment https://forums.phpfreaks.com/topic/272970-php-foreach-count/#findComment-1404786 Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 I'd advice removing all of those, quite unnecessary, PHP tags. Not only is there no need to jump in and out of PHP mode all of the time, but it's also quite detrimental to both the readability and the performance of the code. This is how I'd write it: <?php // Move the first and last item out of the products array. $first = array_shift ($products); $last = array_pop ($products); // Create the output template for the item list, and initialize the output variable. $itemTemplate = '<li>%3$s<a href="product_details.php?product_id=%1$d">%2$s</a></li>'."\n"; $itemList = ''; if ($first) { $itemList .= sprintf ($itemTemplate, $first['product_id'], htmlspecialchars ($first['product_name']), 'First: '); } foreach ($products as $row) { $itemList .= sprintf ($itemTemplate, $row['product_id'], htmlspecialchars ($row['product_name']), ''); } if ($last) { $itemList .= sprintf ($itemTemplate, $last['product_id'], htmlspecialchars ($last['product_name']), 'Last: '); } Then you just need to echo out the contents of the $itemList variable, wherever you want the actual list. Quote Link to comment https://forums.phpfreaks.com/topic/272970-php-foreach-count/#findComment-1404899 Share on other sites More sharing options...
premiso Posted January 11, 2013 Share Posted January 11, 2013 I'd advice removing all of those, quite unnecessary, PHP tags. From his post, I am taking it he is working in the view section. Which is proper, in my opinion. So they are not really "unnecessary" that is just how views usually look. If, however, he only needs to change or add a class to it, it is really much simpler than what was posted, just need a little more clarification from the OP on that. Quote Link to comment https://forums.phpfreaks.com/topic/272970-php-foreach-count/#findComment-1404979 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.