Can you search an array for a value between 2 keys. In my case search between two timestamps dates in the array for a certain status.
I’ve tried using a loop between two dates but its so slow
// get weeks data from db and put into array
$query = “SELECT slotStatus, slotTime FROM tblTimeSlots WHERE slotTime>=‘$todayNow’ AND slotTime<=‘$unixSevenDays’ AND timeSlotID=‘$timeSlotID’”;
$result = mysql_query($query,$db);
while($row = mysql_fetch_array($result))
{
$status = $row[slotStatus];
$slotTime = $row[slotTime];
$slotArray[$slotTime] = "$status";
}
Any help would be great
Well the first thing you should consider is how do you need your data? If I recall you are writing a week of data at a time by time interval.
So you would have something like so already:
// loop from starting hour to ending hour by time interval
// loop Sunday through Saturday
// Output time slots horizontally
So with that said, your array ideally should try and identify where it falls in the time slots or at least store its’ data so it can be easily identified into a specific time slot.
There are several ways to organize your data, I’ll outline one, and we’ll see if others agree with it or think a different organization is better suited.
$timeSlots = array('Sunday' => array(), 'Monday' => array(), 'Tuesday' => array() ...);
while($row = mysql_fetch_array($result))
{
$timeSlots[date('l', $row['slotTime'])][] = array('time' => date('H:i:s', $row['slotTime']), 'status' => $row['slotStatus']);
}
print_r($timeSlots);
// idea of output:
// array(
// 'Sunday' = array(
// [0] = array(
// ['time'] = 09:00:00
// ['status'] = ""
// ),
// [1] = array(
// ['time'] = 09:15:00
// ['status'] = ""
// ),
// [2] = array(
// ['time'] = 09:30:00
// ['status'] = ""
// )
// ),
// 'Monday' = array(
// [0] = array(
// ['time'] = 09:15:00
// ['status'] = ""
// ),
// [1] = array(
// ['time'] = 09:30:00
// ['status'] = ""
// ),
// [2] = array(
// ['time'] = 09:45:00
// ['status'] = ""
// )
// ),
// ....
// ....
// 'Saturday' = array(
// [0] = array(
// ['time'] = 09:15:00
// ['status'] = ""
// ),
// [1] = array(
// ['time'] = 09:30:00
// ['status'] = ""
// ),
// [2] = array(
// ['time'] = 09:45:00
// ['status'] = ""
// )
// )
// )
//
Then as you loop through the time intervals and the weekdays you can loop through the day’s array of time slots and check that if [‘time’] fits in the current interval.
Hope this helps and if you choose this route and need a more thorough code set, let me know. I’d be glad to help you and I think this will get you started in a good direction.
Hi Thanks for your help. I mucked around before your post and got the problem working about 90%
Got it checking start and finish time of slot just need to check status between the start and finish times.
What do you think or am I going about it wrong and your idea would be best
// start of time slot ie 9:00
$status = $slotArray[$unix];
// end of time slot ie 60 minute duration
$statusLength = $slotArray[$unixSlotTimeLength];
Post your code, as I’d have to see how its implemented. Are you getting faster response times (should be, as your time was likely spent in all of the database queries)?
Hi
Down from 7 sends to 0.7 seconds so great improvement. Wondered if you could scan a arrange range for a value ie $startTime, $endTime for value booked
<?
//$todayNow = time();
// hourly time slot rows + needs 7 vertical columns for time
for($h=$hourStart; $h<=$hourFinish; $h++)
{
echo(“<tr align=‘center’>”);
// columns 7 for each day
for($c = 0; $c <7; $c++)
{
$counter++;
if($h==$hourStart)
{
$minsLeft="";
}
$time = 86400 * $c;
$newTime = $todayNow + $time;
$dateNum = date('j', $newTime);
$dateYear = date('Y', $newTime);
$dateMonth = date('M', $newTime);
// build up date and time from $h
$timeStamp = "$dateNum $dateMonth $dateYear $h:00 GMT";
$unix = strtotime($timeStamp);
// left hand column
if($c==0)
{
// display hour column on left
if($h>12 && $h<=23)
{
$columnTime = $h - 12;
$columnTime = $columnTime ."pm";
}
else
{
$columnTime = $h."am";
}
echo("<th width='45'><span class='hourtext'>$columnTime</span></th>");
}
// end of column on left
echo("<td width='70' valign='top'>");
// START OF BOOKING TESTHOUR INTERGRATION //
if($minsLeft=="Y")
{
// get value from array
$mins = $minsLeftArray[$counter-7];
$minsLeft ="";
}
else
{
$mins=0;
}
$loop=0;
// minutes part of system
for($m=$mins; $m<60; $m+=$timeInterval)
{
$loop++;
$minutes = $m;
$timeStamp = "$dateNum $dateMonth $dateYear $h:$minutes GMT";
$unix = strtotime($timeStamp);
$unixSlotTimeLength = $unix+(60*$duration)-60;
$startStatus="";
$endStatus="";
// get status for start of slot
$startStatus = $slotArray[$unix];
// get status at end of slot
$endStatus = $slotArray[$unixSlotTimeLength];
if($startStatus=="AVAILABLE" && $endStatus=="AVAILABLE")
{
$timeStamp = "$dateNum $dateMonth $dateYear $h:$minutes GMT";
if($minutes==0)
{
$minutes="00";
}
$showTime = gmdate("g:ia",$unix);
$showTimeHours = gmdate("g",$unix);
if($unix>=time())
{
if($showTimeHours>9)
{
echo("<a href='/serviceWidgetBook.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID&serviceID=$serviceID&locationID=$locationID' class='available'>$showTime</a>");
}
else
{
echo("<a href='/serviceWidgetBook.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID&serviceID=$serviceID&locationID=$locationID' span class='available' align='center'> $showTime </a></class>");
}
}
if($loop<$slotNumber)
{
echo("<br />");
}
else
{
}
$m++;
if($m>59)
{
$minsLeft = $m - 60;
// add to array
$minsLeftArray[$counter] = $minsLeft;
$minsLeft="Y";
$slotStatus="";
}
}
//elseif($bookedStatus=="BOOKED")
elseif($startStatus=="BOOKED" || $endStatus=="BOOKED")
{
$unixSlotTimeLength = gmdate("i",$unixSlotTimeLength+60);
if($unixSlotTimeLength=="00")
{
$unixSlotTimeLength = 59;
}
// booked
echo("<span class='booked'> </class>");
if($loop<$slotNumber)
{
echo("<br />");
}
else
{
}
$m++;
if($m>59)
{
$minsLeft = $m - 60;
$minsLeftArray[$counter] = $minsLeft;
$minsLeft="Y";
$slotStatus="";
}
}
else
{
// not available
if($loop<$slotNumber)
{
echo("<span class='notavailable'>oooooooo</class><br />");
}
else
{
echo("<span class='notavailable'>oooooooo</class>");
}
$m++;
if($m>59)
{
$minsLeft = $m - 60;
$minsLeftArray[$counter] = $minsLeft;
$minsLeft="Y";
$slotStatus="";
}
}
// end of minute loop
$m=$m-1;
}// end of minute loop
echo("</td>");
} // end of column loop
echo("</tr>");
} // end of hour loop
?>
</table>
Well you can use array_keys and tell it to search for ‘BOOKED’ and it will return ALL keys that contain ‘BOOKED’ as their value.
Then you could loop through the keys to see if it is in the range of $startTime and $endTime. I personally think with the code you already have this would be the best approach to get to that data.