Php 使用循环会降低页面速度

Php 使用循环会降低页面速度,php,mysql,Php,Mysql,我使用php日历,在其中我从数据库中获取值。但我在获取时遇到问题。网页速度增加到30秒。我已经缓存了所有图像,所以它缩短到12秒。现在唯一的主要问题是Db查询,它显然运行了30多次,我也在其中使用嵌套循环 请告诉我如何减少数据库查询,但得到相同的结果。我正在使用mysql PHP5.4 这是我的代码,在其中我从数据库中获取数据,并制作Php日历 for ($i=0; $i<($maxday+$startday); $i++) { if(($i % 7) == 0

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

请告诉我如何减少数据库查询,但得到相同的结果。我正在使用mysql PHP5.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";}



        }
($i=0;$i 0)的
{
$text='Only'。$total_person.'spot left';
}
$second_path=“pictures/”$row_trip['image']。”;
如果(文件_存在($second_path)){
复制(“图片/”$row\U trip['image']。”,“调整大小/”$row\U trip['image']。”);
}
$path=“resize/”$row_trip['image'];
$image=调整图片大小($path,$path,200300);
如果(文件_存在($path)){
$img='';
}否则{
$img='';
}
回音“

”; } } } 回音“\n”; 未结算($cDayStatus); unset($dateColor); unset($所有者); 未结算($天); 未结算($cTodayId); } 如果($i%7)==6{echo“\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

您可以在三个查询中首先进行查询,以收集所有数据,然后对页面进行所有位的构建。使用第一个查询的结果
id
s,将它们放在第二个和第三个查询的
WHERE trip\u id in(…)
WHERE tp\u trip\u id in(…)
子句中。确保您将所有结果保存在合适的php数组中,然后您就可以开始构建页面了。您能给我一个示例代码吗?