我正在使用php日历,其中我从数据库中获取值。但是我在抓取时遇到了问题。网页速度增加到30秒。我已缓存所有图像,因此它缩短为12秒。唯一的主要问题是现在Db查询显然运行了30多次,而且我也在其中使用嵌套循环。
请告诉我如何减少数据库查询但得到相同的结果。我正在使用mysql php 5.4。
这是我的代码,我从db中获取数据并制作Php日历。
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) {echo "<tr >\n";}
if($i < $startday) {echo "<td></td>\n";}
else {
$cDay = ($i - $startday + 1);
$cToday = $cYear."-".$cMonth."-".$cDay;
$cTodayId = $cYear."_".$cMonth."_".$cDay;
echo '<td align="center" style="width:500px; height:150px;">';
echo '<span style="color:white;">'.$cDay.'</span>';
echo "<br/>";
$sel_trip = mysql_query("select maxPeople,regChild,regAdult,id,image,trip_start_time,trip_end_time,price1Rider,price2Riders from tablename where status = 1 order by trip_start_time asc") or die(mysql_error());
while($row_trip = mysql_fetch_array($sel_trip)){
$sel_time = mysql_query("select trip_date from tablename where trip_id = '".$row_trip['id']."'") or die(mysql_error());
while($row_time = mysql_fetch_array($sel_time)){
$title = str_replace('?','',$row_trip['name']);
$title2 = html_entity_decode(stripslashes(utf8_decode($title)));
$main_title = str_replace('?','',$title2);
$spot_date = date('Y-m-d',strtotime($cToday));
$sel_spot_Total = mysql_query("select SUM(tp_2man_sleds)+ SUM(tp_1man_sleds) as total_person_reserv from tablename where tp_trip_id = '".$row_trip['id']."' and tp_reserve_date = '".$spot_date."'") or die(mysql_error());
$row_spot_total = mysql_fetch_array($sel_spot_Total);
$total_person_reserv = $row_spot_total['total_person_reserv'];
$maxPeople = $row_trip['maxPeople'];
$total_person = $maxPeople - $total_person_reserv;
$cTodayy = date('Y-m-d',strtotime($cToday));
if($cTodayy == $row_time['trip_date']){
$child = '';
$adult = '';
$text = '';
if($row_trip['regChild'] == 1){
$child = '<br />Child: ($'.number_format($row_trip['price1Rider'],2).')';
}
if($row_trip['regAdult'] == 1){
$adult = '<br />Adult: ($'.number_format($row_trip['price2Riders'],2).')';
}
if($total_person < COUNT_DOWN && $total_person > 0){
$text = '<span style="color:red;">Only '.$total_person.' spot left</span>';
}
$second_path = "pictures/".$row_trip['image']."";
if(file_exists($second_path)){
copy("pictures/".$row_trip['image']."","resize/".$row_trip['image']."");
}
$path = "resize/".$row_trip['image'];
$image = resize_picture($path,$path,200,300);
if(file_exists($path)){
$img = '<img class="lazy" data-original="'.SITE_URL.'reservation_new/resize/'.$row_trip['image'].'" style="max-width:130px;" />';
}else{
$img = '';
}
echo '<a href="'.SITE_URL.'reservation_new/bookstep2.php?id='.$row_trip['id'].'&y='.$cYear.'&m='.$cMonth.'&d='.$cDay.'" style="color:#619081; text-decoration:none;">
<b>'.$img.'</b><br /><br /><span style="color:white;"><b>'.$main_title.'</b>
<br />'.date('g:i a',strtotime($row_trip['trip_start_time'])).' - '.date('g:i a',strtotime($row_trip['trip_end_time'])).'
'.$adult.$child.'<br />'.$text.'</a></span><br /><br />';
}
}
}
echo "</td>\n";
unset($cDayStatus);
unset($dateColor);
unset($owner);
unset($cToday);
unset($cTodayId);
}
if(($i % 7) == 6 ) {echo "</tr>\n";}
}
答案 0 :(得分:0)
像
这样的东西$sel_trip = mysql_query("select maxPeople,regChild,regAdult,id,image,trip_start_time,trip_end_time,price1Rider,price2Riders from triptable where status = 1 order by trip_start_time asc") or die(mysql_error());
while($r = mysql_fetch_array($sel_trip)) {
rows_trip[$r['id']]=$r;
$ids[]=$r['id'];
}
然后第二个查询就像这样
$sel_time = mysql_query("select trip_id,trip_date from datestable where trip_id IN (".join($ids).")")
or die(mysql_error());
while($r = mysql_fetch_array($sel_time)){
$rows_time[$r['trip_id']][]=$r; -- assuming there can be several
-- dates associated with one trip.
}
另一方面,如果每次旅行只有一个旅行日期,那么您当然可以直接在SQL查询中加入表格,如
SELECT maxPeople,regChild,regAdult,id,image,trip_start_time,
trip_end_time,price1Rider,price2Riders, -- from triptable
trip_date -- from datestable
FROM triptable
INNER JOIN datestable ON trip_id=id
WHERE status = 1 order by trip_start_time asc