www.webdeveloper.com
Results 1 to 7 of 7

Thread: Starting numbers in for loop in php with 00 characters

  1. #1
    Join Date
    Dec 2012
    Posts
    14

    Starting numbers in for loop in php with 00 characters

    I need for loop to display images..
    ..
    but image name must start with 00... like 001.jpg, 002.jpg
    .
    I write my code like this..
    for ($i = 001; $i <= 10; $i++) {

    echo "<img src='Ony/Ony-Small/'.str_pad($i, 3, '0', STR_PAD_LEFT);.'.jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>";

    }
    ..
    but this is not working
    ..
    can anybody suggest me the right way

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,876
    Try using something to format your number. Look up (in the PHP manual) the "sprintf()" function.

    http://php.net/manual/en/function.sprintf.php
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,281
    I think it's just a quoting issue: you need to close your initial double-quoted string before executing the str_pad(), then reopen it after it (with the concatenation, of course).

    I noticed that after taking a closer look when this worked fine for me:
    Code:
    09:57 $ php -a
    Interactive shell
    
    php > for($i=0; $i<=10; $i++) {
    php { echo str_pad($i, 3, '0', STR_PAD_LEFT).PHP_EOL;
    php { }
    000
    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    php >
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,281
    PS: Might be cleaner to do the str_pad first?
    PHP Code:
    for ($i 001$i <= 10$i++) {
        
    $number str_pad($i3'0'STR_PAD_LEFT);
        echo 
    "<img src='Ony/Ony-Small/$number.jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>";

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Apr 2017
    Posts
    70
    Alternate simple clean solution

    PHP Code:
    <?php
    foreach (range(0,10) as $d)
    {
    echo 
    "<img src='Ony/Ony-Small/".sprintf('%02d'$d).".jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>\n";
    }
    ?>
    Last edited by benanamen; 11-27-2017 at 12:18 PM.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,281
    Quote Originally Posted by benanamen View Post
    Alternate simple clean solution

    PHP Code:
    <?php
    foreach (range(0,10) as $d)
    {
    echo 
    "<img src='Ony/Ony-Small/".sprintf('%02d'$d).".jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>\n";
    }
    ?>
    I think '%03d' is what's really wanted here, but in any case, another good option -- could even just replace the echo with printf() if you wanted. TMTOWTDI
    PHP Code:
    printf("<img src='Ony/Ony-Small/%03d.jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>\n"$d); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Join Date
    Apr 2017
    Posts
    70
    Yep, NogDog, it should be %03d. Looking back, the OP also wanted to start at 001, not 000.

    Corrected Code

    PHP Code:
     <?php
    foreach (range(1,10) as $d)
    {
    echo 
    "<img src='Ony/Ony-Small/".sprintf('%03d'$d).".jpg' class='img-responsive mx-auto d-block center-block img-thumbnail'>\n";
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"