PHP For Loop, Compare Incrementing Value to Incrementing Array - php

Short question: I am trying to see if a variable ($y) is between multiple array values from a database ($ending[0] && $starting[0], $ending[1] && $starting[1], etc...). "If" $y is between any of those, echo "statement".
I'm using a for loop to increment a drop down in 30 minute intervals from 9am - 7pm.
If any of those times = a time saved within my database, it echoes "not available" instead.
Now I need to see "if" the dropdown time is "between" the start and end times in the database and make those "not available" as well.
Everything works if I manually identify which array keys to compare...but my question is: can I check the drop down value against all of the values in the arrays?
Example:
Start Dropdown: 09:00am 09:30am 10:00am etc up until 07:30pm. End Dropdown: 09:30am 10:00am 10:30am etc. up until 08:00pm. If there is a start and end time of 09:00am(start) 10:00am(end) saved in the database for an appointment, the dropdown will show "Not available" for the corresponding starting and end times, so a user cannot double book an appointment. What I am asking about, is how to identify all of the "in between" values...09:30am for example, cannot be a start or an end time, if the 09:00am-10:00am slot is already booked.
$sql_slots = "SELECT * FROM XXXXX WHERE date = '$query_date' ORDER BY XXXXX ASC";
$result_slots = mysqli_query($connection, $sql_slots);
$open = strtotime("9:00am");
$close = strtotime("9:30am");
$starting = array();
$ending = array();
while($row_result_slots = mysqli_fetch_assoc($result_slots)){
$starting[] = $row_result_slots['start_time'];
$ending[] = $row_result_slots['end_time'];
}
echo'
<select name="start_time">';
for($b = 0; $b <= 20; $b++){
$y = strtotime(($b*30) . " Minutes", $open);
$the_time = date("h:ia", $y);
// Here, instead of only comparing the first values in the array, I need to match it up against $ending[1] & $starting[1], $ending[2] & $starting[2], etc...
if(in_array($the_time, $starting) || ($y <= strtotime($ending[0]) && $y >= strtotime($starting[0]))){
echo "<option>Not Available<br></option>";
} else {
echo "<option>" . $the_time . "<br></option>";
}
}
echo'</select>';

