Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

ibrahim el shahawy
ibrahim el shahawy
627 Points

could my code be any better. on my very first project?

hello i started learning php by watching php course on youtube contents 200 videos. I coded this calculator that helps me in a side job. it basically calculates the total of a products purchased from the usa to be shipped to Egypt including (shipping fees+customs+ %5 of the product cost based on the product type) "not sure if I explained it enough :) . I used what i have learned in this 200 videos. here is the code, if someone can look at it and give me a feedback, would be so nice.

<?php

if (isset($_POST['i_price'])&&isset($_POST['i_weight'])&&isset($_POST['i_type'])&&isset($_POST['calc'])&&isset($_POST['s_type'])&&isset($_POST['d_rate'])) {

    $price =  $_POST['i_price'];
    $weight = $_POST['i_weight'];
    $type = $_POST['i_type'];
    $shipping = $_POST['s_type'];
    $d_rate = $_POST['d_rate'];
    $quan = $_POST['quan'];
    if(!empty($price)&&!empty($weight)&&!empty($type)&&!empty($shipping)&&!empty($_POST['d_rate'])){

        switch ($shipping) {
            case 'usps':
                switch ($weight) {
                    case 1:
                        $shipping_cost=35.29;
                        break;
                    case 2:
                        $shipping_cost=37.88;
                        break;
                    case 3:
                        $shipping_cost=41.55;
                        break;
                   case 4:
                        $shipping_cost=45.22;
                        break;
                    case 5:
                        $shipping_cost=48.91;
                        break;
                    case 6:
                        $shipping_cost=51.09;
                        break;
                    case 7:
                        $shipping_cost=54.33;
                        break;
                    case 8:
                        $shipping_cost=57.56;
                        break;
                    case 9:
                        $shipping_cost=60.80;
                        break;
                    case 10:
                        $shipping_cost=64.03;
                        break;
                    case 11:
                        $shipping_cost=67.17;
                        break;
                    case 12:
                        $shipping_cost=70.31;
                        break;
                    case 13:
                        $shipping_cost=73.46;
                        break;
                    case 14:
                        $shipping_cost=76.60;
                        break;
                    case 15:
                        $shipping_cost=79.74;
                        break;
                    case 16:
                        $shipping_cost=82.88;
                        break;
                    case 17:
                        $shipping_cost=86.02;
                        break;
                    case 18:
                        $shipping_cost=89.17;
                        break;
                    case 19:
                        $shipping_cost=92.31;
                        break;
                    case 20:
                        $shipping_cost=95.46;
                        break;
                    case 21:
                        $shipping_cost=97.52;
                        break;
                    case 22:
                        $shipping_cost=100.63;
                        break;
                    case 23:
                        $shipping_cost=103.74;
                        break;
                    case 24:
                        $shipping_cost=106.85;
                        break;

                    default:
                        break;
                }
                break;

            case 'aramex':
                switch ($weight) {
                    case 1:
                        $shipping_cost= 12;
                        break;
                    case 2:
                        $shipping_cost= 21;
                        break;
                    case 3:
                        $shipping_cost= 30;
                        break;
                    case 4:
                        $shipping_cost= 39;
                        break;
                    case 5:
                        $shipping_cost=48 ;
                        break;
                    case 6:
                        $shipping_cost= 57;
                        break;
                    case 7:
                        $shipping_cost= 66;
                        break;
                    case 8:
                        $shipping_cost= 75;
                        break;
                    case 9:
                        $shipping_cost=84 ;
                        break;
                    case 10:
                        $shipping_cost= 93;
                        break;
                    case 11:
                        $shipping_cost=102 ;
                        break;
                    case 12:
                        $shipping_cost= 111;
                        break;
                    case 13:
                        $shipping_cost=120 ;
                        break;
                    case 14:
                        $shipping_cost= 129;
                        break;
                    case 15:
                        $shipping_cost= 138;
                        break;
                    case 16:
                        $shipping_cost= 147;
                        break;
                    case 17:
                        $shipping_cost= 156;
                        break;
                    case 18:
                        $shipping_cost= 165;
                        break;
                    case 19:
                        $shipping_cost= 174;
                        break;
                    case 20:
                        $shipping_cost= 183;
                        break;
                    default:
                        break;
                }

                break;

            default:
                break;
        }


    switch ($type) {
        case 'Books':
            $type = .05;
            break;
        case 'Brake pads':
            $type = .20+20;
            break;
        case 'Camera Accessories':
            $type = .20;
            break;
        case 'Car spar parts':
            $type = .20;
            break;
        case 'Case-Bags':
            $type = .40;
            break;
        case 'Cataloges-magazines':
            $type = .20;
            break;
        case 'DVDs-CDs':
            $type = .10;
            break;
        case 'Clothes-Shoes':
            $type = .40;
            break;
        case 'Computer Equipmints':
            $type = .10;
            break;
        case 'Decorative arts':
            $type = .20;
            break;
        case 'digital cameras/video cameras':
            $type = .30;
            break;
        case 'electronics':
            $type = .20;
            break;
        case 'food/agricultural stuff':
            $type = .20+20;
            break;
        case 'GPS':
            $type = .20+20;
            break;
        case 'Ipad/3G Tablets':
            $type = .10+50;
            break;
        case 'Jewelry':
            $type = .15;
            break;
        case 'kitchenware':
            $type = .20;
            break;
        case 'laptops/Tablets':
            $type = .10;
            break;
        case 'makeup/perfuems/cosmetics':
            $type = .55;
            break;
        case 'Medical Equipments':
            $type = .15;
            break;
        case 'MIDI/Audio eqiupments':
            $type = .15;
            break;
        case 'mini laptop':
            $type = .10;
            break;
        case 'Mobile/cellphones':
            $type = .10+50;
            break;
        case 'cellphones accessories':
            $type = .10;
            break;
        case 'others':
            $type = .20;
            break;
        case 'Printers':
            $type = .10;
            break;
        case 'RC toys':
            $type = .20+35;
            break;
        case 'routers/wireless/RC':
            $type = .20+50;
            break;
        case 'online service':
            $type = 0+50;
            break;
        case 'shower panels/Massagers':
            $type = .40;
            break;
        case 'SLR Digital Cameras':
            $type = .20;
            break;
        case 'Sports Equipments':
            $type = .15;
            break;
        case 'Stationary':
            $type = .40;
            break;
        case 'Strollers':
            $type = .10;
            break;
        case 'Sunglasses':
            $type = .30;
            break;
        case 'Supplements':
            $type = .15+20;
            break;
        case 'textiles':
            $type = .40;
            break;
        case 'toys':
            $type = .20;
            break;
        case 'TVs':
            $type = .55;
            break;
        case 'Watch':
            $type = .20;
            break;


        default:
            break;
    }

     $price_quan = $price*$quan;
    if ($price_quan<=100) {
        $commission=5;
    } else {
        $commission=$price_quan* .05;
    }


    $customs=$price*$type;
    round (($cost = $price_quan + $shipping_cost + $commission + $customs),2);
    round (($shipping_cost),2);
    round (($commission),2);
    round (($customs),2);
    round (($total_egy = $cost*$d_rate),2);
    round (($deposit = $total_egy*0.5),2);

    echo 'Your order will cost you the following: <br> $'.$price.' item price + $'.$shipping_cost.' international shipping + $'.$commission.' our commission + $'.$customs.' Customs fees = $'.$cost.' = '.$total_egy.' EGY';

    }else {

    echo 'Please fill all fields!.';

} 
}

