Generating an automatic student registration number using php - php

I want to generate an automatic student registration number composed of "ID N0.","INTAKE"
and the current year, each time a student is recorded.
EX: KIAC 0013/INTAKE 4/2019.
But the problem is that the intake value which is 4 only increments once and i want it to increment each time a new intake starts.
I wasn't able to increment the intake value
as a new intake starts,in march and in september. so this means that students
for the september intake will be having "KIAC 0013/INTAKE 4/2019" as registration number
and those of march 2020 "KIAC 0013/INTAKE 5/2020" again
september 2020 "KIAC 0013/INTAKE 6/2020" and so on... but the intake value would still be 5 for september 2020 also.. this means it increments no more, the code below is a sample code and it doesn't insert into the database i was just testing for the result..
<?php
$k = "KIAC";
$id = "0013/INTAKE";
$month = date("m");
$year = date("Y");
if($month == '3'){
$intake = 3;
$intake++;
echo $k . " " . $id . " " . $intake. "/". $year;
}
else if($month == '9'){
$intake = 3;
$intake++;
echo $k . " " . $id . " " . $intake. "/". $year;
}
else{
$intake = 3;
echo $k . " " . $id . " " . $intake. "/". $year;
}
?>
I expected the output to be "KIAC 0013/INTAKE 4/2019." and the intake value
would change as a new intake starts.

I'm not sure if i understod your question, but maybe you could do something like this:
$k = "KIAC";
$id = "0013/INTAKE";
$month = date("m");
$year = date("Y");
$base_year = 2019; // Set a base when the intakes started
$intake = intval($year) - $base_year; // This will increase for every year
$increase_with = $intake++;
if($month == '3'){
$intake += $increase_with;
echo $k . " " . $id . " " . $intake. "/". $year;
}
else if($month == '9'){
$increase_with++;
$intake += $increase_with;
echo $k . " " . $id . " " . $intake. "/". $year;
}
Edited to make intake number unique.

You have to write condition in IF so between march to august it will increase 1 and after September intake it will increase with 2.
<?php
$k = "KIAC";
$id = "0013/INTAKE";
$month = date("m");
$year = date("Y");
$base_year = 2016;
$intake = intval($year) - $base_year;
if ($month == 3) {
$intake += 1;
echo $k . " " . $id . " " . $intake . "/" . $year;
} else if($month == 9) {
$intake += 2;
echo $k . "enter code here " . $id . " " . $intake . "/" . $year;
}else{
echo $k . " " . $id . " " . $intake. "/". $year;
}
?>

Related

The same function with the same data gives me different results

I have a little code just to know when a message was sent:
function time_ago ($respuestas) {
$date = $respuestas;
$today_date = getdate();
$months = ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];
$minute = $today_date['minutes'];
$hour = $today_date['hours'];
$day = $today_date['mday'];
$month = $today_date['mon'];
if ($today_date['hours'] <= 9) {
$hour = "0" . $today_date['hours'];
}
if ($today_date['minutes'] <= 9) {
$minute = "0" . $today_date['minutes'];
}
if ($today_date['mon'] <= 9) {
$month = "0" . $today_date['mon'];
}
if ($today_date['mday'] <= 9) {
$day = "0" . $today_date['mday'];
}
$actual_total_date = $today_date['year'] . "-" . $month . "-" . $day . " " . $hour . ":" . $minute;
$actual_total_date = new DateTime($actual_total_date);
//data of the date of the post
$post_date = substr($date,0,16);
$post_date = new DateTime($post_date);
$post_year = substr($date,0,4);
$post_month = substr($date,5,2);
$post_day = substr($date,8,2);
$post_hour = substr($date,11,2);
$post_minute = substr($date,14,2);
$interval = date_diff($post_date, $actual_total_date);
$invertal_days = substr($interval->format('%R%a'),1,strlen($interval->format('%R%a')));
$invertal_hours = $interval->format('%H');
$invertal_minutes = $interval->format('%I');
$result = "";
if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes < 1) {
$result = "Now";
} else if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes == 1) {
$result = substr($invertal_minutes,1,2) . " minute ago";
} else if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes < 10 AND $invertal_minutes > 1) {
$result = substr($invertal_minutes,1,2) . " minutes ago";
} else if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes < 60 AND $invertal_minutes >= 10) {
$result = $invertal_minutes . " minutes ago";
} else if ($invertal_days == 0 AND $post_day == $day) {
$result = "Today at " . $post_hour . ":" . $post_minute ;
} else if ($invertal_days == 0 AND $post_day == $day-1) {
$result = "Yesterday at " . $post_hour . ":" . $post_minute;
} else if ($invertal_days == 1 AND $post_day == $day-1) {
$result = "Yesterday at " . $post_hour . ":" . $post_minute;
} else {
$result = $post_day . " " . $months[$post_month-1] . " " . $post_year . " " . $post_hour . ":" . $post_minute;
}
return $result;
The problem I'm having only happens in production, not in local, and after doing some research I don't know what is happening. What happens is that this function gives me two different outputs with the same input. For example with the date: 2017-07-09 20:52:39, at the time I'm sending this post, it should enter the fourth conditional, because the minutes interval is between 10 and 60 minutes.
I could share to the site where I'm having problems if needed. I have already tested the data I am using for the function and it is correct.

