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.