0

I have a website that reads data about baseball games. First the website displays the game dates and scores:

$.post('../php/getGameDates.php', function(returnedDates) {
    var objDates = jQuery.parseJSON( returnedDates );
    $('#content').hide();

    var pubStr = "";
    for (var a=0; a<objDates.length; a++) {
        var dateParts = objDates[a].game_date.split("-");
                    var mo;
                    switch(dateParts[1]) {
                        case "04":
                            mo = "April"
                            break;
                        case "05":
                            mo = "May"
                            break;
                        case "06":
                            mo = "June"
                            break;
                        case "07":
                            mo = "July"
                            break;
                        case "08":
                            mo = "Aug."
                            break;
                        case "09":
                            mo = "Sept."
                            break;
                        case "10":
                            mo = "Oct."
                            break;
                        default:
                            break;
                    }
                    var day = dateParts[2].replace(/^0+/, '');

        pubStr += "<div class='game_to_click' id='" + objDates[a].game_date + "'>" + mo + " " + day + ", " + dateParts[0] + ": " + objDates[a].score + "</div>"
    }
    $('#game_dates').append(pubStr);
...
});

When you click a date, you get a popup of data about that game. There are prev/next buttons on the popup. The thing is, the data seems to "blink" when it appears on the popup. I suspect that's because of the query to the database. Here is the php code for the "prev" button:

<?php
include_once ('../../../homicide/php/constants_test.php'); 
// connect to database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //open db conn
if (mysqli_connect_errno()) {
        printf("Connect failed: %s", mysqli_connect_error());
        exit();
}
//date_default_timezone_set('America/New_York');
$thisDate = $_POST['thisDate'];
//echo $thisDate;
//$time = strtotime($thisDate . ' - 1 day');
//$newDate = date('Y-m-d',$time);
$parts = explode("-", $thisDate);
$day = $parts[2];
if (substr($day, -2) == "0") {
    $day = substr($day, -1);
    $day = intval($day);
}
$day--;
$newDate = $parts[0] . "-" . $parts[1] . "-";
//echo '$day: ' . $day;
if (strlen($day) < 2){
    $newDate .= "0";
}
$newDate .= $day;
//echo "new: " . $newDate . " ";
tryQuery($newDate, $mysqli);

function tryQuery($thisDate, $mysqli) {
    $q = "SELECT * FROM pirates_games where game_date = '" . $thisDate . "'";
    $result = $mysqli->query($q);
    $row_cnt = mysqli_num_rows($result);
    //echo $row_cnt;
    if ($row_cnt > 0) {
        while($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $arrGame = $row;
        }
        echo json_encode($arrGame);
        mysqli_close($mysqli);
    }
    else {
        //echo 'date on entry: ' . $thisDate . " ";
        $parts = explode("-", $thisDate);
        $day = $parts[2];
        if (substr($day, -2) == "0") {
            $day = substr($day, -1);
            $day = intval($day);
        }
        $day--;
        $newDate = $parts[0] . "-" . $parts[1] . "-";
        //echo '$day: ' . $day;
        if (strlen($day) < 2){
            $newDate .= "0";
        }
        $newDate .= $day;
        //echo "new: " . $newDate . " ";
        //$time = strtotime($thisDate . ' - 1 day');
        //$newDate = date('Y-m-d',$time);
        //echo "new: " . $newDate;
        tryQuery($newDate, $mysqli);
    }
} 

?>     

Is this method of trying first one query then another the right way to go about this? Most times, there is a game the next day or the previous day, but sometimes, the dates skip a day. I'm not sure how to account for skipped days when I try to find the next or previous game.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
LauraNMS
  • 2,720
  • 7
  • 39
  • 73
  • what do you mean by 'dates skips a day' is it business logic or program problem ? – siddhesh Jul 13 '15 at 15:44
  • simpler to query for date >= $date and limit to 1 to get the next game from db – charlietfl Jul 13 '15 at 15:44
  • The problem seems to have something to do with when I click the next or last button, the number of clicks goes up exponentially: 1st click - behaves as if I clicked it 1 time; 2nd click - behaves as if I clicked it 2 times; 3rd click - behaves as if I clicked it 4 times; 4th click - behaves as if I clicked it 8 times (2 to the power of 0, then 1, then 2, then 3). – LauraNMS Jul 13 '15 at 18:50

2 Answers2

0

My way of thinking on this would be to load the games as a sequential rather than a date based method. Then you could recode to show previous 'game' rather than 'previous day's game', (or next day's game) etc... It's more accurate to the game sequence you've described. This way the previous game's date is just associated information, but it could easily be part of the display.

If that doesn't appeal to you, you could load the dates into an array with the associated games (date being the key and game or no game being the data). On dates that have no game you can show that in your display options.

glennw
  • 58
  • 5
  • This doesn't seem to be the answer. Seems to have something to do with when I click the next or last button, the number of clicks goes up exponentially: 1st click - behaves as if I clicked it 1 time; 2nd click - behaves as if I clicked it 2 times; 3rd click - behaves as if I clicked it 4 times; 4th click - behaves as if I clicked it 8 times (2 to the power of 0, then 1, then 2, then 3). – LauraNMS Jul 13 '15 at 18:50
  • On second look, your answer is part of the answer, but it turned out I was having two problems at once. Thank you! – LauraNMS Jul 14 '15 at 15:06
0

I didn't articulate my problem very well, but I was having two problems at once. Part of the problem was click events firing multiple times, for both the next and prev buttons (See jQuery click events firing multiple times):

$(".next").unbind().click(function() {
    //Stuff
});

$(".prev").unbind().click(function() {
    //Stuff
});

The other part of the problem was finding what the next or previous date was, and here, @glennw (below) was correct.

Community
  • 1
  • 1
LauraNMS
  • 2,720
  • 7
  • 39
  • 73