?>
<style>

table {
    border-collapse: collapse;
    width: 380px;
    padding: 30px;
    margin: 30px 30px;

    }




#calc {
    text-align: center;
}

#tab_resault {
    border: 1px solid black;
     border-collapse: collapse;

}

.tab_resault td {
        border: 1px solid black;
        margin: 2px;
        padding: 4px;
        width: 80%;
}
.tab_input {
     border: 1px solid black;
     border-collapse: collapse;
}
.tab_input td {
     border: 1px solid black;
     margin: 2px;
     padding: 4px;

}





</style>

<table>
    <tr>
        <td class="calc_resault">
            <table class="tab_resault">

                <tr>
                    <td class="res_name">  Total Cost in $$ is:</td>
                    <td class="res_num">  <?php echo round ($cost,2); ?></td>
                    </tr>
                    <tr>
                 <td class="res_name">   Shipping cost:</td>
                  <td class="res_num">  <?php echo round ($shipping_cost,2);?></td>
                    </tr>
                    <tr>
                <td class="res_name">    Commission:</td>
                  <td class="res_num">   <?php echo round ($commission,2);?></td>
                    </tr>
                    <tr>
                <td class="res_name">    Total in EGY:</td>
              <td class="res_num">       <?php echo round ($total_egy,2);?></td>
                    </tr>
                    <tr>
                <td class="res_name">    Deposit:</td>
                <td class="res_num">     <?php echo round ($deposit,2); ?></td>
                </tr>

            </table>
        </td>       
    </tr>



    <tr>
        <td>
            <table class="tab_input">
            <form action="index.php" method="POST">
                <tr><td> Item Price:</td><td> <input type="number" min="1" name="i_price" maxlength="4" step="0.01" <?php echo $price; ?>></td></tr>
                <tr><td>Quantity:</td><td><input type="number" value="1" min="1" name="quan" <?php echo $quan; ?>></td> </tr>
                <tr><td> Product Weight:</td><td> <input type="number" name="i_weight" max="70" min="1"  <?php echo $weight; ?>></td></tr>
               <tr><td>Product Type:</td><td> <select name="i_type"<?php echo $type; ?>>
                    <option>Books</option>
                    <option>Brake pads</option>
                    <option>Camera Accessories</option>
                    <option>Car spar parts</option>
                    <option>Case-Bags</option>
                    <option>Cataloges-magazines</option>
                    <option>DVDs-CDs</option>
                    <option>Clothes-Shoes</option>
                    <option>Computer Equipmints</option>
                    <option>Decorative arts</option>
                    <option>digital cameras/video cameras</option>
                    <option>electronics</option>
                    <option>food/agricultural stuff</option>
                    <option>GPS</option>
                    <option>Ipad/3G Tablets</option>
                    <option>Jewelry</option>
                    <option>kitchenware</option>
                    <option>laptops/Tablets</option>
                    <option>makeup/perfuems/cosmetics</option>
                    <option>Medical Equipments</option>
                    <option>MIDI/Audio eqiupments</option>
                    <option>mini laptop</option>
                    <option>Mobile/cellphones</option>
                    <option>cellphones accessories</option>
                    <option>others</option>
                    <option>Printers</option>
                    <option>RC toys</option>
                    <option>routers/wireless/RC</option>
                    <option>online service</option>
                    <option>shower panels/Massagers</option>
                    <option>SLR Digital Cameras</option>
                    <option>Sports Equipments</option>
                    <option>Stationary</option>
                    <option>Strollers</option>
                    <option>Sunglasses</option>
                    <option>Supplements</option>
                    <option>textiles</option>
                    <option>toys</option>
                    <option>TVs</option>
                    <option>Watch</option>

                   </select> </td></tr>   
               <tr><td>Shipping Method:</td><td> <select name="s_type"<?php echo $shipping; ?>>
                    <option value="usps">usps</option>
                    <option value="aramex">aramex</option>
               </select></td></tr>
               <tr><td>Dollar Rate: </td><td><input type="number" min="1" max="100" maxlength="10" name="d_rate" step="0.01" ></td></tr>


               <tr><td colspan="2" id="calc"><input type="submit" value="Calculate" name="calc"></td></tr>
            </form>
            </table><br>
        </td>
    </tr>