Change language in date()

i have a website with appointments and i use this booking script http://www.planetphp.co.uk/free-php-booking-slots-calendar/.
Unfortunately, i have tried everything to change the language when it shows the month.
class booking_diary {
// Mysqli connection
function __construct($link) {
$this->link = $link;
}
// Settings you can change:
// Time Related Variables
public $booking_start_time = "09:00"; // The time of the first slot in 24 hour H:M format
public $booking_end_time = "21:00"; // The time of the last slot in 24 hour H:M format
public $booking_frequency = 30; // The slot frequency per hour, expressed in minutes.
// Day Related Variables
public $day_format = 3; // Day format of the table header. Possible values (1, 2, 3)
// 1 = Show First digit, eg: "M"
// 2 = Show First 3 letters, eg: "Mon"
// 3 = Full Day, eg: "Monday"
public $day_closed = array("Saturday", "Sunday"); // If you don't want any 'closed' days, remove the day so it becomes: = array();
public $day_closed_text = "CLOSED"; // If you don't want any any 'closed' remove the text so it becomes: = "";
// Cost Related Variables
public $cost_per_slot = 20.50; // The cost per slot
public $cost_currency_tag = "€"; // The currency tag in HTML such as € £ ¥
// DO NOT EDIT BELOW THIS LINE
public $day_order = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
public $day, $month, $year, $selected_date, $back, $back_month, $back_year, $forward, $forward_month, $forward_year, $bookings, $count, $days, $is_slot_booked_today;
/*========================================================================================================================================================*/
function make_calendar($selected_date, $back, $forward, $day, $month, $year,$first_name) {
// $day, $month and $year are the $_GET variables in the URL
$this->day = $day;
$this->month = $month;
$this->year = $year;
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->telephone = $telephone;
$this->email = $email;
$this->page_id = $page_id;
$this->user_id = $user_id;
$this->uid = $uid;
$this->pon = $owner;
$this->rsvnumb = $rsvnumb;
// $back and $forward are Unix Timestamps of the previous / next month, used to give the back arrow the correct month and year
$this->selected_date = $selected_date;
$this->back = $back;
$this->back_month = date("m", $back);
$this->back_year = date("Y", $back); // Minus one month back arrow
$this->forward = $forward;
$this->forward_month = date("m", $forward);
$this->forward_year = date("Y", $forward); // Add one month forward arrow
// Make the booking array
$this->make_booking_array($year, $month);
}
function make_booking_array($year, $month, $j = 0) {
$stmt = $this->link->prepare("SELECT name, date, start FROM bookings WHERE date LIKE CONCAT(?, '-', ?, '%') and page_id=".$_GET['page_id']."");
$this->is_slot_booked_today = 0; // Defaults to 0
$stmt->bind_param('ss', $year, $month);
$stmt->bind_result($name, $date, $start);
$stmt->execute();
$stmt->store_result();
while($stmt->fetch()) {
$this->bookings_per_day[$date][] = $start;
$this->bookings[] = array(
"name" => $name,
"date" => $date,
"start" => $start
);
// Used by the 'booking_form' function later to check whether there are any booked slots on the selected day
if($date == $this->year . '-' . $this->month . '-' . $this->day) {
$this->is_slot_booked_today = 1;
}
}
// Calculate how many slots there are per day
$this->slots_per_day = 0;
for($i = strtotime($this->booking_start_time); $i<= strtotime($this->booking_end_time); $i = $i + $this->booking_frequency * 60) {
$this->slots_per_day ++;
}
$stmt->close();
$this->make_days_array($year, $month);
} // Close function
function make_days_array($year, $month) {
// Calculate the number of days in the selected month
$num_days_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
// Make $this->days array containing the Day Number and Day Number in the selected month
for ($i = 1; $i <= $num_days_month; $i++) {
// Work out the Day Name ( Monday, Tuesday... ) from the $month and $year variables
$d = (mktime(0, 0, 0, $month, $i, $year));
// Create the array
$this->days[] = array("daynumber" => $i, "dayname" => date("l", $d));
}
/*
Sample output of the $this->days array:
[0] => Array
(
[daynumber] => 1
[dayname] => Monday
)
[1] => Array
(
[daynumber] => 2
[dayname] => Tuesday
)
*/
$this->make_blank_start($year, $month);
$this->make_blank_end($year, $month);
} // Close function
function make_blank_start($year, $month) {
/*
Calendar months start on different days
Therefore there are often blank 'unavailable' days at the beginning of the month which are showed as a grey block
The code below creates the blank days at the beginning of the month
*/
// Get first record of the days array which will be the First Day in the month ( eg Wednesday )
$first_day = $this->days[0]['dayname']; $s = 0;
// Loop through $day_order array ( Monday, Tuesday ... )
foreach($this->day_order as $i => $r) {
// Compare the $first_day to the Day Order
if($first_day == $r && $s == 0) {
$s = 1; // Set flag to 1 stop further processing
} elseif($s == 0) {
$blank = array(
"daynumber" => 'blank',
"dayname" => 'blank'
);
// Prepend elements to the beginning of the $day array
array_unshift($this->days, $blank);
}
} // Close foreach
} // Close function
function make_blank_end($year, $month) {
/*
Calendar months start on different days
Therefore there are often blank 'unavailable' days at the end of the month which are showed as a grey block
The code below creates the blank days at the end of the month
*/
// Add blank elements to end of array if required.
$pad_end = 7 - (count($this->days) % 7);
if ($pad_end < 7) {
$blank = array(
"daynumber" => 'blank',
"dayname" => 'blank'
);
for ($i = 1; $i <= $pad_end; $i++) {
array_push($this->days, $blank);
}
} // Close if
$this->calendar_top();
} // Close function
function calendar_top() {
// This function creates the top of the table containg the date and the forward and back arrows
echo "
<div id='lhs'><div id='outer_calendar'>
<table border='0' cellpadding='0' cellspacing='0' id='calendar'>
<tr id='week'>
<td align='left'><a href='?month=" . date("m", $this->back) . "&first_name=" . $_GET['first_name'] . "&email=" . $_GET['email'] . "&last_name=" . $_GET['last_name'] . "&telephone=" . $_GET['telephone'] . "&user_id=" . $_GET['user_id'] . "&uid=" . $_GET['uid'] . "&rsvnumb=" . $_GET['rsvnumb'] . "&pon=" . $_GET['pon'] . "&page_id=" . $_GET['page_id'] . "&year=" . date("Y", $this->back) . "'>«</a></td>
<td colspan='5' id='center_date'>" . date("F, Y", $this->selected_date) . "</td>
<td align='right'><a href='?month=" . date("m", $this->forward) . "&first_name=" . $_GET['first_name'] . "&email=" . $_GET['email'] . "&last_name=" . $_GET['last_name'] . "&telephone=" . $_GET['telephone'] . "&user_id=" . $_GET['user_id'] . "&uid=" . $_GET['uid'] . "&rsvnumb=" . $_GET['rsvnumb'] . "&pon=" . $_GET['pon'] . "&page_id=" . $_GET['page_id'] . "&year=" . date("Y", $this->forward) . "'>»</a></td>
</tr>
<tr>";
/*
Make the table header with the appropriate day of the week using the $day_format variable as user defined above
Definition:
1: Show First digit, eg: "M"
2: Show First 3 letters, eg: "Mon"
3: Full Day, eg: "Monday"
*/
foreach($this->day_order as $r) {
switch($this->day_format) {
case(1):
echo "<th>" . substr($r, 0, 1) . "</th>";
break;
case(2):
echo "<th>" . substr($r, 0, 3) . "</th>";
break;
case(3):
echo "<th>" . $r . "</th>";
break;
} // Close switch
} // Close foreach
echo "</tr>";
$this->make_cells();
} // Close function
function make_cells($table = '') {
echo '<h3>Επέλεξε μία μέρα</h3>';
echo "<tr>";
foreach($this->days as $i => $r) { // Loop through the date array
$j = $i + 1; $tag = 0;
// If the the current day is found in the day_closed array, bookings are not allowed on this day
if(in_array($r['dayname'], $this->day_closed)) {
echo "\r\n<td width='21' valign='top' class='closed'>" . $this->day_closed_text . "</td>";
$tag = 1;
}
// Past days are greyed out
if (mktime(0, 0, 0, $this->month, sprintf("%02s", $r['daynumber']) + 1, $this->year) < strtotime("now") && $tag != 1) {
echo "\r\n<td width='21' valign='top' class='past'>";
// Output day number
if($r['daynumber'] != 'blank') echo $r['daynumber'];
echo "</td>";
$tag = 1;
}
// If the element is set as 'blank', insert blank day
if($r['dayname'] == 'blank' && $tag != 1) {
echo "\r\n<td width='21' valign='top' class='unavailable'></td>";
$tag = 1;
}
// Now check the booking array $this->booking to see whether we have a booking on this day
$current_day = $this->year . '-' . $this->month . '-' . sprintf("%02s", $r['daynumber']);
if(isset($this->bookings_per_day[$current_day]) && $tag == 0) {
$current_day_slots_booked = count($this->bookings_per_day[$current_day]);
if($current_day_slots_booked < $this->slots_per_day) {
echo "\r\n<td width='21' valign='top'>
<a href='reservation.php?month=" . $this->month . "&year=" . $this->year . "&first_name=" . $_GET['first_name'] . "&email=" . $_GET['email'] . "&last_name=" . $_GET['last_name'] . "&rsvnumb=" . $_GET['rsvnumb'] . "&telephone=" . $_GET['telephone'] . "&pon=" . $_GET['pon'] . "&user_id=" . $_GET['user_id'] . "&uid=" . $_GET['uid'] . "&page_id=" . $_GET['page_id'] . "&day=" . sprintf("%02s", $r['daynumber']) . "' class='part_booked' title='This day is part booked'>" .
$r['daynumber'] . "</a></td>";
$tag = 1;
} else {
echo "\r\n<td width='21' valign='top'>
<a href='reservation.php?month=" . $this->month . "&year=" . $this->year . "&first_name=" . $_GET['first_name'] . "&email=" . $_GET['email'] . "&last_name=" . $_GET['last_name'] . "&rsvnumb=" . $_GET['rsvnumb'] . "&telephone=" . $_GET['telephone'] . "&pon=" . $_GET['pon'] . "&user_id=" . $_GET['user_id'] . "&uid=" . $_GET['uid'] . "&page_id=" . $_GET['page_id'] . "&day=" . sprintf("%02s", $r['daynumber']) . "' class='fully_booked' title='This day is fully booked'>" .
$r['daynumber'] . "</a></td>";
$tag = 1;
} // Close else
} // Close if
if($tag == 0) {
echo "\r\n<td width='21' valign='top'>
<a href='reservation.php?month=" . $this->month . "&year=" . $this->year . "&first_name=" . $_GET['first_name'] . "&email=" . $_GET['email'] . "&last_name=" . $_GET['last_name'] . "&rsvnumb=" . $_GET['rsvnumb'] . "&telephone=" . $_GET['telephone'] . "&pon=" . $_GET['pon'] . "&user_id=" . $_GET['user_id'] . "&uid=" . $_GET['uid'] . "&page_id=" . $_GET['page_id'] . "&day=" . sprintf("%02s", $r['daynumber']) . "' class='green' title='Please click to view bookings'>" .
$r['daynumber'] . "</a></td>";
}
// The modulus function below ($j % 7 == 0) adds a <tr> tag to every seventh cell + 1;
if($j % 7 == 0 && $i >1) {
echo "\r\n</tr>\r\n<tr>"; // Use modulus to give us a <tr> after every seven <td> cells
}
}
echo "</tr></table></div><!-- Close outer_calendar DIV -->";
if(isset($_GET['year']))
$this->basket();
echo "</div><!-- Close LHS DIV -->";
// Check booked slots for selected date and only show the booking form if there are available slots
$current_day = $this->year . '-' . $this->month . '-' . $this->day;
$slots_selected_day = 0;
if(isset($this->bookings_per_day[$current_day]))
$slots_selected_day = count($this->bookings_per_day[$current_day]);
if($this->day != 0 && $slots_selected_day < $this->slots_per_day) {
$this->booking_form();
}
} // Close function
Have someone any idea about this script and how can i change the language of the month (greek)?
The date() function can only use English for its outputs. You'd have to use strftime() instead, while setting the locale with setlocale(). This will output in the language of the set locale, in this case - greek.
The two have somewhat different formats, but aren't altogether that different. A brief example is given below.
setlocale(LC_TIME, 'el_GR.UTF-8'); // Set the locale to greek
echo strftime("%A", $d); // Outputs weekname, Monday through Sunday
echo strftime("%m", $this->back) // Outputs month, numerically, e.g. "10"
echo strftime("%B, %Y", $this->selected_date); // Outputs e.g "October, 2016"
// These comments, about the formats, are in English,
// because I don't know the Greek translation
// It will be in Greek should the locale be installed and properly set
Change the date() functions to strftime() instead (with the new parameters, found in the documentation), and you should be good!
If the above doesn't work, it's because that locale (el_GR, for Greek) hasn't been installed on your server.
References
http://php.net/manual/en/function.strftime.php
http://php.net/manual/en/function.setlocale.php
Thank you for your answer. i tried setlocale(LC_TIME, 'el_GR.UTF-8'); but nothing happend. I have tried setlocale(LC_ALL, 'greek'); but the greek words are ???????.When i encode page to windows-1253 i can see the month in greek language but this is not a solution. What can i do?
EDIT:
I found the solution
$date_encoded = strftime('%B %Y', $this->selected_date);
$date_encoded = iconv('Windows-1253', 'UTF-8//IGNORE', $date_encoded);
How we can change the default time to dynamic using mysqli db
// Time Related Variables
public $booking_start_time = "09:00"; // The time of the first slot in 24 hour H:M format
public $booking_end_time = "21:00"; // The time of the last slot in 24 hour H:M format

How do I auto increase a number in increments of one using an IF statement?

I am trying to echo a variable number, but I am not sure the best way to go about it. My problem is this:
I have a series of 'IF' conditions and I echo a variable accordingly.
if ($month == "8" || $month == "9" || $month == "10") {
$comingseason = 'winter';
$elementsDue = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "11" || $month == "12" || $month == "1") {
$comingseason = 'spring';
$elementsDue = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "2" || $month == "3" || $month == "4") {
$comingseason = 'summer';
$elementsDue = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. 9";
}
else if ($month == "5" || $month == "6" || $month == "7") {
$comingseason = 'fall';
$elementsDue = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. 9";
}
I am wondering how I would replace the number in 'issue No. 9' with the next increment ++1 to output '10'? I don't want to have to come back into the code each time and type in the next issue number. I would like the number to increase on its own, essentially every quarter like you see below.
I was thinking of a php session, but from what I understand that will not be permanent. I need the number to remain even after a user has ended their session.
How can I accomplish this?
Looks to me like the issue # is a function of year and quarter: you have 1 issue per quarter starting at some base year/quarter. So, something like this should automatically calculate it (you'll have to play with $base_year and $base_qtr to get it to match your issue numbering).
function currentIssue() {
$base_year = 2014;
$base_qtr = 2;
$now_year = intval(date('Y');
$now_qtr = ceil(intval(date'm')/3);
return ($now_year - $base_year)*4 + ($now_qtr - $base_qtr);
}
Ok this seems to be a very simple problem to solve. I would suggest reading a good entry level PHP programming book. Having said that, you need to set a while loop and some nested if else conditions.
$month = 0;
while ($month < 12) {
$month++;
If ($month > 7 && $month < 11)
{
$comingsoon = "winter";
}
elseif
{
some logic
}
elseif
{
some logic
}
else
{
}
} # end of while loop
I am intentionally leaving the remaining code for you to write. It would be a good exercise for you to learn some coding :) Good Luck!
may be you're looking for this,
Explanation: add an integer variable say it is $issuNo, and than add a initial value of that. Then after if/else condition block put the incremental var of that like $issuNo++ and then you better make a program that can continue your if/else statement later.
$issuNo = 9;
if ($month == "8" || $month == "9" || $month == "10") {
$comingseason = 'winter';
$elementsDue = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. ".$issuNo;
}
else if ($month == "11" || $month == "12" || $month == "1") {
$comingseason = 'spring';
$elementsDue = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. ".$issuNo;
}
else if ($month == "2" || $month == "3" || $month == "4") {
$comingseason = 'summer';
$elementsDue = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. ".$issuNo;
}
else if ($month == "5" || $month == "6" || $month == "7") {
$comingseason = 'fall';
$elementsDue = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. ".$issuNo;
}
$issuNo++;
If this is what you're looking for:
<?php
$month = '3';
$year = '2015';
$issue = 9;
if ($month == "8" || $month == "9" || $month == "10") {
$comingseason = 'winter';
echo $elementsDue = "Due October 31, " . $year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "11" || $month == "12" || $month == "1") {
$comingseason = 'spring';
$issue = $issue + 1;
echo $elementsDue = "Due January 31, " . ++$year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "2" || $month == "3" || $month == "4") {
$comingseason = 'summer';
$issue = $issue + 2;
echo $elementsDue = "Due April 30, " . $year . ", for our " . $comingseason . " issue No. $issue";
}
else if ($month == "5" || $month == "6" || $month == "7") {
$comingseason = 'fall';
$issue = $issue + 3;
echo $elementsDue = "Due July 31, " . ++$year . ", for our " . $comingseason . " issue No. $issue";
}
?>
PHP fiddle

getting next/previous record via jquery and php

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.

Compare 2 timestamps and set a statement based on the result

I have the following table:
-------------------------------------------
|id|list_id|start_date|end_date|min_nights|
-------------------------------------------
|17| 55 |1437487200|1437735600|3|
|18| 55 |1438005600|1438167600|2|
I want to display the minimum nights for each range.
PHP:
//Seasonal Price
//1. Store all the dates between checkin and checkout in an array
$checkin_time = get_gmt_time(strtotime($checkin));
$checkout_time = get_gmt_time(strtotime($checkout));
$travel_dates = array();
$seasonal_prices = array();
$total_nights = 1;
$total_price = 0;
$is_seasonal = 0;
$i = $checkin_time;
while ($i < $checkout_time) {
$i = get_gmt_time(strtotime('+1 day', $i));
$checkin_date = date('m/d/Y', $i);
$checkin_date = explode('/', $checkin_date);
$travel_dates[$total_nights] = $checkin_date[1] . $checkin_date[0] . $checkin_date[2];
$total_nights++;
}
for ($i = 1; $i < $total_nights; $i++) {
$seasonal_prices[$travel_dates[$i]] = "";
}
//Store seasonal price of a list in an array
$seasonal_query = $this->Common_model->getTableData('seasonalprice', array('list_id' => $id));
//vaild array checked ilan
$seasonal_result = $seasonal_query->result_array();
if ($seasonal_query->num_rows() > 0) {
foreach ($seasonal_result as $time) {
//Get Seasonal price
$seasonalprice_query = $this->Common_model->getTableData(
'seasonalprice', array(
'list_id' => $id,
'start_date' => $time['start_date'],
'end_date' => $time['end_date']
));
$seasonalprice = $seasonalprice_query->row()->price;
$seasonal_min_nights = $seasonalprice_query->row()->min_nights;
//Days between start date and end date -> seasonal price
$start_time = $time['start_date'];
$end_time = $time['end_date'];
$i = $start_time;
while ($i <= $end_time) {
$start_date = date('m/d/Y', $i);
$s_date = explode('/', $start_date);
$s_date = $s_date[1] . $s_date[0] . $s_date[2];
$seasonal_prices[$s_date] = $seasonalprice;
$i = get_gmt_time(strtotime('+1 day', $i));
}
}
Then there are the following conditions:
//half-day booking functionality
//add time up to 2PM
$checkin_timestamp = get_gmt_time(strtotime($checkin)) + (14 * 60 * 60);
//add time up to 11AM
$checkout_timestamp = get_gmt_time(strtotime($checkout)) + (11 * 60 * 60);
$query = $this->db->query('SELECT id, list_id FROM `calendar` WHERE `list_id` = "' . $id . '" AND (`booked_days` BETWEEN ' . $checkin_timestamp . ' AND ' . $checkout_timestamp . ') GROUP BY `list_id`');
$rows = $query->num_rows();
$daysexist = $this->db->query("SELECT id,list_id,booked_days FROM `calendar` WHERE `list_id` = '" . $id . "' AND (`booked_days` BETWEEN " . $checkin_timestamp . " AND " . $checkout_timestamp . ") GROUP BY `list_id`");
$rowsexist = $daysexist->num_rows();
if ($rowsexist > 0) {
echo '{"available":false,"total_price":' . $data['price'] . ',"reason_message":"Those dates are not available"}';
}
if ($data['guests'] > $capacity) {
echo '{"available":false,"total_price":' . $data['price'] . ',"reason_message":"' . $capacity . ' guest(s) only allowed"}';
}
elseif ($is_seasonal == 1 && $total_nights -1 < $seasonal_min_nights) {
echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_nights_seasonal'] . ' nights for the following period: '.date('m/d/Y', $start_time).' - '.date('m/d/Y', $end_time).'"}';
exit;
}
$this->session->set_userdata("total_price_'" . $id . "'_'" . $this->dx_auth->get_user_id() . "'", $data['price']);
$staggered_price = "";
if ($days >= 30) {
$staggered_price = ',"staggered_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']) . '","staggered":false';
}
elseif (isset($extra_guest)) {
if ($extra_guest == 1) {
echo '{"service_fee":"' . get_currency_symbol($id) . get_currency_value_lys($row->currency, get_currency_code(), $data['commission']) . '","extra_guest_price":"' . get_currency_symbol($id) . get_currency_value1($id, $extra_guest_price) . '","extra_guest":1,"reason_message":"","price_per_night":"' . get_currency_symbol($id) . get_currency_value1($id, $per_night) . '","nights":' . $days . ',"available":true,"can_instant_book":false,"total_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']) . '"' . $staggered_price . '}';
}
}
else {
echo '{"service_fee":"' . get_currency_symbol($id) . get_currency_value_lys($row->currency, get_currency_code(), $data['commission']) . '","reason_message":"","price_per_night":"' . get_currency_symbol($id) . get_currency_value1($id, $per_night) . '","nights":' . $days . ',"available":true,"can_instant_book":false,"total_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']).'"}';
}
And the condition i setted up:
elseif ($is_seasonal == 1 && $total_nights -1 < $seasonal_min_nights) {
echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_nights_seasonal'] . ' nights for the following period: '.date('m/d/Y', $start_time).' - '.date('m/d/Y', $end_time).'"}';
exit;
}
And it works fine as long as i have only 1 custom range, in the database there are 2 entries. but the condition i have made always show me the last information that was added to the database instead of the information that applies for the specific timestamp range.
How would i modify the condition to meet with the timestamps and retrive the min_nights foreach timestamps range? Thanks!
I have solved it myself, looped the timestamps and in the loop i setted up the variables i need to work with, then the condition applied to each range of the timestamp individually.. If anybody have similar situation:
Getting the min_nights
$seasonal_min_nights = $seasonalprice_query->row()->min_nights;
Looping in a while:
$start_time = $time['start_date'];
$end_time = $time['end_date'];
$i = $start_time;
while ($i <= $end_time) {
$start_date = date('m/d/Y', $i);
$s_date = explode('/', $start_date);
$s_date = $s_date[1] . $s_date[0] . $s_date[2];
$seasonal_nights[$s_date] = $seasonal_min_nights;
$i = get_gmt_time(strtotime('+1 day', $i));
}
And for loop:
//Total Price
for ($i = 1; $i < $total_nights; $i++) {
$min_seasonal_nights = $seasonal_nights[$travel_dates[$i]];
$is_seasonal = 1;
}
Finally the statment:
if ($is_seasonal == 1 && $total_nights - 1 < $min_seasonal_nights) {
echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_seasonal_nights'] . ' nights for the following period: ' . date('m/d/Y', $data['s_start_date']) . ' - ' . date('m/d/Y', $data['s_end_date']) . '"}';
exit;
}
I would do other stype of coding for this but had to work with the existing.. so hope it helps anybody :)

Categories