SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating leading zeros

    Hi all,

    I need to create data in the following format...

    ST_COUNTY_ID

    I'm pulling the state, county and ID from my database. My ID needs to be reformatted so that there are always 6 digits in the ID.

    so, if ID=42, it needs to be changed to 000042.

    How do I add the correct amount of zeros to my new 6 digit ID number?

    Thanks for any help.
    No, I REALLY dislike having to use Joomla.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to say that i dont know about the specific function to do the task.

    How about this function. Please review this function that i made myself for retrieving the member id and its table name (which table is used to store the member's information)
    PHP Code:
        function GetNewMemberID(){
            
    $rsMem mysql_query("SELECT MAX(RIGHT(MemberID,5)) AS max_memid FROM tblmembers") or die(mysql_error());
            if(
    mysql_num_rows($rsMem) >= 1){
                
    $row mysql_fetch_array($rsMem);
                
    $MaxMemID $row['max_memid'];
                
    $rsTable mysql_query("SELECT * FROM tblmembers WHERE RIGHT(MemberID,5)='$MaxMemID'") or die(mysql_error());
                
    $rows mysql_fetch_array($rsTable);
                
    $MaxTableID $rows['MemberID'];
                
    $MaxTableID substr($MaxTableID,0,strlen($MaxTableID) - 5);
                
    //increasing the memberid by always 1
                
    $MaxMemID += 1;
                if(
    strlen($MaxMemID) == 1$MaxMemID "0000" $MaxMemID;
                if(
    strlen($MaxMemID) == 2$MaxMemID "000" $MaxMemID;
                if(
    strlen($MaxMemID) == 3$MaxMemID "00" $MaxMemID;
                if(
    strlen($MaxMemID) == 4$MaxMemID "0" $MaxMemID;
                
    //increasing tableid
                
    if($MaxTableID == 5$MaxTableID 1;
                else 
    $MaxTableID += 1;
                
                if(
    strlen($MaxTableID) == 1$MaxTableID "00" $MaxTableID;
                if(
    strlen($MaxTableID) == 2$MaxTableID "0" $MaxTableID;
                
    $newgenid $MaxTableID.$MaxMemID;
                return 
    $newgenid;
            }
            else{
                
    $newgenid "00100001";
                return 
    $newgenid;
            }
        } 
    If you could not understand about it, please let me know what you did not understand.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crowden View Post
    How do I add the correct amount of zeros to my new 6 digit ID number?

    Thanks for any help.
    You have to convert it to string -
    PHP Code:
    <?php
    $width 
    6;
    $str 43;
    if(
    strlen($str) < $width) {
        for (
    $i=0;$i<$width-strlen($str);$i++) $output .= "0";
    }
    $output .= $str;
    echo 
    $output;
    ?>

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $data 42;
    $data sprintf('%06s'$data);
    print 
    $data// 000042 
    Or
    PHP Code:
    $data 42;
    printf('%06s'$data); // 000042 
    http://php.net/sprintf
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    $data 42;
    $data sprintf('%06s'$data);
    print 
    $data// 000042 
    Or
    PHP Code:
    $data 42;
    printf('%06s'$data); // 000042 
    http://php.net/sprintf
    Thank you very much
    No, I REALLY dislike having to use Joomla.

  6. #6
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm what about str_pad?

    str_pad($number, 6, "0", STR_PAD_LEFT)
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •