Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work out values from loaded information

I need to be able to show a "dynamic return" for each bet that the user has in place, but for some reason none of them work. I have previously asked this question before, but with no luck.

I am hoping that the extra detail in this will be sufficient to help with getting an answer to this at last.

I have hard coded one of the scripts in so to use odds-1, stake-1etc, and this worked but the others have not.

If anybody can help with this it would be greatly appreciated.

JavaScript

        var count_div = 0;
        $("div").each(function () {
            count_div++;
            console.log("Counter: " + count_div);
        });

        for (var i = 0; i < count_div; ++i) {
            $("#stake-" + i).on('keyup', function () {
                var newVal = (parseFloat($("#stake-" + i).val(), 10) * parseFloat($("#__odds-" + i).val(), 10)) + parseFloat($("#stake-" + i).val(), 10) || 0;
                $("#showdynamicreturn-" + i).val(parseFloat(newVal).toFixed(2));
            });
        }

PHP

function readbets( $link, $id, $currentpage, $loggedIn = true ) {
    $idCount = 0;
    $queryBase = "SELECT * FROM `bets` WHERE `user_id` = '%s';";
    if($loggedIn == true) {
        $queryBase2 = sprintf($queryBase, $id);
    }
    else {
        $queryBase2 = sprintf($queryBase, $id);
    }
    $selectQuery = $link->query($queryBase2);
    $return = "<div style='max-height: 680px; overflow: auto;'>";
    while($result = mysqli_fetch_array($selectQuery)) {
        $idCount++;
        if($result['odds'] == "SP") {
            $odds = "SP";
        }
        else {
            $_odds = explode("/", $result['odds']);
            $odds = $_odds[0] / $_odds[1];
        }            

        $return .= "<div style='border: 1pt solid black; width: 99%;'>";
        $return .= "<h2>" . stripslashes($result['title']) . "</h2>";
        $return .= "<form action='/' method='post'>";
        $return .= "<table class='table table-condensed'>";

        $return .= "<tr><td style='width:50%;'>Sport: </td><td>" . $result['sport'] . "</td></tr>";
        $return .= "<tr><td style='width:50%'>Participant: </td><td>" . stripslashes($result['participant']) . "</td></tr>";
        $return .= "<tr><td>Market: </td><td>" . stripslashes($result['market']) . "</td></tr>";
        $return .= "<tr><td>Time: </td><td>" . date("H:i", strtotime($result['bet_till_time'])) . "</td></tr>";
        $return .= "<tr><td>Odds: </td><td>" . $result['odds'] . "<input type='hidden' value='" . $odds . "' id='__odds-" . $idCount . "' /></td></tr>";
        $return .= "<tr><td>Stake: </td><td><div class='input-group'><span class='input-group-addon' id='basic-addon1'>&pound;</span><input style='width:100%' type='text' name='stake' id='stake-" . $idCount . "' aria-describedby='basic-addon1' placeholder='Stake' /></div></td></tr>";
        $return .= "<tr id='dynamic_return'><td colspan='10'><center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: &pound;</span><input style='width:100%' type='text' id='showdynamicreturn-" . $idCount . "' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center></td></tr>";

        if($result['ew_available'] == "true") {
            $return .= "<tr><td><center><label>Each way bet?&nbsp;&nbsp;<input type='checkbox' id='eachWayChk' name='eachWayChk' /></label></center></td>" .  "<td><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
        }
        else {                
            $return .=  "<tr><td colspan='10'><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
        }

        $return .= "<!-- Hidden fields for the horses information. -->";
        $return .= "<input type='hidden' name='betslip_id' value='" . $result['bet_id'] . "' />";
        $return .= "<input type='hidden' name='sport' value='" . $currentpage . "' />";
        $return .= "<input type='hidden' name='eachWay' id='eachWay' value='' />";

        $return .= "<input type='hidden' name='odds' value='" . $result['odds'] . "' />";
        $return .= "<input type='hidden' name='ew_odds' value='" . $result['ew_odds'] . "' />";
        $return .= "<input type='hidden' name='sport' value='" . $result['sport'] . "' />";

        $return .= "<input type='hidden' name='bettilldate' value='" . $result['bettilldate'] . "' />";
        $return .= "<input type='hidden' name='bettilltime' value='" . $result['bettilltime'] . "' />";

        $return .= "<!-- Area to \"submit a delete\" and remove an item from the bet slip. -->";
        $return .= "<tr><td colspan='100%'><center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick=\"return confirm('Are you sure you want to delete this?')\" /></center></td></tr>";
        $return .= "</table>";
        $return .= "</form>";
        $return .= "</div><br>";
    }
    $return .= "</div>";
    return $return;
}

