You are subscribed to this thread displaying a number as four digits...

Discussion in 'PHP' started by benifactor, Dec 23, 2009.

  1. #1
    i would like to display numbers 0000 - 9999 and am having a problem.

    heres my code

    <?php

    $num = 0000;

    while ($num != 10000) {

    echo $num;

    $num++;

    }

    ?>

    this lists all the numbers. however it list it like: 0 1 2 3

    and i need it to list it like 0000, 0001, 0002, 0003 and so on.


    any help would be appreciated. thanks.
     
    benifactor, Dec 23, 2009 IP
  2. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #2
    try str_pad

    echo str_pad($num,4,"0");
     
    creativeGenius, Dec 23, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yup str_pad() is the easiest way to do it
     
    JAY6390, Dec 23, 2009 IP
  4. benifactor

    benifactor Peon

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    that worked only for 0000

    it then didnt show 0001, 002 all the wasy to 0999

    those didnt show up at all.
     
    benifactor, Dec 23, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This works fine for me (note I changed it to 20 and added the <br />
    $num = 0000;
    while ($num != 20) {
        echo str_pad($num, 4, '0',STR_PAD_LEFT).'<br />';
        $num++;
    }
    PHP:
     
    JAY6390, Dec 23, 2009 IP
  6. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #6
    try this then...

    for($num=0;$num<10000;$num++)
    {
    echo str_pad($num, 4, "0", STR_PAD_LEFT).'<br />';
    }
     
    creativeGenius, Dec 23, 2009 IP
  7. benifactor

    benifactor Peon

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    perfect, thank you.
     
    benifactor, Dec 23, 2009 IP
  8. benifactor

    benifactor Peon

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    this worked great, untill i tried to enter these nums into the database, they then turned back into the single digits again :(


    
    
    while ($num != 10000) {
    
    $num = str_pad($num, 4, '0',STR_PAD_LEFT);
    
    mysql_query("INSERT into table (id, num) VALUES ('null', '$num')", $l) or die(mysql_error());
    
    $num = $num + ;
    
    }
    
    
    PHP:

    i need it to hold those 4 digits into the database. any help????
     
    benifactor, Dec 25, 2009 IP
  9. benifactor

    benifactor Peon

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    never mind i will just change the var when it comes out of the db.
     
    benifactor, Dec 25, 2009 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You could change the column type to be varchar rather than the number format you have. It will never hold it otherwise
     
    JAY6390, Dec 25, 2009 IP