簡體   English   中英

加快PHP渲染的巨大選擇列表

[英]Speed up PHP rendering HUGE select list

你們真棒。

我的腳本從MYSQL中選擇了所有8500個客戶,這在MySQL工作台中大約需要0.16秒 ,這很好,但是瀏覽器需要10秒才能從結果中渲染該框。

有沒有更快的方法來創建這個巨大的選擇框?

我很沮喪,因為我認為所有PHP都是在服務器端制作的,無法加速。

<body>
<select name="customer" id="customer" onChange="getcustinfo();" data-placeholder="Choose a Customer" class="chosen-select" style="width:500px;" tabindex="1">
<option value=""></option>
<?php // get the products
    $sql = "
        SELECT *
        FROM `cust`
        LEFT Join `address` 
        ON `cust`.`custid` = `address`.`addcustid`
        and `address`.`addtype` = 'b'
        WHERE `cust`.`custactive` = 'y'"
    ;
    $result = mysqli_query($con,$sql) or die('Query failed: Could not get list of CLIENTS: ' . mysqli_error($con)); // query
        while ($row = mysqli_fetch_array($result)) {
                foreach ($row as $key => $value){ ${$key} = $value; }
                $space="";
                $endingspaces = 4-(2*strlen($prodid));

                $custname = substr($row['custname'],0,15);
                $address1 = substr($row['address1'],0,15);
                $addcity = substr($row['addcity'],0,15);

                print "<option value=\"$custid\">$custid: $space$custname, $address1, $addcity, $addstate</option>";                                                
        }
?>
</select>
</body>

緩存它 生成一次 HTML,然后將其保存在內存中(如果可以的話)(APC,內存緩存)或平面文件。 然后在隨后的頁面加載中,只需從緩存中讀取並回顯即可。 快得多。

找到了問題。 哇。

問題是“打印每個選項行”功能。 在遍歷MySQL列表時,我正在打印每個8500選項值。 每行都“打印”到並保存在內存中,然后顯示在末尾。

顯然,打印選項功能實際上是在每次迭代時將該行發送到瀏覽器,因此選擇框不是由服務器編譯的,而是逐段發送到瀏覽器,這需要花費大量時間。

通過使php在服務器上創建整個選擇框(包括在添加的“ select_box”變量中打開和關閉SELECT標記)進行了修復,然后在完全完成后作為回顯將其發送回瀏覽器。 現在只需要1.2秒。

我可以忍受這一點。

<?php // get the products
    $select_box = "<select name=\"customer\" id=\"customer\" onChange=\"getcustinfo();\" data-placeholder=\"Choose a Customer\" class=\"chosen-select\" style=\"width:227px;\" tabindex=\"1\">
    <option value=\"\"></option>";

    $sql = "
        SELECT *
        FROM `cust`
        LEFT Join `address` 
        ON `cust`.`custid` = `address`.`addcustid`
        and `address`.`addtype` = 'b'
        WHERE `cust`.`custactive` = 'y'"
    ;
    $result = mysqli_query($con,$sql) or die('Query failed: Could not get list of CLIENTS: ' . mysqli_error($con)); // query
        while ($row = mysqli_fetch_array($result)) {
                foreach ($row as $key => $value){ ${$key} = $value; }
                $space="";   
                $custname = substr($row['custname'],0,15);
                $address1 = substr($row['address1'],0,15);
                $addcity = substr($row['addcity'],0,15);

                $select_box .= "<option value=\"$custid\">$custid: $space$custname, $address1, $addcity, $addstate</option>";                                               
        }
    $select_box .="</select>";
    echo $select_box;   
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM