El Presidente
El Presidente

Reputation: 21

How do I shorten my code in php using different condition?

I have problem in using this long sourcecode. Is there any substitute code or condition statement to shorten or minimize the code. I use the if-ElseIf Statement. Thank you!

I actually doing a system regarding on multiple conditions regarding on the specified amount. Ex. If SHARE equal or less than 30,000 their is a specified percentage computed with different months.

<div class="span8" align="center">
    <table width="100%">
            <tr>
                <td align="center" width="50%"> 
                <form action="" method="POST">
                <label> *NOTE: Php.36,000.00 - maximum applied loanable amount</label>
                <br/>
                <label>Share Capital:</label>
                <input onBlur="this.value=formatCurrency(this.value)" type="hidden"> 
                <input type="text" name="share" value="0" class="textbox"/><br>
                <label>Term:</label>
                <select name="terms"/>
                        <option value="1">1 Month</option>
                        <option value="2">2 Months</option>
                      </select> <br /> 
                <input type="submit" name="calculate" class="btn btn-success" value="Let's COMPUTE">
                </form></td>

                <td width="50%" align="justify" > 
                    <div class="alert alert-info" align="justify" style="font-size:16px">
                <?php if(isset($_POST['calculate'])) { 
                        $share = $_POST['share']; 
                        $terms = $_POST['terms']; 
                        ?>
                <?php if($share <= "30000"){ ?>
                        <?php if($terms == "1"){ 
                            $interest = $share * .0130;                     
                            $service_fee =  $share * .01;
                            $filling_fee = 30;
                            $cash_on_hand =  $share - ($service_fee + $interest + $filling_fee);
                            $loan_receivable =  $cash_on_hand + $service_fee + $interest + $filling_fee;
                            $debit = $loan_receivable;
                            $credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
                            ?>
                            <table width="100%">
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
                                    <td width="30%" align="right"> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Interest "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
                                </tr>

                                <tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
                                    <td width="40%" align="right"><?php echo "TOTAL "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
                                    <td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
                                </tr>
    <tr>
    <td><br/><br/></td>
    <td><br/><br/></td>
    <td><br/><br/></td>
    </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Term: "; ?></td>
                                    <td width="30%" align="right"><?php echo $terms. " month "; ?> </td>
                                </tr>
                            </table>

                            <?php } elseif($terms == "2"){ 
                            $interest = $share * .0195;                     
                            $service_fee =  $share * .01;
                            $filling_fee = 30;
                            $cash_on_hand =  $share - ($service_fee + $interest + $filling_fee);
                            $loan_receivable =  $cash_on_hand + $service_fee + $interest + $filling_fee;
                            $debit = $loan_receivable;
                            $credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
                            ?>
                            <table width="100%">
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
                                    <td width="30%" align="right"> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Interest "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
                                </tr>

                                <tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
                                    <td width="40%" align="right"><?php echo "TOTAL "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
                                    <td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
                                </tr>
    <tr>
    <td><br/><br/></td>
    <td><br/><br/></td>
    <td><br/><br/></td>
    </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Term: "; ?></td>
                                    <td width="30%" align="right"><?php echo $terms. " months "; ?> </td>
                                </tr>
                            </table>            


    <!--END OF SHARE 30,000 BELOW -->   <?php } ?>
    <!--START OF SHARE 30,000 ABOVE --> <?php } elseif($share >= "30001"){ ?>
                            <?php if($terms == "1"){ 
                            $interest = $share * .0130;                     
                            $service_fee =  $share * .01;
                            $filling_fee = 30;
                            $cash_on_hand =  $share - ($service_fee + $interest + $filling_fee);
                            $loan_receivable =  $cash_on_hand + $service_fee + $interest + $filling_fee;
                            $debit = $loan_receivable;
                            $credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
                            ?>
                            <table width="100%">
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
                                    <td width="30%" align="right"> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Interest "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
                                </tr>

                                <tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
                                    <td width="40%" align="right"><?php echo "TOTAL "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
                                    <td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
                                </tr>
    <tr>
    <td><br/><br/></td>
    <td><br/><br/></td>
    <td><br/><br/></td>
    </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Term: "; ?></td>
                                    <td width="30%" align="right"><?php echo $terms. " month "; ?> </td>
                                </tr>
                            </table>

                            <?php } elseif($terms == "2"){ 
                            $interest = $share * .0205;                     
                            $service_fee =  $share * .01;
                            $filling_fee = 30;
                            $cash_on_hand =  $share - ($service_fee + $interest + $filling_fee);
                            $loan_receivable =  $cash_on_hand + $service_fee + $interest + $filling_fee;
                            $debit = $loan_receivable;
                            $credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
                            ?>
                            <table width="100%">
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
                                    <td width="30%" align="right"> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Interest "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
                                </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
                                    <td width="30%" align="right"> </td>
                                    <td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
                                </tr>

                                <tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
                                    <td width="40%" align="right"><?php echo "TOTAL "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
                                    <td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
                                </tr>
    <tr>
    <td><br/><br/></td>
    <td><br/><br/></td>
    <td><br/><br/></td>
    </tr>
                                <tr>
                                    <td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
                                    <td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
                                </tr>

                                <tr>
                                    <td width="40%" align="justify"><?php echo "Term: "; ?></td>
                                    <td width="30%" align="right"><?php echo $terms. " months "; ?> </td>
                                </tr>
                            </table>
                        <?php } ?>  
                        <?php } ?> 
                    <?php } ?> 
    </div>          </td>
            </tr>
        </table>

