Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Explode & Limit

Featured Replies

Hi just wondering if anybody can assist me in Limiting and explode ... basically it's a list of image refs seperated by comma .. the explode is working fine and showing all the image .. but i would like to limit it to 4.

 

Tried a few variations, but i kept getting a random image of a 404 page .. very strange !

Here is my code

 

<ul class="thumbs list-image clearfix">
               <?php 
                 $parts = explode(',', $row["imagerefs"]);
                 foreach($parts as $part) {
	        ?>
<li>
<a class="thumb" name="leaf" href="<?php echo $part; ?>" title="<?php echo $row["make"]; ?>">
<img src="<?php echo $part; ?>" alt="<?php echo $row["make"]; ?>" style="width:146px; height:88px;"/></a>
</li>
	        <?php
                     }
                ?>

 

Thanks folks !!

Try:

 

 

 

<?php
$parts = explode(',', $row["imagerefs"]);
$numImg = 0;
foreach($parts as $part) {
    $numImg++;

    if ($numImg <= 4) {
?>
    <li>
        <a class="thumb" name="leaf" href="<?php echo $part; ?>" title="<?php echo $row["make"]; ?>">
        <img src="<?php echo $part; ?>" alt="<?php echo $row["make"]; ?>" style="width:146px; height:88px;"/></a>
    </li>
<?php
    }
}
?>
 

An alternative method, for anyone interested - not that Lyndey's answer is incorrect by any means :)

<?php
$parts = explode(',', $row["imagerefs"]);
$numImg = 0;
foreach($parts as $part) :
    if (++$numImg > 4) break;
?>
    <li>
        <a class="thumb" name="leaf" href="<?php echo $part; ?>" title="<?php echo $row["make"]; ?>">
        <img src="<?php echo $part; ?>" alt="<?php echo $row["make"]; ?>" style="width:146px; height:88px;"/></a>
    </li>
<?php endforeach ?>

Slightly more efficient in that it prevents unnecessary loops if $parts happens to contain a large number of items. Also uses more shorthand for code tidy-up!

  • Author

This is maybe gonna sound a bit strange, but is it possible to exclude the first $ part found so instead of showing the first 4 images, show image 2,3,4,5 ?

 

I know it's a strange request, but i have my reasons lol


<?php
$parts = explode(',', $row["imagerefs"]);
unset($parts[0]); // Add this line
$numImg = 0;
foreach($parts as $part) :
if (++$numImg > 4) break;
?>
<li>
<a class="thumb" name="leaf" href="<?php echo $part; ?>" title="<?php echo $row["make"]; ?>">
<img src="<?php echo $part; ?>" alt="<?php echo $row["make"]; ?>" style="width:146px; height:88px;"/></a>
</li>
<?php endforeach ?>

 

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.