Generated HTML

<div style='border: 1pt solid black; width: 99%; border-radius: 25pt;'>
    <h2>Cyprus v Wales - Match Betting</h2>
    <form action='/' method='post'>
        <table class='table table-condensed'>
            <tr>
                <td style='width: 50%;'>Sport: </td>
                <td>Football</td>
            </tr>
            <tr>
                <td style='width: 50%'>Participant: </td>
                <td>Cyprus</td>
            </tr>
            <tr>
                <td>Market: </td>
                <td>Euro 2016</td>
            </tr>
            <tr>
                <td>Time: </td>
                <td>19:45</td>
            </tr>
            <tr>
                <td>Odds: </td>
                <td>19/5<input type='hidden' value='3.8' id='__odds-3' /></td>
            </tr>
            <tr>
                <td>Stake: </td>
                <td>
                    <div class='input-group'><span class='input-group-addon' id='basic-addon1'>&pound;</span><input style='width: 100%' type='text' name='stake' id='stake-3' aria-describedby='basic-addon1' placeholder='Stake' /></div>
                </td>
            </tr>
            <tr id='dynamic_return'>
                <td colspan='10'>
                    <center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: &pound;</span><input style='width:100%' type='text' id='showdynamicreturn-4' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center>
                </td>
            </tr>
            <tr>
                <td colspan='10'>
                    <center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center>
                </td>
            </tr>
            <!-- Hidden fields for the horses information. -->
            <input type='hidden' name='betslip_id' value='13' /><input type='hidden' name='sport' value='' /><input type='hidden' name='eachWay' id='eachWay' value='' /><input type='hidden' name='odds' value='19/5' /><input type='hidden' name='ew_odds' value='' /><input type='hidden' name='sport' value='Football' /><input type='hidden' name='bettilldate' value='' /><input type='hidden' name='bettilltime' value='' /><!-- Area to "submit a delete" and remove an item from the bet slip. --><tr>
                <td colspan='100%'>
                    <center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick="return confirm( 'Are you sure you want to delete this?' )" /></center>
                </td>
            </tr>
        </table>
    </form>
</div>
like image 503
Can O' Spam Avatar asked Aug 18 '15 12:08

Can O' Spam


People also ask

What is dynamic calculation?

In Essbase, you can define a member to have a dynamic calculation. This definition tells Essbase to calculate a data value for the member as users request it. Dynamic calculation shortens batch database calculation time, but may increase retrieval time for the dynamically calculated data values.

What is the difference between table calculation and calculated field?

Table Calculations are simpler and their scope is more limited compared to Calculated Fields. Calculated fields are much more diverse enabling deeper analysis.


2 Answers

I think the problem is in the usage of variable i in your "for" cycle. You are using this variable in a closure - if you put alert(i) into the keyup callback, it will always give you the total number of divs, no matter which value you changed.

To fix this, I'd suggest you to bind keyup method in more clever way, something like this:

        $("body").delegate('.stake', 'keyup', function () {
            var $input = $(this);
            var i = parseInt($input.attr('id').replace('stake-', ''));
            var newVal = (parseFloat($input.val(), 10) * parseFloat($("#__odds-" + i).val(), 10)) + parseFloat($input.val(), 10) || 0;
            $("#showdynamicreturn-" + i).val(parseFloat(newVal).toFixed(2));
        });

To make it work you need to add "stake" class to your input element

like image 77
Lukas Kral Avatar answered Oct 11 '22 16:10

Lukas Kral


As you have declared $idCount=0 inside the function, each time when the function is called it is re assigned with value 0. So to make it work the way we want initialize the $idCount with value zero globbaly outside the function prototype one time and above calling the "readbets" function. it could be somethinglike this. I am not sure whether this is what you are trying to do but i think it is.hope this helps.

