April 15, 201313 yr 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 !!
April 15, 201313 yr 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 } } ?>
April 16, 201313 yr 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!
April 17, 201313 yr 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
April 17, 201313 yr <?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