syclonefx Posted May 24, 2008 Share Posted May 24, 2008 I am trying to get a double digit return. I am building a image gallery and I have a random number of image. The images are named feat_01.jpg thru feat_25.jpg. The out put of my code is: <div class="thumb"><a href="images/feat_1.jpg" class="feature">image1</a></div> I want this out put: <div class="thumb"><a href="images/feat_01.jpg" class="feature">image01</a></div> Is it possible to get a double digit return? I know I could change my echo statement to echo '<div class="thumb"><a href="images/feat_0' . $i . '.jpg" class="feature">image0'. $i .'</a></div>'; This would work for 0 - 9, but if I do that once I get over 9 images this won't work. <?php $i = 01; while ($i <= 25): echo '<div class="thumb"><a href="images/feat_' . $i . '.jpg" class="feature">image'. $i .'</a></div>'; $i++; endwhile; ?> Link to comment https://forums.phpfreaks.com/topic/107034-solved-formating-numbers-help/ Share on other sites More sharing options...
serverman Posted May 24, 2008 Share Posted May 24, 2008 try... this isnt tested tho just a guess i want to try to help out <?php $i = 1; $n = 0; while ($i <= 25): $n = $n + 1; echo '<div class="thumb"><a href="images/feat_' . $n . '.jpg" class="feature">image'. $n .'</a></div>'; $i++; endwhile; ?> i don't know if this will work tho (i Don't use loops that much and iim kinda new) Link to comment https://forums.phpfreaks.com/topic/107034-solved-formating-numbers-help/#findComment-548686 Share on other sites More sharing options...
sasa Posted May 24, 2008 Share Posted May 24, 2008 <?php $i = 01; while ($i <= 25): printf( '<div class="thumb"><a href="images/feat_%02d.jpg" class="feature">image%d</a></div>',$i,$i); $i++; endwhile; ?> Link to comment https://forums.phpfreaks.com/topic/107034-solved-formating-numbers-help/#findComment-548687 Share on other sites More sharing options...
syclonefx Posted May 24, 2008 Author Share Posted May 24, 2008 Thanks sasa. That worked. Link to comment https://forums.phpfreaks.com/topic/107034-solved-formating-numbers-help/#findComment-548690 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.