How about that?
$sql_slots = "SELECT * FROM XXXXX WHERE date = '$query_date' ORDER BY XXXXX ASC";
$result_slots = mysqli_query($connection, $sql_slots);
$open = strtotime("9:00am");
$close = strtotime("9:30am");
$reserved = array();
while($row_result_slots = mysqli_fetch_assoc($result_slots)){
$start = $row_result_slots['start_time'];
while ($start <= $row_result_slots['end_time']) {
$reserved[$start] = true;
$start = date("Y/m/d h:i:s", strtotime($start . "+30 minutes"));
}
}
echo '<select name="start_time">';
for($b = 0; $b <= 20; $b++){
$y = strtotime(($b*30) . " Minutes", $open);
$the_time = date("h:ia", $y);
if(isset($reserved[$the_time]){
echo "<option>Not Available<br></option>";
} else {
echo "<option>" . $the_time . "<br></option>";
}
}
echo'</select>';

Below is what I came up with. This uses a do while loop and creates a reserved array that will fill all time between what is given in the appointment.
I updated the data to use my own array but updated your mysqli work with what should be correct. If not I left in my testing code for you to view if needed. I also used the disabled attribute on an option over outputting some text.
<?php
$sql_slots = "SELECT * FROM XXXXX WHERE date = '$query_date' ORDER BY XXXXX ASC";
$result_slots = mysqli_query($connection, $sql_slots);
$open = strtotime("9:00am");
$close = strtotime("7:00pm");
$space = 900; //15min * 60 seconds space between?
//This seems more normal for people to see.
//Simulate these appoinments
/*
$query_results = array(
array(
"start_time" => "9:00am",
"end_time" => "9:30am"
),
array(
"start_time" => "10:30am",
"end_time" => "11:30am"
),
array(
"start_time" => "11:45am",
"end_time" => "12:30pm"
)
);
*/
$reserved = array();
while($row_result_slots = mysqli_fetch_assoc($result_slots)){
//**NOTE**: Make sure these are times or use strtotime()
$next_slot = $reserved[] = strtotime($row_result_slots['start_time']);
$end_time = strtotime($row_result_slots['end_time']);
while($next_slot < $end_time){
$next_slot += $space;
$reserved[] = $next_slot;
}
}
/*
foreach($query_results as $appoinment) {
$next_slot = $reserved[] = strtotime($appoinment['start_time']);
$end_time = strtotime($appoinment['end_time']);
while($next_slot < $end_time){
$next_slot += $space;
$reserved[] = $next_slot;
}
}
*/
echo'<select name="start_time">';
$current = $open;
do {
$disabled = in_array($current, $reserved) ? 'disabled' : '';
echo '<option ' . $disabled . '>' . date("h:ia", $current) . '</option>';
$current += $space;
} while($current <= $close);
echo'</select>';

Related

php counter on mysql results

I got this simple foreach loop and can't figure out where is the problem with counter.
I get results like this. I am trying to make counter enlarged for one if it meets the conditions.
$building = 5;
$todaysdate = date("Y-m-d");
$tomorrows_date = date("Y-m-d", strtotime($todaysdate . "+1 days"));
$ends_date = "2018-04-30";
$counter = 0;
$query = "SELECT * FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC"; // results give me numbers from 30 to 110.
$querydone = $db->query($query);
while($row = $querydone->fetch_assoc()) {
$every_id[$row['sifra']] = $row;
}
$firstday = new DateTime($tomorrows_date);
$lastday = new DateTime($ends_date);
for($q = $firstday; $q <= $lastday; $q->modify('+1 day')){
$result_day = $q->format("Y-m-d");
$i = 0; // counter for every value from mysql
foreach ($every_id as $key => $value) {
$counter = ${$i++} = $value['sifra'];
if($building >= $i) {
$valuesResult = "('$result_day','$counter')" . "<br />";
} else {
break;
}
echo $valuesResult;
}
}
Where am I wrong?
$building = 5;
$todaysdate = date("Y-m-d");
$tomorrows_date = date("Y-m-d", strtotime($todaysdate . "+1 days"));
$ends_date = "2018-04-30";
$counter = 0;
$query = "SELECT * FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC LIMIT 80"; // results give me numbers from 30 to 110.
$querydone = $db->query($query);
while($row = $querydone->fetch_assoc()) {
$every_id = $row['sifra'];
}
$firstday = new DateTime($tomorrows_date);
$lastday = new DateTime($ends_date);
for($q = $firstday; $q <= $lastday; $q->modify('+1 day')){
$result_day = $q->format("Y-m-d");
$i = 0; // counter for every value from mysql
foreach ($every_id as $key => $value) {
$counter = $i++;
if($building >= $i) {
$valuesResult = "('$result_day','$counter')" . "<br />";
} else {
break;
}
echo $valuesResult;
}
}

WooCommerce Bookings - Available dates as drop down menu

I'm trying to customize the booking experience. My products are tours spread over the year and scrolling through the calendar to find an available day isn't efficient. What I want is a drop down list that has the next available day for that tour. Is there any plugin or solution for my situation?
Hi The file is located in wp-content/plugins/woocommerce-bookings/templates/booking-form
The code in the post described works for the most part but there are some errors
Warning: reset() expects parameter 1 to be array, string given in /home/betaacademyofflo/public_html/wp-content/plugins/woocommerce-bookings/templates/booking-form/date-picker.php on line 54
Warning: Variable passed to each() is not an array or object in /home/betaacademyofflo/public_html/wp-content/plugins/woocommerce-bookings/templates/booking-form/date-picker.php on line 55
This error appears for every date available.
A tweak that I am trying to make is that currently the select list shows duplicates of each date I need it to just show the date once.
By each date once I mean just the start date my bookings have the same start and end date.
You can see an example here example list I have suppresed the errors for now.
heres the code so far pretty much the same as the OP version on wordress support.
<?php
$year = array();
$month = array();
$days = array();
$s;
$day1;
$i=0;
$j=0;
foreach ($availability_rules as $value) {
foreach ( $value as $value2) {
foreach ( $value2 as $value3) {
reset($value3);
while (list($key, $val) = each($value3)) {
$year[$i] = $key; //add year to array - $i is the count of how many course
reset($val);
while (list($key2, $val2) = each($val)) {
if($key2 < 10){
$month[$i] = '0'.$key2; //add the month value to another array - with leading 0
}else{
$month[$i] = $key2; //add the month value to another array
}
$s = "";
$j = 0;
reset($val2);
while (list($key3, $val3) = each($val2)) {
if($j==0||$j==1){
$s = $s . $key3 .',';
}else{
$s = $s . $key3;
}
if($j==0){
$day1[$i] = $key3; //add the day value to another array
}
$j++;
}
$days[$i] = $s;
}
$i++; //increments each time we loop through a year
}
}
}
}
$arrlength = 0;
$arrlength = count($year); //this is our total amount of courses
?>
<fieldset class="wc-bookings-date-picker <?php echo implode( ' ', $class ); ?>">
<legend><?php echo $label; ?>: </small></legend>
<select id="availableDates">
<option value="">Select Date</option>
<?php
if($arrlength==0){
echo "<option value=''>There are no dates</option>";
}else{
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
for($total_dates = $arrlength -1; $total_dates >= 0; $total_dates--){ //loop through total amount
$expiration_date =strtotime($day1[$total_dates].'-'.$month[$total_dates].'-'.$year[$total_dates]);
$actualdates = $day1[$total_dates]-$month[$total_dates]-$year[$total_dates];
$displaydates = $day1[$total_dates]/$month[$total_dates]/$year[$total_dates];
//$input = array( $day1[$total_dates]-$month[$total_dates]-$year[$total_dates]);
//$result = array_keys($input);
if ($expiration_date > $today) {
echo "<option value='$day1[$total_dates]-$month[$total_dates]-$year[$total_dates]'>$day1[$total_dates]/$month[$total_dates]/$year[$total_dates]</option>"; //pull in the index for each date array
}
}
}
?>
What I've tried;
Running an extra foreach on the final result did not work
tried array_unique did not work
Anny guidance here would be most aprreciated.

Check for multiple rows - PHP & MySQL

I am cycling through cells in a HTML table using PHP to color specific cells.
I want to color the cells that return true to this statement:
if ($currentCell >= $reservationStartDayOfMonth && $currentCell <= $reservationEndDayOfMonth) {
make red
} else {
make normal color
}
This is not the exact statement, but the idea of it.
I get the reservationEndDayOfMonth and reservationStartDayOfMonth from a MySQL server.
For each row in the SQL server, there is a new reservation with a start and an end date. Along with the id (1...2...3...)
Basically I got a reservation in the current month. And it works when I just cycle through the cells with the data from ONE reservation. But I got lots of reservations in my database. I need to check and see if the current cell corresponds to any of the many reservations I have.
My code is long and bulky. It's basically just a table with all the checkers and parameters inside. Here you go:
<table border="1" id="calendar">
<?php
$dk_database = array("Januar","Februar","Marts","April","Maj","Juni","Juli","August","September","Oktober","November","December");
$dk_month = $dk_database[(int)date("m")-1];
ECHO '<caption><h1 style="color:#554100;">' . $dk_month . '</h1></caption>';
$sqldateStart = sqlRequest("SELECT res_datestart FROM spottrup_reservations WHERE res_id = 1");
$dateStartRaw = mysql_fetch_array($sqldateStart);
$dateStartArray = explode("-", $dateStartRaw[0]);
$dateStartDay = (int)$dateStartArray[2];
$dateStartMonth = (int)$dateStartArray[1];
$dateStartYear = (int)$dateStartArray[0];
$sqldateEnd = sqlRequest("SELECT res_dateend FROM spottrup_reservations WHERE res_id = 1");
$dateEndRaw = mysql_fetch_array($sqldateEnd);
$dateEndArray = explode("-", $dateEndRaw[0]);
$dateEndDay = (int)$dateEndArray[2];
$dateEndMonth = (int)$dateEndArray[1];
$dateEndYear = (int)$dateEndArray[0];
$currentYear = (int)date("y")+2000;
$currentDate = (int)date("d");
$currentMonth = (int)date("m");
$columnsInRow = 7;
$daysInMonth = date("t");
$currentDay = 1;
for ($i=0 ; $i < $daysInMonth ; $i++) {
ECHO '<tr class="cal">';
for ($j=0 ; $j < $columnsInRow && $currentDay <= $daysInMonth ; $j++) {
if($currentDate==$currentDay) {
if($currentDay>=$dateStartDay && $currentDay<=$dateEndDay && $currentMonth==$dateStartMonth && $currentYear==$dateStartYear) {
ECHO '<div style="background:blue;height:100%;width:100%text-decoration:none;"><td class="cal"><h2 style="color:red;">' . $currentDay . '</h2></td></div>';
}else{
ECHO '<div style="background:#D4BB6A;height:100%;width:100%text-decoration:none;"><td class="cal"><h2 style="color:red;">' . $currentDay . '</h2></td></div>';
}
}else{
if($currentDay>=$dateStartDay && $currentDay<=$dateEndDay && $currentMonth==$dateStartMonth && $currentYear==$dateStartYear) {
ECHO '<div style="background-color:blue;height:100%;width:100%text-decoration:none;"><td class="cal" style="background-color:#FF8080;"><h2 style="color:#554100;">' . $currentDay . '</h2></td></div>';
}else{
ECHO '<div style="background-color:#D4BB6A;height:100%;width:100%text-decoration:none;"><td class="cal"><h2 style="color:#554100;">' . $currentDay . '</h2></td></div>';
}
}
$currentDay++;
}
ECHO '</tr>';
}
?>
The sqlRequest(string); calls a function that returns the result of the query you give.
Not the most efficient way since lots of queries but it does reduce a for loop, but you could run a query for each day to see if there is a reservation for it. Simple to implement.
for ($i=0 ; $i < $daysInMonth ; $i++)
{
...
$query = "Select * from table where currentDate >= startDate and currentDate <= endDate"
...
}

For loop Logic Problems with PHP

This is my new code, its almost working, but I think my while loop is wrong some where?
Same as before, gets users taken sick puts it into a var. Then get users entitlement based on years of service, then work out what they have taken and see if the user has used all the full entitlement, then to see if they have used all there half pay entitlement.
Please Help?
if($this->CURRENT_USER['User']['role_id'] > 3) { //locks out user types
//Get Holidaytypes
$types = $this->Holiday->find(
'all',
array(
'conditions' => array(
'Holiday.holidaystype_id' => 3,
'Holiday.user_id' => $id
)));
//Get starting date
$contracts = $this->Holiday->User->Contract->find(
'all',
array(
'conditions' => array(
'Contract.user_id' => $id//$data['user_id']),
'order' => array('Contract.startson' => 'ASC')
)
);
//Get How Many sick days
foreach ($types as $key => $value) {
global $SickTotal;
$typesDataEnds = strftime ("%u-%d-%Y", $types[$key]['Holiday']['endson']);
$typesDataStarts = strftime ("%u-%d-%Y", $types[$key]['Holiday']['startson']);
$SickTotal = count($typesDataEnds - $typesDataStarts);
//echo $SickTotal;
//Get Contract Start & End Dates
$start = array_shift($contracts);
$end = array_pop($contracts);
$endDate = $end['Contract']['endson'];
$startDate = $start['Contract']['startson'];
if (empty($endDate)) {
$endDate = time('now');
}
if (!empty($startDate)) {
$SortEnd = strftime("%Y", $endDate);
$SortStart = strftime("%Y", $startDate);
$YearsService = $SortEnd - $SortStart;
if ($YearsService <= 1) {
$SetFullEntitlement = 5;
$SetHalfEntitlement = 5;
//echo 'one year';
} elseif ($YearsService >= 2) {
$SetFullEntitlement = 10;
$SetHalfEntitlement = 10;
//echo 'two years';
} elseif ($YearsService >= 5) {
$SetFullEntitlement = 20;
$SetHalfEntitlement = 20;
//echo 'up to five years';
} elseif ($YearsService > 5) {
$SetFullEntitlement = 30;
$SetHalfEntitlement = 30;
//echo 'five years or more';
} else {
$SetFullEntitlement = 0;
$SetHalfEntitlement = 0;
//echo 'no sick pay';
}
} else {
$YearsService = 0;
//echo 'Sorry No Start Date For You Found!';
}
while ($SickTotal > 0) {
if ($SetFullEntitlement != 0) {
$SetFullEntitlement--;
} elseif ($SetHalfEntitlement != 0) {
$SetHalfEntitlement--;
}
}
echo 'FullPay:';
echo $SetFullEntitlement;
echo '<br/><br/>Halpay:';
echo $SetHalfEntitlement;
echo $SickTotal;
}
debug($types);
die();
//$this->render('/artists/holidayslist');
}
}
If ($startdate <= 1 year) {
If that's literally what you've typed, it's not going to work. Maybe strtotime might make some sense of it?
In any case,
For ($i = $Totalsick, $i >= $Fulldays, $i--) {
For ($i = $Totalsick, $>= $Halfdays, $i--) {
you're missing an $i in there - you're evaluating nothing against $Halfdays. You're also using $i for two seperate loops, so they're both on the same counter. switch your second loop to use a different variable.
I'm not quite sure I follow exactly what you are trying to do here but you could tidy up your code a bit and save on lines of code. I assume that $startdate is how many years ago the user started.
$sickdays= array( 0=>5, 1=>10, 2=>20, 5=>30 );
$userdays= 0;
foreach ( array_keys( $sickdays ) as $years_service )
{
if ( $years_service <= $startdate )
$userdays=$sickdays[$years_service];
}
Now $userdays should be the correct number of paid sick days and half days ( the two are always the same in your example ) for this user. A simple comparison should be sufficient to check whether they have taken less or more than their allotted number of days and half days.
I haven't tried this as I'm not working with PHP today, so I'm sure someone will shoot me down directly, but by setting your variables once and writing fewer lines you should find your code gets easier to maintain.

Calculate age from datebirth sql field?

This is the query I am using and it is in page 1 not inside the function.
<?php
$sql2= mysql_query("SELECT *
FROM catego WHERE category_id = '$idc'");
$categoryCount = mysql_num_rows($sql2);
if ($categoryCount>0 )
{
$row2 = mysql_fetch_array($sql2);
$id = $row2["category_id"];
$birthdate = $row2["birthdate"];
$address = $row2["address"];
}
?>
SELECT YEAR(SUBDATE(NOW(), INTERVAL DATE_FORMAT("1975,09,02", "%Y-%m") YEAR_MONTH)) fs_age;
A simple php function could be something like the following:
function birth_date ($birth_date){
list($y,$m,$d) = explode(",",$birth_date);
$y_diff = date("Y") - $y;
$m_diff = date("m") - $m;
$d_diff = date("d") - $d;
if ($m_diff < 0 || $d_diff < 0) { $y_diff--; }
return $y_diff;
}
i.e split on your commas, work out the differences, adjust for which side of their birthdate the day/month is, if so reduce the year by one, then just return the year.
Then in your page you can put:
<li><span class="label">Age:</spa><?php$age = getage($birthdate); echo $age;?> </li>
EDIT:
<?php
function birth_date($birth_date){
list($y,$m,$d) = explode(",",$birth_date);
$y_diff = date("Y") - $y;
$m_diff = date("m") - $m;
$d_diff = date("d") - $d;
if ($m_diff < 0 || $d_diff < 0) { $y_diff--; }
return $y_diff;
}
$sql2= mysql_query("SELECT * FROM catego WHERE category_id = '$idc'");
$categoryCount = mysql_num_rows($sql2);
if ($categoryCount>0 )
{
$row2 = mysql_fetch_array($sql2);
$id = $row2["category_id"];
$birthdate = $row2["birthdate"];
$address = $row2["address"];
$age = birth_date($birthdate);
}
?>
In PHP, there are a few ways to do this. Here are some good resources:
http://snippets.dzone.com/posts/show/1310
http://www.dreamincode.net/forums/topic/19953-calculating-age-tutorial/
Like this, in your language of choice:
function getAge(dateOfBirth)
{
DateTime now = DateTime.Today;
int years = now.Year - dateOfBirth.Year;
// subtract another year if we're before the birth day in the current year
if (now.Month < dateOfBirth.Month || (now.Month == dateOfBirth.Month && now.Day < dateOfBirth.Day))
--years;
return years;
}

Categories