getting next/previous record via jquery and php -
i have website reads data baseball games. first website displays game dates , scores:
$.post('../php/getgamedates.php', function(returneddates) { var objdates = jquery.parsejson( returneddates ); $('#content').hide(); var pubstr = ""; (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 click date, popup of data game. there prev/next buttons on popup. thing is, data seems "blink" when appears on popup. suspect that's because of query database. here php code "prev" button:
<?php include_once ('../../../homicide/php/constants_test.php'); // connect 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 * pirates_games 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 method of trying first 1 query right way go this? times, there game next day or previous day, sometimes, dates skip day. i'm not sure how account skipped days when try find next or previous game.
my way of thinking on load games sequential rather date based method. recode show previous 'game' rather 'previous day's game', (or next day's game) etc... it's more accurate game sequence you've described. way previous game's date associated information, part of display.
if doesn't appeal you, load dates array associated games (date being key , game or no game being data). on dates have no game can show in display options.
Comments
Post a Comment