Upvotes: 0

Views: 93

Answers (2)

Rasclatt
Rasclatt

Reputation: 12505

I think what you could do here is create a function (or have this as part of a set of tools saved in a class) that you can make available for reuse via an include(). To mix the algorithm into your html will severely limit it's possible use elsewhere. It also clutters up your html output when you intersperse it. Creating a function/class->method allows for expandability and porting into other functions/class->methods as well:

<?php
// Create a function that can accept parameters
function CalculateItem($share = 0,$terms = 0)
    {
        if($share <= 30000)
            $multiplier =   ($terms == "1")? .0130 : .0195;
        elseif($share >= 30001)
            $multiplier =   ($terms == "1")? .0130 : .0205;

        if(empty($multiplier))
            return;

        $data['share']              =   $share;
        $data['terms']              =   $terms; 
        $data['interest']           =   ($share * $multiplier);                     
        $data['service_fee']        =   ($share * .01);
        $data['filling_fee']        =   30;
        $data['cash_on_hand']       =   $share - ($data['service_fee'] + $data['interest'] + $data['filling_fee']);
        $data['loan_receivable']    =   $data['cash_on_hand'] + $data['service_fee'] + $data['interest'] + $data['filling_fee'];
        $data['debit']              =   $data['loan_receivable'];
        $data['credit']             =   $data['interest'] + $data['service_fee'] + $data['filling_fee'] + $data['cash_on_hand'];

        return $data;
    }

// Get the state of data. Anything but false will trigger the breakout table
$data   =   (isset($_POST['calculate']))? CalculateItem($_POST['share'],$_POST['terms']) : false;
?>

<div class="span8" align="center">
<table width="100%">
    <tr>
        <td align="center" width="50%">
            <form action="" method="POST">
                <label> *NOTE: Php.36,000.00 - maximum applied loanable amount</label>
                <br/>
                <label>Share Capital:</label>
                <input onBlur="this.value=formatCurrency(this.value)" type="hidden">
                <input type="text" name="share" value="0" class="textbox"/>
                <br>
                <label>Term:</label>
                <select name="terms"/>
                    <option value="1">1 Month</option>
                    <option value="2">2 Months</option>
                </select>
                <br />
                <input type="submit" name="calculate" class="btn btn-success" value="Let's COMPUTE">
            </form></td>
        <td width="50%" align="justify" >
            <div class="alert alert-info" align="justify" style="font-size:16px">
                <?php if($data != false) { ?>
                <table width="100%">
                    <tr>
                        <td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
                        <td width="30%" align="right"><?php echo number_format($data['loan_receivable'], 2); ?></td>
                        <td width="30%" align="right"></td>
                    </tr>
                    <tr>
                        <td width="40%" align="justify"><?php echo "Interest "; ?></td>
                        <td width="30%" align="right"></td>
                        <td width="30%" align="right"><?php echo number_format($data['interest'], 2); ?></td>
                    </tr>
                    <tr>
                        <td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
                        <td width="30%" align="right"></td>
                        <td width="30%" align="right"><?php echo number_format($data['service_fee'], 2); ?></td>
                    </tr>
                    <tr>
                        <td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
                        <td width="30%" align="right"></td>
                        <td width="30%" align="right"><?php echo number_format($data['filling_fee'], 2); ?></td>
                    </tr>
                    <tr>
                        <td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
                        <td width="30%" align="right"></td>
                        <td width="30%" align="right"><?php echo number_format($data['cash_on_hand'], 2); ?></td>
                    </tr>
                    <tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
                        <td width="40%" align="right"><?php echo "TOTAL "; ?></td>
                        <td width="30%" align="right"><?php echo number_format($data['debit'], 2); ?></td>
                        <td width="30%" align="right"><?php echo number_format($data['credit'], 2); ?></td>
                    </tr>
                    <tr>
                        <td><br/>
                            <br/></td>
                        <td><br/>
                            <br/></td>
                        <td><br/>
                            <br/></td>
                    </tr>
                    <tr>
                        <td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
                        <td width="30%" align="right"><?php echo number_format($data['share'], 2); ?></td>
                    </tr>
                    <tr>
                        <td width="40%" align="justify"><?php echo "Term: "; ?></td>
                        <td width="30%" align="right"><?php echo $data['terms']. " month "; ?></td>
                    </tr>
                </table>
                <?php } ?>
            </div>
        </td>
    </tr>
