SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Padding with zero's

    I have a directory full of wallpaper images for a project I'm working on. The images are named in this format "001_1024.jpg", the "001" being the wallpaper ID, and the "1024" being the wallpaper resolution.

    So I created a database which would store information about each wallpaper image (title, description, categories, ID). I also created a form that will allow me to input information about a new wallpaper easily.

    One of the fields is Wallpaper ID. So I type in "001" and sure enough, the zeros don't make it into the database. Is there a way to keep the zeros intact?

    If not, I can just use a sprintf() when I access the wallpaper later through a different script. But it sure would be nice if I could keep the zeros in. Any ideas?
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The leading zeros are inconsequential except for display so it is a waste of memory to store them.

  3. #3
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by 7stud
    Hi,

    The leading zeros are inconsequential except for display so it is a waste of memory to store them.
    I agree with 7stud.

    Just store the ID as integers and when you have to call the image just write a function to add the missing zeros.

    Paul

  4. #4
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks guys, I'll do that...

    But just for the know-how, is that possible?
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  5. #5
    SitePoint Enthusiast RandMan's Avatar
    Join Date
    Mar 2001
    Location
    Crossville, TN USA
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will pad your number with leading zeros. The leading zeros will not appear in a number greater than 99:
    Code:
    $id = 1;
    $res = 1024;
    $file = substr("00$id",-3) . "_" . $res . ".jpg";

  6. #6
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that does the same thing as sprintf() would do. But thanks anyway guys, it's no big deal.
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  7. #7
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this one when you want to recall the images

    PHP Code:
    if ($id && $id 10
       
    $zeros "00";
    elseif (
    $id && $id 100
       
    $zeros "0";

       
    $file "$zeros$id"."_"."$resolution.jpg" 
    Paul

  8. #8
    SitePoint Member imalliques's Avatar
    Join Date
    Jun 2000
    Location
    Belgrade, Yugoslavia
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!
    All replies are excellent, but as I know (I'm not IKE, so the mistakes are possible and present, too), AutoNumber INCREMENT cannot contains leading zeros, you must add them, as they suggest. But did you consider other thing: I agree that db can be as smal as possible, but if you identify your items with numberss (ie 001_1024), why don't you add column 'ImgNo'
    mysql > alter table [table name] add column imgno varchar(3);
    to store 'id\'s' for your data?

    HTH
    Let the Source be with You!

  9. #9
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The best way would be to instruct MySQL to pad the values in your SELECT query using MySQL's LPAD function:

    SELECT LPAD(wallpaperid_col, 3, "0") FROM wallpaper_tbl
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  10. #10
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTH: That is exactly what I did. I have a column called wallpaperID which only stores the unique ID of the given wallpaper.

    Kevin: Thanks! That is exactly what I was looking for. I will probably go with the sprintf() route, but LPAD() was what I was looking for.
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  11. #11
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Actually, it turns out you can do exactly what you need without using functions at all:

    Code:
    CREATE TABLE WALLPAPER (
      WallpaperID INT(3) ZEROFILL NOT NULL
      ...
    )
    Enjoy!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


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
  •