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.
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.
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.
Related
We are creating a system where employees need to update their daily status report. The fundamental purpose of the system is to note the missing dates on which they did not update the status report.
I have found a way to do that by checking the day difference between the 2 array values and then counting & displaying the days. However, I am not sure how to do this dynamically between the 2 array values.
Here's the code I have used along with the output:
//id of the person updating the DSR
$userid = $_id;
// Array to fetch all the DSR by specific user
$fine = getDSRbyUserIdOrderDate($userid);
$today = date('d-m-Y');
$tomorrow = date("d-m-Y", strtotime($today . " +1 day"));
$ok = count($fine) - 1;
//Array to get dates
$d1 = $fine[0]['date'];
$d2 = $fine[1]['date'];
$d3 = $fine[2]['date'];
// Function call to find date difference
$dateDiff = dateDiffInDays($d1, $d2);
$dateDiff1 = dateDiffInDays($d2, $d3);
echo "<h4 style='color:red'>You have missed the DSR on the following dates.</h4>";
for($p = 1; $p < $dateDiff; $p++){
$missingdate = date("d-m-Y", strtotime($d1 . " +$p day"));
echo "<span style='color:red'>$missingdate</span>";
echo "<br />";
}
for($p = 1; $p < $dateDiff1; $p++){
$missingdate = date("d-m-Y", strtotime($d2 . " +$p day"));
echo "<span style='color:red'>$missingdate</span>";
echo "<br />";
}
if($d2 != $today){
echo "<span style='color:red'>$today <i>- Kindly update DSR before midnight</i></span> ";
echo "<br />";
}
Output:
I would create first a list of entries by date and then "paint" it accordingly.
$starting_date = new DateTime('2019-11-23');
$num_days = 10
$from_db_by_date = [];
foreach ($fine as $entry) {
$from_db_by_date[$entry['date']] = $entry;
}
$all_by_date = [];
for ($i = 0; $i < $num_days; $i++) {
$date_str = $starting_date->format('d-m-Y');
$all_by_date[$date_str] = $from_db_by_date[$date_str] ?: null;
$starting_date->modify('+1 day');
}
Now you can loop through $all_by_date, check if the entry is null and act accordingly.
Hi I am trying to write a home budget forecast. I got to the point where I'd like to echo out table with dates from today up to certain date (next 2 months for example) and total value for this date.
I put some example data in database already but for some reason none of the records are being returned and all the values are 0 for each date in the table. I did not have a problem with creating same table for "Last Transactions" or "Upcoming Transactions".. Seems that here problem lays in query itself as num_rows is alwasys 0.
$currentDate = date('Y-m-d', time());
$endDate = "2019-09-30";
$content = '';
$dayTotal = 0;
while ($currentDate < $endDate) {
$getForecast_sql = "SELECT value FROM log WHERE date=".$currentDate."";
$getForecast_result = $conn->query($getForecast_sql);
if ($getForecast_result === false) {
echo "Query failed: " .$conn->error;
return false;
} else {
if ($getForecast_result->num_rows > 0) {
while($row = $getForecast_result->fetch_assoc()) {
$value = $row['value'];
$dayTotal += $value;
}
}
$content .= '<tr>
<td class="log-date">' . $currentDate . '</td>
<td class="log-desc">' . $dayTotal . '</td>
</tr>';
}
$currentDate = date('Y-m-d', strtotime($currentDate . '+1 day'));
}
echo $content;
i'm trying to do a contract management app using php and mysql and i'm having some questions regarding the dates.
I need to know the time that there is between today and specific dates in the contract, or if there is less than a month it should display days left..
the problem is that the comparison to know if the end of contract is in the past or in the future, does'n seems to work!
link to check the code: link to project
$hoje = date_create();
$fim = '2022-11-11';
$fim_data = date_create($fim);
$diff = date_diff( $hoje, $fim_data );
$meses = (($diff->format('%y')*12)+$diff->format('%m'));
$dias = $diff->days;
var_dump($fim < $hoje);
if($fim < $hoje) {
$result = "Contract has ended";
} elseif($meses >=1 ) {
$result = $meses . " months";
echo '<br>';
} else {
$result = $dias . " days";
};
echo '<br>';
echo $result;
You are comparing string with date object
Replace
if($fim < $hoje) {
With
if($fim_data < $hoje) {
Corrected code with solution by Felippe Duarte
$hoje = date_create();
$fim = '2018-11-11';
$fim_data = date_create($fim);
$diff = date_diff( $hoje, $fim_data );
$meses = (($diff->format('%y')*12)+$diff->format('%m'));
$dias = $diff->days;
var_dump($fim_data < $hoje);
if($fim_data < $hoje){$result = "não aplicavel";}
elseif($meses >=1 ){
$result = $meses . " meses";
echo '<br>';}
else{
$result = $dias . " dias";};
echo '<br>';
echo $result;
I feel like I'm missing something easy here and getting a little stuck while building a "schedule an appointment" calendar.
Clients can book their own time slots, and when they do...I need to make those time slots unavailable to others.
I am using a drop down to "for loop" in 30 minute intervals from 9am - 7pm for the "start time" selection. If a "start time" of "10:00am" and an "end time" of "11:30am" are saved in the database...10:00am, 10:30am, and 11:00am need to be withheld from the "start time" dropdown after successful form submission.
(Database results are in the form of a string...fyi)
To do that, I have this:
$sql_slots = "SELECT * FROM XXXXXXXXX WHERE date = '$query_date' ORDER BY XXXXXXXXX ASC";
$result_slots = mysqli_query($connection, $sql_slots);
$open = strtotime("9:00am");
echo'
<select name="start_time">';
for($b = 0; $b <= 20; $b++){
$y = strtotime(($b*30) . " Minutes", $open);
$the_time = date("h:ia", $y) . "<br>";
$starting = array();
while($row_result_slots = mysqli_fetch_assoc($result_slots)){
$starting[] = $row_result_slots['start_time'];
}
if(in_array($the_time, $starting)){
echo "<option>Not Available</option>";
} else {
echo "<option>" . $the_time . "</option>";
}
}
echo'</select>';
Any suggestions, to finish this or a better method than "in_array" to accomplish what I'm trying to do?
Thank you in advance.
The solution was to move the "while loop" outside of the "for loop" and I wasn't taking into account the "br" attached to the end of the $the_time variable.
Here is the working code:
$sql_slots = "SELECT * FROM XXXXXX WHERE date = '$query_date' ORDER BY XXXXXXX ASC";
$result_slots = mysqli_query($connection, $sql_slots);
$open = strtotime("9:00am");
$starting = array();
while($row_result_slots = mysqli_fetch_assoc($result_slots)){
$starting[] = $row_result_slots['start_time'];
}
echo'
<select name="start_time">';
for($b = 0; $b <= 20; $b++){
$y = strtotime(($b*30) . " Minutes", $open);
$the_time = date("h:ia", $y);
if(in_array($the_time, $starting)){
echo "<option>Not Available<br></option>";
} else {
echo "<option>" . $the_time . "<br></option>";
}
}
Not sure how to name correctly this question so feel free to edit if is needed.
I have 12 boxes one per month, and each box display:
1 - A message like "Sold" or "Available" depending on the info coming from the database.
2 - The name of the month and the year, if the month is older than the current month the year will be increased by one.
The info coming from the database have a few values separated by a | symbol, the pertinent one to check is the las one, an example value of $row['custom'] is: 93 | Supper | New Port | August - 2012
My problem is that I need to update each box with their "status" and with the current script only the las entry on the database is used to update the boxes, so In cases where i know there is two or more boxes that should display the message "Sold", only the most recent is the updated.
How can I modify the following script to update one by one box ?
The problem is the way I'm querying the database or something else ?
Thanks in advance for any help or advice.
Code only for 2 of 12 month, to make things short
<?php
$month = date("m"); // Current month
$year = date("Y"); // Current year
$next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
$nextyear = date("Y", $next); // Next year
// Check if this month is gone or not, if gone replace current year with next year
$january = "01";
$february = "02";
if ($month > $january) {
$jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}
if ($month > $february) {
$feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}
//Get info from Database
$query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$check_custom = explode(" | ", $row['custom']);
$month_sold = $check_custom[3];
}
// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';} else { $jan_status = 'Available';}
if ($month_sold == $feb) { $feb_status = 'Sold';} else { $feb_status = 'Available';}
//Output the months and their status
?>
<div class="month">
<div class="mname"><?php echo $jan;?></div>
<div class="<?php echo $jan_status;?>">
<?php echo $jan_status;?>
</div>
<?php if($jan_status == 'Available') {
echo 'This month is ' . $jan_status . '.';
} else {
echo 'This month has been ' . $jan_status . '.';} ?>
</div>
<div class="month">
<div class="mname"><?php echo $feb;?></div>
<div class="<?php echo $feb_status;?>">
<?php echo $feb_status;?>
</div>
<?php if($feb_status == 'Available') {
echo 'This month is ' . $feb_status . '.';
} else {
echo 'This month has been ' . $feb_status . '.';} ?>
</div>
You misplaced the closing bracket of while and move the `$jan_status='Available' from inside while loop to just above it; Here is the modified code
<?php
$month = date("m"); // Current month
$year = date("Y"); // Current year
$next = strtotime(date("d-m-Y", strtotime($now)) . "+1 year");
$nextyear = date("Y", $next); // Next year
// Check if this month is gone or not, if gone replace current year with next year
$january = "01";
$february = "02";
if ($month > $january) {
$jan = 'January - ' . $nextyear; } else { $jan = 'January - ' . $year;}
if ($month > $february) {
$feb = 'February - ' . $nextyear; } else { $feb = 'January - ' . $year;}
//Get info from Database
$query = "SELECT `custom` FROM `payments` WHERE `status` = 'Pending' OR `status` = 'Completed'";
$result = mysql_query($query) or die(mysql_error());
$jan_status = 'Available';
$feb_status = 'Available';
while($row = mysql_fetch_array($result)) {
$check_custom = explode(" | ", $row['custom']);
$month_sold = $check_custom[3];
// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';}
if ($month_sold == $feb) { $feb_status = 'Sold';}
}//th closing bracket should be here;
//Output the months and their status
?>
<div class="month">
<div class="mname"><?php echo $jan;?></div>
<div class="<?php echo $jan_status;?>">
<?php echo $jan_status;?>
</div>
<?php if($jan_status == 'Available') {
echo 'This month is ' . $jan_status . '.';
} else {
echo 'This month has been ' . $jan_status . '.';} ?>
</div>
<div class="month">
<div class="mname"><?php echo $feb;?></div>
<div class="<?php echo $feb_status;?>">
<?php echo $feb_status;?>
</div>
<?php if($feb_status == 'Available') {
echo 'This month is ' . $feb_status . '.';
} else {
echo 'This month has been ' . $feb_status . '.';} ?>
</div>
Every time you run $row = mysql_fetch_array(), it replaces the contents of $row with the next row from the database. Look at the while loop: it assigns values to $check_custom and $month_sold, but doesn't do anything with them before overwriting them with new values. You need to move all your code for parsing the database output and generating the info for a given month inside the loop. Then output or save the information you want to display for that month before it goes on to the next one.
There's a lot you could do to make your code simpler and easier to maintain. For example, I would make an array for the months and iterate over that, rather than creating a separate set of variables and separate output code for each month. Also, what you're doing with your custom column right now is using a field in your database table to store another tiny table. This creates a lot of problems -- if you broke that data out into multiple columns, you could just do a query for the appropriate month and year.
Move your status checks into the loop so that it operates on every row from the database. Otherwise you only wind up referring to the row returned by the last call of mysql_fetch_array. I would recommend using a switch statement for readability, though.
while($row = mysql_fetch_array($result)) {
$check_custom = explode(" | ", $row['custom']);
$month_sold = $check_custom[3];
// Check if month is sold or not
if ($month_sold == $jan) { $jan_status = 'Sold';}
else { $jan_status = 'Available';}
if ($month_sold == $feb) { $feb_status = 'Sold';}
else { $feb_status = 'Available';}
}