</table>

Upvotes: 1

saurabh kamble
saurabh kamble

Reputation: 1549

NOTE:Used previous link and have shortened the code.Please test it properly just did blank coding

 <div class="span8" align="center">
<table width="100%">
    <tr>
        <td align="center" width="50%"> 
            <form action="" method="POST">
                <label> *NOTE: Php.36,000.00 - maximum applied loanable amount</label>
                <br/>
                <label>Share Capital:</label>
                <input onBlur="this.value = formatCurrency(this.value)" type="hidden"> 
                <input type="text" name="share" value="0" class="textbox"/><br>
                <label>Term:</label>
                <select name="terms"/>
                <option value="1">1 Month</option>
                <option value="2">2 Months</option>
                </select> <br /> 
                <input type="submit" name="calculate" class="btn btn-success" value="Let's COMPUTE">
            </form></td>

        <td width="50%" align="justify" > 
            <div class="alert alert-info" align="justify" style="font-size:16px">
                <?php
                if (isset($_POST['calculate'])) {
                    $share = $_POST['share'];
                    $terms = $_POST['terms'];
                    ?>
                    <?php if ($share <= "30000") { ?>
                        <?php
                        if ($terms == "1") {
                            $interest = $share * .0130;
                        } elseif ($terms == "2") {
                            $interest = $share * .0195;
                            if ($terms == "1") {
                                $interest = $share * .0130;
                            } elseif ($terms == "2") {
                                $interest = $share * .0205;
                            }}

elseif ($share >= 30001) { if ($terms == "1") { $interest = $share * .0130; } elseif ($terms == "2") { $interest = $share * .0205; } } ?>

                        ?> 

                        <table width="100%">
                            <tr>
                                <td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
                                <td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
                                <td width="30%" align="right"> </td>
                            </tr>

                            <tr>
                                <td width="40%" align="justify"><?php echo "Interest "; ?></td>
                                <td width="30%" align="right"> </td>
                                <td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
                            </tr>

                            <tr>
                                <td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
                                <td width="30%" align="right"> </td>
                                <td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
                            </tr>
                            <tr>
                                <td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
                                <td width="30%" align="right"> </td>
                                <td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
                            </tr>
                            <tr>
                                <td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
                                <td width="30%" align="right"> </td>
                                <td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
                            </tr>

                            <tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
                                <td width="40%" align="right"><?php echo "TOTAL "; ?></td>
                                <td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
                                <td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
                            </tr>
                            <tr>
                                <td><br/><br/></td>
                                <td><br/><br/></td>
                                <td><br/><br/></td>
                            </tr>
                            <tr>
                                <td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
                                <td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
                            </tr>

                            <tr>
                                <td width="40%" align="justify"><?php echo "Term: "; ?></td>
                                <td width="30%" align="right"><?php echo $terms . " month "; ?> </td>
                            </tr>
                        </table>



                    <?php } ?> 
            </div>          </td>
    </tr>
</table>

Upvotes: 1

Related Questions