Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

quick php/general programming question

Options
  • 16-01-2002 1:24pm
    #1
    Registered Users Posts: 1,862 ✭✭✭


    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