Professional Web Applications Themes

Can this code be improoved? If so how? - PHP Development

Can this code be improoved? If so how? I am new to php. This is one of my first scripts. Thank you <? define("TEXT_SHIP_TODAY", "<BR><B>Place your order today</B> before <B>4:00pm Est.</B><br>Your order will ship by 5:00pm <B>Today.</B>"); define("TEXT_SHIP_FRITD", "<BR><B>Place your order today</B> before <B>1:00pm Est.</B><br>Your order will ship by 2:00pm <B>Today.</B>"); define("TEXT_SHIP_TOMM", "<BR><B>Place your order today.</B><BR>Your order will ship by 5:00pm Est on"); define("TEXT_SHIP_WEEKEND", "<BR><B>Place your order today.</B><BR>Your order will ship by 5:00pm Est on"); define("HOLIDAY_SHIP_TEXT", "<BR><B>Due to the Holiday.</B><BR>If you place your order today.<BR>Your order will ship by 5:00pm Est on"); define("TEXT_SHIP_TODAY_FRIDAY", "<BR><B>Place your order today before 1:00pm Est.</B><BR>Your ...

  1. #1

    Default Can this code be improoved? If so how?

    Can this code be improoved? If so how?
    I am new to php. This is one of my first scripts.
    Thank you

    <?
    define("TEXT_SHIP_TODAY", "<BR><B>Place your order today</B> before
    <B>4:00pm Est.</B><br>Your order will ship by 5:00pm <B>Today.</B>");
    define("TEXT_SHIP_FRITD", "<BR><B>Place your order today</B> before
    <B>1:00pm Est.</B><br>Your order will ship by 2:00pm <B>Today.</B>");
    define("TEXT_SHIP_TOMM", "<BR><B>Place your order today.</B><BR>Your order
    will ship by 5:00pm Est on");
    define("TEXT_SHIP_WEEKEND", "<BR><B>Place your order today.</B><BR>Your
    order will ship by 5:00pm Est on");
    define("HOLIDAY_SHIP_TEXT", "<BR><B>Due to the Holiday.</B><BR>If you place
    your order today.<BR>Your order will ship by 5:00pm Est on");
    define("TEXT_SHIP_TODAY_FRIDAY", "<BR><B>Place your order today before
    1:00pm Est.</B><BR>Your order will ship by 5:00pm Est on");
    define("PERD", ".");
    $TDAY = date("D");
    $TME = date("G:i:s");
    $ORDER_WILL_SHIPA = date("l F jS");
    $ORDER_WILL_SHIPB = date("l F jS", time()+86400); //86400 = 1 day this is
    seconds
    $ORDER_WILL_SHIPC = date("l F jS", time()+172800);
    $ORDER_WILL_SHIPD = date("l F jS", time()+259200);
    $ORDER_WILL_SHIPE = date("l F jS", time()+345600);
    $ORDER_WILL_SHIPF = date("l F jS", time()+432000);
    $DELAYDAYFL = date("F j");
    //Define $IS_SHP_HOLIDAY true
    if ($DELAYDAYFL == 'Nov 23' || $DELAYDAYFL == 'May 27' || $DELAYDAYFL ==
    'Jul 1' || $DELAYDAYFL == 'Sep 2' || $DELAYDAYFL == 'Oct 7' || $DELAYDAYFL
    == 'Nov 8' || $DELAYDAYFL == 'Nov 24' || $DELAYDAYFL == 'Dec 23' ||
    $DELAYDAYFL == 'May 28' || $DELAYDAYFL == 'Jul 2' || $DELAYDAYFL == 'Sep 3'
    || $DELAYDAYFL == 'Oct 8' || $DELAYDAYFL == 'Nov 9' || $DELAYDAYFL == 'Nov
    25' || $DELAYDAYFL == 'Dec 24' || $DELAYDAYFL == 'May 29' || $DELAYDAYFL ==
    'Jul 3' || $DELAYDAYFL == 'Sep 4' || $DELAYDAYFL == 'Oct 9' || $DELAYDAYFL
    == 'Dec 25' || $DELAYDAYFL == 'Jan 1' || $DELAYDAYFL == 'May 30' ||
    $DELAYDAYFL == 'Jul 4' || $DELAYDAYFL == 'Sep 5' || $DELAYDAYFL == 'Oct 10'
    || $DELAYDAYFL == 'Dec 26' || $DELAYDAYFL == 'Jan 2') {${IS_SHP_HOLIDAY} =
    'true';}
    else $IS_SHP_HOLIDAY = 'false';
    //Define Regular Weekday ship days
    if ($TDAY == 'Mon' || $TDAY == 'Tue' || $TDAY == 'Wed' || $TDAY ==
    'Thu'){$STDSHIPWD = 'true';}
    else $STDSHIPWD = 'false' ;

    if ($DELAYDAYFL == 'Nov 23'){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT . ' ' .
    $ORDER_WILL_SHIPF;}
    if ($DELAYDAYFL == 'May 27' || $DELAYDAYFL == 'Jul 1' || $DELAYDAYFL == 'Sep
    2' || $DELAYDAYFL == 'Oct 7' || $DELAYDAYFL == 'Nov 8' || $DELAYDAYFL ==
    'Nov 24' || $DELAYDAYFL == 'Dec 23'){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT
    .. ' ' . $ORDER_WILL_SHIPE . PERD;}
    if ($DELAYDAYFL == 'May 28' || $DELAYDAYFL == 'Jul 2' || $DELAYDAYFL == 'Sep
    3' || $DELAYDAYFL == 'Oct 8' || $DELAYDAYFL == 'Nov 9' || $DELAYDAYFL ==
    'Nov 25' || $DELAYDAYFL == 'Dec 24'){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT
    .. ' ' . $ORDER_WILL_SHIPD . PERD;}
    if ($DELAYDAYFL == 'May 29' || $DELAYDAYFL == 'Jul 3' || $DELAYDAYFL == 'Sep
    4' || $DELAYDAYFL == 'Oct 9' || $DELAYDAYFL == 'Dec 25' || $DELAYDAYFL ==
    'Jan 1' ){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT . ' ' . $ORDER_WILL_SHIPC .
    PERD;}
    if ($DELAYDAYFL == 'May 30' || $DELAYDAYFL == 'Jul 4' || $DELAYDAYFL == 'Sep
    5' || $DELAYDAYFL == 'Oct 10' || $DELAYDAYFL == 'Dec 26' || $DELAYDAYFL ==
    'Jan 2') {${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT . ' ' . $ORDER_WILL_SHIPB .
    PERD;}

    if ($STDSHIPWD == 'true' && $IS_SHP_HOLIDAY == 'false' && $TME <=
    '16:00:00'){${ORDER_WILL_SHIP} = TEXT_SHIP_TODAY . ' ' . $ORDER_WILL_SHIPA .
    PERD;}
    if ($STDSHIPWD == 'true' && $IS_SHP_HOLIDAY == 'false' && $TME >
    '16:00:00'){${ORDER_WILL_SHIP} = TEXT_SHIP_TOMM . ' ' . $ORDER_WILL_SHIPB .
    PERD ;}
    //weekend shipping
    if ($TDAY == 'Fri' && $IS_SHP_HOLIDAY == 'false' && $TME >=
    '13:00:00'){${ORDER_WILL_SHIP} = TEXT_SHIP_FRITD . ' ' . $ORDER_WILL_SHIPA .
    PERD;}
    if ($TDAY == 'Fri' && $IS_SHP_HOLIDAY == 'false' && $TME <
    '13:00:00'){${ORDER_WILL_SHIP} = TEXT_SHIP_WEEKEND . ' ' . $ORDER_WILL_SHIPD
    .. PERD;}
    if ($TDAY == 'Sat' && $IS_SHP_HOLIDAY == 'false'){${ORDER_WILL_SHIP} =
    TEXT_SHIP_WEEKEND . ' ' . $ORDER_WILL_SHIPC . PERD;}
    if ($TDAY == 'Sun' && $IS_SHP_HOLIDAY == 'false'){${ORDER_WILL_SHIP} =
    TEXT_SHIP_WEEKEND . ' ' . $ORDER_WILL_SHIPB . PERD;}
    echo $ORDER_WILL_SHIP;
    ?>


    Michael Guest

  2. Moderated Post

    Default Re: Can this code be improoved? If so how?

    Removed by Administrator
    Kimmo Guest
    Moderated Post

  3. Moderated Post

    Default Re: Can this code be improoved? If so how?

    Removed by Administrator
    Daedalus.OS Guest
    Moderated Post

  4. #4

    Default Re: Can this code be improoved? If so how?

    And since I can't sleep (insomnia probably) here is a little more... using a
    simple function

    Instead of giving the $HOLIDAYS array values referring to code A through F,
    lets give it the number of day we have to add to current date. So the array
    would now look like this:
    $HOLIDAYS = array(
    'Nov 23'=>5, 'May 27'=>4, 'Jul 1'=>4,... // and so on
    );

    Now lets define a function that will deal with these days:

    function shippingDay($days){
    $shipDay = date("l F jS", time()+($days*24*60*60));
    return $shipDay;
    }

    Now lets rewrite the loop I posted earlier:

    if (isset($HOLIDAYS[$DELAYDAYFL])){
    $IS_SHP_HOLIDAY = true;
    $ORDER_WILL_SHIP = HOLIDAY_SHIP_TEXT .' '.
    shippingDay($HOLIDAYS[$DELAYDAYFL]);
    }else{
    $IS_SHP_HOLIDAY = false;
    }

    Later in your script if you would need to get a shipping date, all you need
    is to call this function and pass it the number of day to add (
    shippingDay(0) would mean today).

    Now see how this:
    #################################
    $ORDER_WILL_SHIPA = date("l F jS");
    $ORDER_WILL_SHIPB = date("l F jS", time()+86400); //86400 = 1 day this is
    seconds
    $ORDER_WILL_SHIPC = date("l F jS", time()+172800);
    $ORDER_WILL_SHIPD = date("l F jS", time()+259200);
    $ORDER_WILL_SHIPE = date("l F jS", time()+345600);
    $ORDER_WILL_SHIPF = date("l F jS", time()+432000);
    //Define $IS_SHP_HOLIDAY true
    if ($DELAYDAYFL == 'Nov 23' || $DELAYDAYFL == 'May 27' || $DELAYDAYFL ==
    'Jul 1' || $DELAYDAYFL == 'Sep 2' || $DELAYDAYFL == 'Oct 7' || $DELAYDAYFL
    == 'Nov 8' || $DELAYDAYFL == 'Nov 24' || $DELAYDAYFL == 'Dec 23' ||
    $DELAYDAYFL == 'May 28' || $DELAYDAYFL == 'Jul 2' || $DELAYDAYFL == 'Sep 3'
    || $DELAYDAYFL == 'Oct 8' || $DELAYDAYFL == 'Nov 9' || $DELAYDAYFL == 'Nov
    25' || $DELAYDAYFL == 'Dec 24' || $DELAYDAYFL == 'May 29' || $DELAYDAYFL ==
    'Jul 3' || $DELAYDAYFL == 'Sep 4' || $DELAYDAYFL == 'Oct 9' || $DELAYDAYFL
    == 'Dec 25' || $DELAYDAYFL == 'Jan 1' || $DELAYDAYFL == 'May 30' ||
    $DELAYDAYFL == 'Jul 4' || $DELAYDAYFL == 'Sep 5' || $DELAYDAYFL == 'Oct 10'
    || $DELAYDAYFL == 'Dec 26' || $DELAYDAYFL == 'Jan 2') {${IS_SHP_HOLIDAY} =
    'true';}
    else $IS_SHP_HOLIDAY = 'false';
    //Define Regular Weekday ship days
    if ($TDAY == 'Mon' || $TDAY == 'Tue' || $TDAY == 'Wed' || $TDAY ==
    'Thu'){$STDSHIPWD = 'true';}
    else $STDSHIPWD = 'false' ;

    if ($DELAYDAYFL == 'Nov 23'){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT . ' ' .
    $ORDER_WILL_SHIPF;}
    if ($DELAYDAYFL == 'May 27' || $DELAYDAYFL == 'Jul 1' || $DELAYDAYFL == 'Sep
    2' || $DELAYDAYFL == 'Oct 7' || $DELAYDAYFL == 'Nov 8' || $DELAYDAYFL ==
    'Nov 24' || $DELAYDAYFL == 'Dec 23'){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT
    .. ' ' . $ORDER_WILL_SHIPE . PERD;}
    if ($DELAYDAYFL == 'May 28' || $DELAYDAYFL == 'Jul 2' || $DELAYDAYFL == 'Sep
    3' || $DELAYDAYFL == 'Oct 8' || $DELAYDAYFL == 'Nov 9' || $DELAYDAYFL ==
    'Nov 25' || $DELAYDAYFL == 'Dec 24'){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT
    .. ' ' . $ORDER_WILL_SHIPD . PERD;}
    if ($DELAYDAYFL == 'May 29' || $DELAYDAYFL == 'Jul 3' || $DELAYDAYFL == 'Sep
    4' || $DELAYDAYFL == 'Oct 9' || $DELAYDAYFL == 'Dec 25' || $DELAYDAYFL ==
    'Jan 1' ){${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT . ' ' . $ORDER_WILL_SHIPC .
    PERD;}
    if ($DELAYDAYFL == 'May 30' || $DELAYDAYFL == 'Jul 4' || $DELAYDAYFL == 'Sep
    5' || $DELAYDAYFL == 'Oct 10' || $DELAYDAYFL == 'Dec 26' || $DELAYDAYFL ==
    'Jan 2') {${ORDER_WILL_SHIP} = HOLIDAY_SHIP_TEXT . ' ' . $ORDER_WILL_SHIPB .
    PERD;}
    #######################
    Turn into this:
    +++++++++++++++++++++
    $HOLIDAYS = array(
    'Nov 23'=>5, 'May 27'=>4, 'Jul 1'=>4,
    'Sep 2'=>4, 'Oct 7'=>4, 'Nov 8'=>4,
    'Nov 24'=>4, 'Dec 23'=>4, 'May 28'=>'D',
    'Jul 2'=>3, 'Sep 3'=>3, 'Oct 8'=>3,
    'Nov 9'=>3, 'Nov 25'=>3, 'Dec 24'=>3,
    'May 29'=>2, 'Jul 3'=>2, 'Sep 4'=>2,
    'Oct 9'=>2, 'Dec 25'=>2, 'Jan 1'=>2,
    'May 30'=>1, 'Jul 4'=>1, 'Sep 5'=>1,
    'Oct 10'=>1, 'Dec 26'=>1, 'Jan 2'=>1
    );
    function shippingDay($days){
    $shipDay = date("l F jS", time()+($days*24*60*60));
    return $shipDay;
    }
    if (isset($HOLIDAYS[$DELAYDAYFL])){
    $IS_SHP_HOLIDAY = true;
    $ORDER_WILL_SHIP = HOLIDAY_SHIP_TEXT . ' ' .
    shippingDay($HOLIDAYS[$DELAYDAYFL]);
    }else{
    $IS_SHP_HOLIDAY = false;
    }
    ++++++++++++++++++++++

    And some may have better ways than this one... Anyway I think you get the
    picture...
    Good learning.

    Dae


    Daedalus.OS Guest

  5. #5

    Default Re: Can this code be improoved? If so how?

    Michael spilled the following:
     
    <snip>

    Yes, there is a lot of room for improvement. I sincerely hope that you are
    new to programming.

    First, go read a good book on programming (make sure you find out about
    variables, arrays, and constants). Then go read the PHP manual. Than go
    read the PEAR style rules (at pear.php.net).

    It should be possible to write something to do the same thing as your
    example with less the 40% of the code you published and make it
    understandable.

    HTH

    C.
    Colin Guest

Similar Threads

  1. Why doesn't the Code Completion occur in FlexBuilder IDEwhen source code is in an external file?
    By DuffyVector123 in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: May 25th, 01:39 PM
  2. Custom control fires event but ignores some code in the code behind file
    By recoil@community.nospam in forum ASP.NET Building Controls
    Replies: 0
    Last Post: March 8th, 05:17 PM
  3. Replies: 0
    Last Post: February 9th, 05:26 PM
  4. [PHP] Wrapping code inside [code][/code] tags.
    By Php in forum PHP Development
    Replies: 2
    Last Post: October 6th, 09:34 AM
  5. Replies: 0
    Last Post: July 11th, 06:27 PM

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
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139