#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    What is wrong with my code


    below is my code
    PHP Code:
    <Form name ="form1" Method ="POST" >
    Enter Nifty Spot Price<INPUT TYPE = "TEXT" value="<?php echo $_POST['spotprice'?>" Name ="spotprice"/>
    <INPUT TYPE = "Submit" Name = "getCallPut" VALUE = "Next->">
    </form>

    <?php 

    if(isset($_POST['getCallPut'])) { 
    //echo "BUTTON WORKS"; 
    callput(); 


    function 
    callput() {

    $spotPrice 1.0 $_POST['spotprice'];
    if(
    $spotPrice%100>=&& $spotPrice%100<40){
                
    $callStrikePrice=floor($spotPrice/100)*100 100;
                
    $putStrikePrice=floor($spotPrice/100)*100 100;
                
            }else if(
    $spotPrice%100 >=40 && $spotPrice%100<=60){
                
    $callStrikePrice=floor($spotPrice/100)*100 100;
                
    $putStrikePrice=floor($spotPrice/100)*100;
                
            }else{
                
    $callStrikePrice=floor($spotPrice/100)*100 200;
                
    $putStrikePrice=floor($spotPrice/100)*100;
                
            }
    echo
    ' <Form name ="form2" Method ="POST" >';
    echo
    'Enter price for Nifty <input type="text" name="something" disabled="" style="border:none;width:40px;background-color:#000000;color:yellow;font-weight:bold;text-align:center" value="'.$callStrikePrice.'"/> CE <input type="text" name="callRate" size="10"><br><br>';
    echo
    'Enter price for Nifty <input type="text" name="something1" disabled="" style="border:none;width:40px;background-color:#000000;color:yellow;font-weight:bold;text-align:center" value="'.$putStrikePrice.'"/> PE <input type="text" name="putRate" size="10">';
    echo
    '<INPUT TYPE = "Submit" Name = "trend" VALUE = "Calculate Trend->">';
    echo
    '</form>';  
                     }
             
                    
     
    ?>


    <?php


    if(isset($_POST['trend'])) { 
    //echo "BUTTON WORKS"; 
    trend(); 


    function 
    trend(){
    $callRate 1.0 $_POST['callRate'];
    $putRate 1.0 $_POST['putRate'];
    $actualCallRate ceil(100*(((($callStrikePrice-$spotPrice)-($callStrikePrice-($callStrikePrice+$putStrikePrice)/2))/($callStrikePrice $spotPrice)) + 1) * $callRate);

    $actualPutRate ceil(100.0 * (- (((($callStrikePrice+$putStrikePrice)/2.0 $putStrikePrice) - ($spotPrice $putStrikePrice))/($spotPrice $putStrikePrice))) * $putRate);

    $buyPercent ceil(10000.0 $actualCallRate/($actualCallRate $actualPutRate));
    $sellPercent =  ceil(10000.0 $actualPutRate/($actualCallRate $actualPutRate));
     
    echo
    'The chances of Nifty Future going up is ' .$buyPercent/100.0.  ' % and going down is ' .$sellPercent/100.0' %';
    if(
    $buyPercent==$sellPercent){
                echo
    "<br/><h1 style='color:yellow'>TREND IS NEUTRAL</h1>";
            }else if(
    $buyPercent>$sellPercent){
                echo
    "<br/><h1 style='color:green'>TREND IS UP</h1>";
            }else{
                echo
    "<br/><h1 style='color:red'>TREND IS DOWN</h1>";
            }   
    }
    ?>
    i have to take input from user twice ..the user has to click 2 submit buttons... so i had to define 2 functions...the above code gives me warning
    Warning: Division by zero in /home/rohitb/public_html/test/live nifty trend/trend.php on line 51

    Warning: Division by zero in /home/rohitb/public_html/test/live nifty trend/trend.php on line 53...as the values of the variable are not passed to the other function
    ..... how can i make the above code work...im stuck badly
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    Don't try to divide by zero. You need to echo the values you are using to create your divisor to see why they result in a zero value and correct that. If a zero value is acceptable in context then you need to test for that condition and assign an appropriate result that does not require a divide by zero.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Divide-by-zero has been pushed down to WARNING now? Kids these days and their pampered lives with computer languages which can survive divide-by-zero. Pfft.

    Seriously though, why do you have 2 submit buttons and 2 separate page loads? And why do you believe that the values will be carried over between page loads (they will not)? This is relatively easy input, it can be ONE form with ONE page load, that will solve some of your problems.

    Comments on this post

    • Hammer65 agrees : Damn kids can get off my lawn that's my warning
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,927
    Rep Power
    1045
    Hi,

    not sure why you'd assume that a function has access to the local variables of a completely different function that's called in a completely different request.

    Local variables only exist during a particular function call, and variables in general only exist during a particular request. How else could it work? Would you want PHP to store every single variable that has ever been generated on your hard disk? Would you want user A to have access to all data of user B?

    There are basically three ways of passing data from one request to another:

    • You include all previously submitted data in the form for the next request (as hidden fields). This way they get carried over to the next page.
    • You ask the browser of your user to store the data in a cookie. Next time the user visits the page, the cookie is transmitted and allows you to restore the data.
    • You use sessions. This means you store the data on your server and give your user an identifier for this file (usually a random string in a cookie).

    Another possiblity would be to only have a single page and reload the parts dynamically with JavaScript.

    Which one you choose is a matter of your personal preferences, priorities and your target audience. You have to decide how your form is supposed to work.

    Personally, I'd probably have one form. The input fields are greyed-out first, and when you enter data, they're enabled one by one. And each time the calculated data gets updated.

    But you may have something different in mind. We don't know.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0
    can i have some example code or someone modify my code
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0
    Originally Posted by gw1500se
    Don't try to divide by zero. You need to echo the values you are using to create your divisor to see why they result in a zero value and correct that. If a zero value is acceptable in context then you need to test for that condition and assign an appropriate result that does not require a divide by zero.
    zero is not accepted .... i need 3 values from first function callput()...$callStrikePrice,$putStrikePrice and $spotPrice to be transferred to my second function trend()
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0
    Originally Posted by ManiacDan
    Divide-by-zero has been pushed down to WARNING now? Kids these days and their pampered lives with computer languages which can survive divide-by-zero. Pfft.

    Seriously though, why do you have 2 submit buttons and 2 separate page loads? And why do you believe that the values will be carried over between page loads (they will not)? This is relatively easy input, it can be ONE form with ONE page load, that will solve some of your problems.
    why 2 buttons as user has first give one input... then we do some calculations ... after that calculation he has to give 2nd input....then again some calculation and then final result....i dont need 2 pages ...

    i need 3 values from first function callput()...$callStrikePrice,$putStrikePrice and $spotPrice to be transferred to my second function trend()

IMN logo majestic logo threadwatch logo seochat tools logo