October 2004
Mon Tue Wed Thu Fri Sat Sun
<<  <   >  >>
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Categories

powered by

Valid XHTML 1.0!

09/21/04

11:31:22 am, Categories: Wonky Code, 130 words  

Probably the worse way to pad a string...

Everyday I find code examples that make me go WTF. This is a prime example of one. It is a function that takes an item's id number and generates a left zero padded string with the id number.

I guess nobody told them about the str_pad() function.


function getVanPic($item_id) {
    $pic_path = "/path/to/pic/dir";
    if ($item_id >= 1000000) {
        $strPicName = $item_id;
    }
    else if ($item_id >= 100000) {
        $strPicName = "0".$item_id;
    }
    else if ($item_id >= 10000) {
        $strPicName = "00".$item_id;
    }
    else if ($item_id >= 1000) {
        $strPicName = "000".$item_id;
    }
    else if ($item_id >= 100) {
        $strPicName = "0000".$item_id;
    }
    else if ($item_id >= 10) {
        $strPicName = "00000".$item_id;
    }
    else {
        $strPicName = "000000".$item_id;
    }
    $strPicName1 = "w".$strPicName.".jpg";
    if (file_exists($pic_path."/".$strPicName1)) {
        return "van/".$strPicName1;
    }
    $strPicName1 = "W".$strPicName.".JPG";
    if (file_exists($pic_path."/".$strPicName1)) {
        return "van/".$strPicName1;
    }
    return "";
}

Trackback address for this post:

http://thephpwtf.com/htsrv/trackback.php?tb_id=26

Comments, Trackbacks, Pingbacks:

No Comments/Trackbacks/Pingbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.
Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, a, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
URLs, email, AIM and ICQs will be converted automatically.
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)