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.

php mysql format question

  • 02-03-2007 10:40AM
    #1
    Registered Users, Registered Users 2 Posts: 648 ✭✭✭


    hi

    i have a field in my db called orderid

    if i pull a value from it for example 33 how do i format it with php to show
    00000033 ?

    Tnx


Comments

  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    something like ...

    str_pad("33", 10, "0", STR_PAD_LEFT);


    make yourself a little function and hey presto ...

    function pad0($number,$zeros="0")
    {
    return str_pad($number, $zeros, "0", STR_PAD_LEFT);
    }

    pad0("33",10);


  • Registered Users, Registered Users 2 Posts: 6,651 ✭✭✭daymobrew


    sprintf.
    $formatted_str = sprintf( '%08d', $num_from_db );
    


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    You can also tell MySQL to format the data when it selects it

    SELECT LPAD(`ID_Column`,8,'0') FROM TABLE....

    Will add the '0' character onto ID_Column, up to a maximum of 8 characters.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    you can also have this setup in your database start id 0000001


Advertisement