按时间戳中的日期和时间对条目进行分组
我正在构建一个预订系统,并选择使用时间戳将预订存储在我的 mysql 数据库中。然而,例如,用户可以连续预订 2 个或 3 个时段:
9:00 - 9:30
9:30 - 10:00
10:00 - 10:30
或者他们可以只预订单个时段。
我想显示用户进行的预订,但按时间范围对它们进行分组,因此例如在上面显示的时间中,它应该显示 9:00 - 10:30,而不是列出 3 项。
每个预订都存储有 bookingFrom 和 bookingUntil 作为时间戳。我已经成功地让它适用于 2 个预订(例如 9:00-9:30 和 9:30-10:00 显示为 9:00-10:00),但如果有中间条目,我正在努力思考如何让它发挥作用。
有什么想法吗?我正在使用 PHP/mySQL
TABLE SCHEMA
bookingID、userID、bookingFrom (INT - unix timestamp)、bookingUntil (INT - unix timestamp)
CREATE TABLE IF NOT EXISTS `dma_bookings` (
`bookingID` int(11) NOT NULL AUTO_INCREMENT,
`userID` int(11) NOT NULL,
`bookingFrom` int(11) NOT NULL,
`bookingUntil` int(11) NOT NULL,
`roomID` int(11) NOT NULL,
PRIMARY KEY (`bookingID`),
KEY `userID` (`userID`,`bookingFrom`,`bookingUntil`),
KEY `roomID` (`roomID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=98 ;
--
-- Dumping data for table `dma_bookings`
--
INSERT INTO `dma_bookings` (`bookingID`, `userID`, `bookingFrom`, `bookingUntil`, `roomID`) VALUES
(95, 1, 1317362400, 1317364199, 1),
(96, 1, 1317364200, 1317365999, 1),
(97, 1, 1317366000, 1317367799, 1);
到目前为止的代码(抱歉非常混乱!):
$bookingArray = $data['bookingInfo'];
$bookingCleanArray = array();
$reversedArray = array_reverse($data['bookingInfo']);
$currentItemIndex=0;
$nextItemIndex=1;
foreach($reversedArray as $booking){
echo "<hr>BookingID: ".$booking['bookingID']."<br/><br/>";
$bookingFromMinusOne=$booking['bookingFrom']-1;
if($bookingFromMinusOne==$data['bookingInfo'][$nextItemIndex]['bookingUntil']){
//The current booking is part of a multiple booking range
$bookingArray[$nextItemIndex]['bookingUntil']=$booking['bookingUntil'];
unset($bookingArray[$currentItemIndex]);
}else{
echo "Single booking<br/>";
}
$currentItemIndex++;
$nextItemIndex++;
}
I'm building a booking system and have opted to use timestamps to store the bookings in my mysql database. The user can book 2 or 3 slots consequtively however, for example:
9:00 - 9:30
9:30 - 10:00
10:00 - 10:30
or they can just book single slots.
I want to show the bookings made by a user but group them by the time frame, so for example in the times shown above it should show 9:00 - 10:30 instead of listing 3 items.
Each booking is stored with a bookingFrom and bookingUntil as a timetamp. I have managed to get it working for 2 bookings (eg 9:00-9:30 and 9:30-10:00 to show as just 9:00-10:00) but if there is a middle entry, i'm struggling with how to get this working.
Any ideas? I'm using PHP/mySQL
TABLE SCHEMA
bookingID, userID, bookingFrom (INT - unix timestamp), bookingUntil (INT - unix timestamp)
CREATE TABLE IF NOT EXISTS `dma_bookings` (
`bookingID` int(11) NOT NULL AUTO_INCREMENT,
`userID` int(11) NOT NULL,
`bookingFrom` int(11) NOT NULL,
`bookingUntil` int(11) NOT NULL,
`roomID` int(11) NOT NULL,
PRIMARY KEY (`bookingID`),
KEY `userID` (`userID`,`bookingFrom`,`bookingUntil`),
KEY `roomID` (`roomID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=98 ;
--
-- Dumping data for table `dma_bookings`
--
INSERT INTO `dma_bookings` (`bookingID`, `userID`, `bookingFrom`, `bookingUntil`, `roomID`) VALUES
(95, 1, 1317362400, 1317364199, 1),
(96, 1, 1317364200, 1317365999, 1),
(97, 1, 1317366000, 1317367799, 1);
Code so far (apologies very messy!):
$bookingArray = $data['bookingInfo'];
$bookingCleanArray = array();
$reversedArray = array_reverse($data['bookingInfo']);
$currentItemIndex=0;
$nextItemIndex=1;
foreach($reversedArray as $booking){
echo "<hr>BookingID: ".$booking['bookingID']."<br/><br/>";
$bookingFromMinusOne=$booking['bookingFrom']-1;
if($bookingFromMinusOne==$data['bookingInfo'][$nextItemIndex]['bookingUntil']){
//The current booking is part of a multiple booking range
$bookingArray[$nextItemIndex]['bookingUntil']=$booking['bookingUntil'];
unset($bookingArray[$currentItemIndex]);
}else{
echo "Single booking<br/>";
}
$currentItemIndex++;
$nextItemIndex++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设每次将它们加载到数组中时都有单独的字段,然后使用第一个字符串并计数来查找最后一个字符串。
但是,正如 rahularyansharma 所说,需要更多细节。
好吧……嗯
你难道不想抓住:
哪个应该给我们:
bookingID,userID,bookingFrom,bookingUntil,roomID
所以如果我们......
或者我完全误解了你想要做什么?
Assuming you've got separate fields for each time you load them into an array then use the first string and count to find the last string.
But yes as rahularyansharma said needs a bit more detail.
Okay... hhmm
Wouldn't you just want to grab:
Which should give us:
bookingID, userID, bookingFrom, bookingUntil, roomID
so if we...
Or have I completely misunderstood what you're trying to do?
我认为我的方法是选择给定用户的所有预订,但按开始时间对结果进行排序。我感觉你当前的循环有正确的开始,你只需要更多的条件来确保插槽工作。
我将从第一个时段开始,并不断调整结束时间,直到达到大于当前结束时间的开始时间。此时,您完成了当前插槽(例如将其推送到另一个阵列上),并将当前插槽设置为新插槽。
这是一个例子,虽然我没有时间测试它,但我认为它应该可以工作:
所以在那之后
$condensed_slots
应该包含一组减少的插槽(如果可能),尽管 bookingID 可能不一定反映原始的from
和until
时间戳(显然)。I think my approach would be to select all the bookings for a given user, but order the results by the start time. I get the feeling your current loop has the right start, you just need a few more conditions to make sure the slots work.
I would start with the first slot, and keep adjusting the end time until I reach a start time that is greater than the current end time. At that point you finish with the current slot, (say push it onto another array), and set your current slot to the new one.
Here is an example, although I don't have the time to test it I think it should work:
So after that
$condensed_slots
should contain a reduced set (if possible) of slots, although the bookingID may not necessarily reflect the originalfrom
anduntil
timestamps (obviously).