Jump to content

PHP foreach count


dsbpac

Recommended Posts

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; ?>

Link to comment
Share on other sites

<?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 by premiso
Link to comment
Share on other sites

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; ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.