$idCount=0;
//not clear enough if its for multiple users uncomment the foreach loop
//foreach ($id as $id){
//each time the idCount incements and the value is stored for next increment.
echo readbets( $link, $id, $currentpage, $loggedIn);
//}
//below is same as your code except that i removed $idCount = 0;
function readbets( $link, $id, $currentpage, $loggedIn = true ) {
//$idCount = 0;
    $queryBase = "SELECT * FROM `bets` WHERE `user_id` = '%s';";
    if($loggedIn == true) {
        $queryBase2 = sprintf($queryBase, $id);
    }
    else {
        $queryBase2 = sprintf($queryBase, $id);
    }
    $selectQuery = $link->query($queryBase2);
    $return = "<div style='max-height: 680px; overflow: auto;'>";
    while($result = mysqli_fetch_array($selectQuery)) {
        $idCount++;
        if($result['odds'] == "SP") {
            $odds = "SP";
        }
        else {
            $_odds = explode("/", $result['odds']);
            $odds = $_odds[0] / $_odds[1];
        }            

        $return .= "<div style='border: 1pt solid black; width: 99%;'>";
        $return .= "<h2>" . stripslashes($result['title']) . "</h2>";
        $return .= "<form action='/' method='post'>";
        $return .= "<table class='table table-condensed'>";

        $return .= "<tr><td style='width:50%;'>Sport: </td><td>" . $result['sport'] . "</td></tr>";
        $return .= "<tr><td style='width:50%'>Participant: </td><td>" . stripslashes($result['participant']) . "</td></tr>";
        $return .= "<tr><td>Market: </td><td>" . stripslashes($result['market']) . "</td></tr>";
        $return .= "<tr><td>Time: </td><td>" . date("H:i", strtotime($result['bet_till_time'])) . "</td></tr>";
        $return .= "<tr><td>Odds: </td><td>" . $result['odds'] . "<input type='hidden' value='" . $odds . "' id='__odds-" . $idCount . "' /></td></tr>";
        $return .= "<tr><td>Stake: </td><td><div class='input-group'><span class='input-group-addon' id='basic-addon1'>&pound;</span><input style='width:100%' type='text' name='stake' id='stake-" . $idCount . "' aria-describedby='basic-addon1' placeholder='Stake' /></div></td></tr>";
        $return .= "<tr id='dynamic_return'><td colspan='10'><center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: &pound;</span><input style='width:100%' type='text' id='showdynamicreturn-" . $idCount . "' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center></td></tr>";

        if($result['ew_available'] == "true") {
            $return .= "<tr><td><center><label>Each way bet?&nbsp;&nbsp;<input type='checkbox' id='eachWayChk' name='eachWayChk' /></label></center></td>" .  "<td><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
        }
        else {                
            $return .=  "<tr><td colspan='10'><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
        }

        $return .= "<!-- Hidden fields for the horses information. -->";
        $return .= "<input type='hidden' name='betslip_id' value='" . $result['bet_id'] . "' />";
        $return .= "<input type='hidden' name='sport' value='" . $currentpage . "' />";
        $return .= "<input type='hidden' name='eachWay' id='eachWay' value='' />";

        $return .= "<input type='hidden' name='odds' value='" . $result['odds'] . "' />";
        $return .= "<input type='hidden' name='ew_odds' value='" . $result['ew_odds'] . "' />";
        $return .= "<input type='hidden' name='sport' value='" . $result['sport'] . "' />";

        $return .= "<input type='hidden' name='bettilldate' value='" . $result['bettilldate'] . "' />";
        $return .= "<input type='hidden' name='bettilltime' value='" . $result['bettilltime'] . "' />";

        $return .= "<!-- Area to \"submit a delete\" and remove an item from the bet slip. -->";
        $return .= "<tr><td colspan='100%'><center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick=\"return confirm('Are you sure you want to delete this?')\" /></center></td></tr>";
        $return .= "</table>";
        $return .= "</form>";
        $return .= "</div><br>";
    }
    $return .= "</div>";
    return $return;
}

Or you could simply check whether its set and its value is 0, if not just leave it like that else set it to zero. although it looks pretty this way it will each time check whether its assigned or not.

