gpt4 book ai didi

php - 页面速度因循环而变慢

转载 作者:行者123 更新时间:2023-11-29 22:21:13 24 4
gpt4 key购买 nike

我正在使用 php 日历,并在其中从数据库中获取值。但我在获取时遇到问题。网页速度增加至 30 秒。我已经缓存了所有图像,因此时间减少到 12 秒。现在唯一的主要问题是 Db 查询,它显然运行了 30 多次,而且我也在其中使用了嵌套循环。

请告诉我如何减少数据库查询但得到相同的结果。我正在使用 mysql php 5.4。

这是我的代码,我在其中从数据库获取数据并制作 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";}



}

最佳答案

类似的东西

$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

关于php - 页面速度因循环而变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30691208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com