#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    385
    Rep Power
    16

    Four digit incremented ... string! "0001"


    i want the final file no. to be put in database to be four digits like "0001", "0002", .... "0010", but the code below doesn't work as i wish

    PHP Code:

    $file_no_int 
    3;
    settype($file_no_int"string");


    if (
    strlen($file_no_int)==1) { $file_no_int == "000" $file_no_int; }
    if (
    strlen($file_no_int)==2) { $file_no_int == "00" $file_no_int; }
    if (
    strlen($file_no_int)==3) { $file_no_int == "0" $file_no_int; }
    if (
    strlen($file_no_int)==4) { $file_no_int == "" $file_no_int; }

    echo 
    $file_no_int;                // it gives result       3    not   0003 
    what to do?
    yours,
    George
  2. #2
  3. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    15
    original:

    PHP Code:

    $file_no_int 
    3;
    settype($file_no_int"string");


    if (
    strlen($file_no_int)==1) { $file_no_int == "000" $file_no_int; }
    if (
    strlen($file_no_int)==2) { $file_no_int == "00" $file_no_int; }
    if (
    strlen($file_no_int)==3) { $file_no_int == "0" $file_no_int; }
    if (
    strlen($file_no_int)==4) { $file_no_int == "" $file_no_int; }

    echo 
    $file_no_int;                // it gives result       3    not   0003 
    change to:

    PHP Code:

    $file_no_int 
    3;


    if (
    strlen($file_no_int)==1) { $file_no_int "000" $file_no_int; }
    if (
    strlen($file_no_int)==2) { $file_no_int "00" $file_no_int; }
    if (
    strlen($file_no_int)==3) { $file_no_int "0" $file_no_int; }
    if (
    strlen($file_no_int)==4) { $file_no_int "" $file_no_int; }

    echo 
    $file_no_int;                // it gives result       3    not   0003 

    Comments on this post

    • georgeatef agrees : great catching eye :) thanks a lot
  4. #3
  5. Permanently Banned
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2006
    Location
    In a whale
    Posts
    4,127
    Rep Power
    0
    Would not strpad be easier than reinventing the wheel?

    Comments on this post

    • SimonGreenhill agrees : or put a ZEROFILL condition on the database field.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    385
    Rep Power
    16
    yes, this is what i was looking for from the begining, it will stretch my code to one line:

    PHP Code:
    $file_no_int str_pad($file_no_int4"0"STR_PAD_LEFT); 
    thanks a lot.
    yours,
    George
  8. #5
  9. Hockey face
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    St. Catharines, Canada
    Posts
    8,142
    Rep Power
    1321
    as Simon says, use zerofill right at the database level, stops you from needing unnecessary code.

    Comments on this post

    • edman007 agrees
    • jaeSun agrees : yea, the DB would be much easier

IMN logo majestic logo threadwatch logo seochat tools logo