【发布时间】:2014-01-02 22:53:00
【问题描述】:
免责声明:我知道我的代码中有不推荐使用的 mysql 函数。那在我的待办事项清单上。
我有一个 MySql 选择,让我在房屋预订系统中为不同的项目提供季节。
种类:
Low season: 2010-01-01, 2010-03-01, 100 //meaning start,end,price
这是我的第一个 sql:
while($season_row=mysql_fetch_assoc($season_res)){
$seasonsArray[$season_row['id_item']][] = array(
$season_row['season_start'],
$season_row['season_end'],
$season_row['daily_price']
);
}
这里定义了日期(到达函数YYYY-mm-dd):
function seasonPrice($from,$to,$requested_item){
$start = round(strtotime($from)/86400)*86400; // like 2008-01-01
$end = round(strtotime($to)/86400)*86400; // to 2015-01-01
$formattedStart = date('Y-m-d', $start);
$formattedEnd = date('Y-m-d', $end);
现在我需要在 $seasonsArray 的项目之间循环两个日期,然后检查该项目在特定日期的价格。
我这样做了:
foreach($seasonsArray as $item=>$value){
for( $thisDay = $start; $thisDay < $end; $thisDay = $thisDay + 86400){
foreach($value as $innerValue){
$season_start = roundToSeconds($innerValue[0]);
$season_end = roundToSeconds($innerValue[1]);
if($thisDay >= $season_start && $thisDay <= $season_end) {
$foundPrice[] = round($innerValue[2]);
}
}
$thisSerie[] = array($thisDay * 1000, isset($foundPrice) ? $foundPrice[0] : 0);
// security check to avoid double assigned seasons to same day
if(count($foundPrice) > 1){ die('There is double bookings in item: '.$item);}
unset($foundPrice);
}
$seasonPrices[] = array(
'data'=> $thisSerie,
'label'=> 'House ID: '.$item,
);
}
但我得到:Fatal error: Allowed memory size of 100663296 bytes exhausted
有什么建议可以改进我的代码以不需要这么多内存吗?还是有错误而我没有看到?
【问题讨论】:
-
你在用 seasons 数组做什么?也许您已经可以使用 sql 查询或至少部分简化它来做到这一点,3 嵌套 for 看起来很糟糕
-
@arieljuod 季节数组只是为了从 sql 查询中收集数据。每个房屋项目都有许多季节,例如每年 4/5,开始和结束以及该期间一天的价格。同意 3loops = 不好,但没有更好的方法......
-
您能否发布一个示例,说明您从数据库中获取的数据以及循环结束时想要的数据?不清楚您想对这些循环做什么,也许一个示例可以提供帮助。我猜你可以简化循环做一个更好的查询,但我不明白你最后想要什么
-
@arieljuod,当然。让我知道这是否有帮助:jsfiddle.net/beNUS/show - 感谢您查看此内容。
-
不清楚的部分是你想用这些数据做什么,那个循环在 seasonPrice 函数里面?季节价格返回什么?您期望该输入的输出是什么?