Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

quick php/general programming question

  • 16-01-2002 01:24PM
    #1
    Registered Users, Registered Users 2 Posts: 1,848 ✭✭✭


    Right down to it,
    incramenting a number is no hassel right, but i want to incrament a formatted number.

    say my number is "000001" it needs to be displayed as is, because it becomes part of an sql statement.

    so for each loop i want 1 to be added to this number, ie it will be "000002" "000003" and so on.
    $num = intval($docid)+1;
    if ($num>9)
       {
       $id = "0000" . $num ;
       #$id = printf("%5s", $num);
       }
    else
    $id =  "00000" . $num;
    

    should this work?
    its been anoying me now for a few hours and i need to get over this to move to the next step of the script.

    thanks in advance for your help,
    Flame


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Well this works, but it's not very elegant or scaleable.
    You should be able to translate pretty much directly
    [php]
    #!/usr/bin/perl -w

    my $i = 0;
    my $padding = "0000";

    for ($i = 0; $i <= 10000; $i++)
    {
    if ($i > 10000)
    {
    $padding = "";
    }
    elsif ($i > 1000)
    {
    $padding = "0";
    }
    elsif ($i > 100)
    {
    $padding = "00";
    }
    elsif ($i > 10)
    {
    $padding = "000";
    }
    else
    {
    $padding = "0000";
    }
    print $padding . $i . "\n";
    }

    [/php]


  • Closed Accounts Posts: 286 ✭✭Kev


    what about

    sprintf( "%06d", $i );

    for more about sprintf check out the php manual


Advertisement