function readbets( $link, $id, $currentpage, $loggedIn = true ) {
    //just added this if else to your code nothing else
    $idCount = isset($idCount)?$idCount:$idCount=0;
    //edit end
    $queryBase = "SELECT * FROM `bets` WHERE `user_id` = '%s';";
    if($loggedIn == true) {
        $queryBase2 = sprintf($queryBase, $id);
    }
    else {
        $queryBase2 = sprintf($queryBase, $id);
    }
    $selectQuery = $link->query($queryBase2);
    $return = "<div style='max-height: 680px; overflow: auto;'>";
    while($result = mysqli_fetch_array($selectQuery)) {
        $idCount++;
        if($result['odds'] == "SP") {
            $odds = "SP";
        }
        else {
            $_odds = explode("/", $result['odds']);
            $odds = $_odds[0] / $_odds[1];
        }            

        $return .= "<div style='border: 1pt solid black; width: 99%;'>";
        $return .= "<h2>" . stripslashes($result['title']) . "</h2>";
        $return .= "<form action='/' method='post'>";
        $return .= "<table class='table table-condensed'>";

        $return .= "<tr><td style='width:50%;'>Sport: </td><td>" . $result['sport'] . "</td></tr>";
        $return .= "<tr><td style='width:50%'>Participant: </td><td>" . stripslashes($result['participant']) . "</td></tr>";
        $return .= "<tr><td>Market: </td><td>" . stripslashes($result['market']) . "</td></tr>";
        $return .= "<tr><td>Time: </td><td>" . date("H:i", strtotime($result['bet_till_time'])) . "</td></tr>";
        $return .= "<tr><td>Odds: </td><td>" . $result['odds'] . "<input type='hidden' value='" . $odds . "' id='__odds-" . $idCount . "' /></td></tr>";
        $return .= "<tr><td>Stake: </td><td><div class='input-group'><span class='input-group-addon' id='basic-addon1'>&pound;</span><input style='width:100%' type='text' name='stake' id='stake-" . $idCount . "' aria-describedby='basic-addon1' placeholder='Stake' /></div></td></tr>";
        $return .= "<tr id='dynamic_return'><td colspan='10'><center><div class='input-group'><span class='input-group-addon' id='basic-addon2'>Estimated Return: &pound;</span><input style='width:100%' type='text' id='showdynamicreturn-" . $idCount . "' aria-describedby='basic-addon2' placeholder='0.00' readonly /></div></center></td></tr>";

        if($result['ew_available'] == "true") {
            $return .= "<tr><td><center><label>Each way bet?&nbsp;&nbsp;<input type='checkbox' id='eachWayChk' name='eachWayChk' /></label></center></td>" .  "<td><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
        }
        else {                
            $return .=  "<tr><td colspan='10'><center><input type='submit' name='submitTo__OpenBets' value='Place Bet' /></center></td></tr>";
        }

        $return .= "<!-- Hidden fields for the horses information. -->";
        $return .= "<input type='hidden' name='betslip_id' value='" . $result['bet_id'] . "' />";
        $return .= "<input type='hidden' name='sport' value='" . $currentpage . "' />";
        $return .= "<input type='hidden' name='eachWay' id='eachWay' value='' />";

        $return .= "<input type='hidden' name='odds' value='" . $result['odds'] . "' />";
        $return .= "<input type='hidden' name='ew_odds' value='" . $result['ew_odds'] . "' />";
        $return .= "<input type='hidden' name='sport' value='" . $result['sport'] . "' />";

        $return .= "<input type='hidden' name='bettilldate' value='" . $result['bettilldate'] . "' />";
        $return .= "<input type='hidden' name='bettilltime' value='" . $result['bettilltime'] . "' />";

        $return .= "<!-- Area to \"submit a delete\" and remove an item from the bet slip. -->";
        $return .= "<tr><td colspan='100%'><center><input type='submit' name='delete_betslip_item' value='Delete this bet' onclick=\"return confirm('Are you sure you want to delete this?')\" /></center></td></tr>";
        $return .= "</table>";
        $return .= "</form>";
        $return .= "</div><br>";
    }
    $return .= "</div>";
    return $return;
}
like image 36
aimme Avatar answered Oct 11 '22 17:10

aimme