</table>

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

for your first project it is ok, but there are definitely thing with this code that could be done better. For instance, the aramex and ups switch statements are not needed. You could simply write a formulae to calculated $shipping_cost.

<?php
    function calc_aramex_cost($weight){
        return $weight * 9 + 3;
    }

That function will return the actual shipping cost.

You could also change your html form make your php simpler. In the name attribute of each input place the word product followed by the current name attribute wrapper in square brackets "[]".

    <input type="number" min="1" name="product[i_price]" maxlength="4" step="0.01" <?php echo $price; ?>>

This will take all the form elements and put them into an array called product which you can access from the $_POST variable.

<?php
    $product = $_POST['product'];

That should make thing easier by grouping everything together.

You might also want to consider setting default values for all of the input elements. By giving them a default by using the "value" attribute. Since you are using number inputs and select lists, giving them default values will remove the need to check to see if they exists or are set. In theory, you could remove this whole line of code:

<?php
    if( isset($_POST['i_price'])&&isset($_POST['i_weight'])&&isset($_POST['i_type'])&&isset($_POST['calc'])&&isset($_POST['s_type'])&&isset($_POST['d_rate'])) {

    $price =  $_POST['i_price'];
    $weight = $_POST['i_weight'];
    $type = $_POST['i_type'];
    $shipping = $_POST['s_type'];
    $d_rate = $_POST['d_rate'];
    $quan = $_POST['quan'];
    if(!empty($price)&&!empty($weight)&&!empty($type)&&!empty($shipping)&&!empty($_POST['d_rate'])){

All that being said, for a first project, if it works then you should be proud. Just remember that there is almost always ways to improve your code regardless of how long you've been coding. So please don't be discouraged if others point out things you should have or could have done differently.

ibrahim el shahawy
ibrahim el shahawy
627 Points

Thanks a lot. and yes it works. I will be here more often. Thanks again.