加快PHP呈现巨大的选择列表

加快PHP呈现巨大的选择列表,php,mysql,Php,Mysql,你们真是太棒了 我的脚本将从MYSQL中选择所有8500个客户,在MYSQL workbench中,这大约需要0.16秒,这很好,但是浏览器需要10秒才能从结果中渲染框 有没有更快的方法来创建这个巨大的选择框? 我被难住了,因为我认为所有的PHP都是在服务器端制作的,不能加快速度 <body> <select name="customer" id="customer" onChange="getcustinfo();" data-placeholder="Choose a Cu

你们真是太棒了

我的脚本将从MYSQL中选择所有8500个客户,在MYSQL workbench中,这大约需要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、memcache)或平面文件。然后在随后的页面加载中,只需从缓存中读入并回显即可。快得多。

缓存它。生成HTML一次,然后将其保存在内存中(APC、memcache)或平面文件。然后在随后的页面加载中,只需从缓存中读入并回显即可。更快。

发现了问题。哇。

问题在于“打印每个选项行”函数。我在遍历MySQL列表时打印了8500个选项值。每一行被“打印”到内存中并保存在内存中,然后在末尾显示

打印选项功能显然是在每次迭代时将该行发送到浏览器,因此选择框不是由服务器编译的,而是一块一块地发送到浏览器,需要花费大量时间

我通过让php在服务器上创建整个选择框(包括add to“select_box”变量中的打开和关闭选择标记)来修复它,然后在完成后将其作为一个回音发送回浏览器。现在只需要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;   
?>

找到了问题。哇。

问题在于“打印每个选项行”函数。我在遍历MySQL列表时打印了8500个选项值。每一行被“打印”到内存中并保存在内存中,然后在末尾显示

打印选项功能显然是在每次迭代时将该行发送到浏览器,因此选择框不是由服务器编译的,而是一块一块地发送到浏览器,需要花费大量时间

我通过让php在服务器上创建整个选择框(包括add to“select_box”变量中的打开和关闭选择标记)来修复它,然后在完成后将其作为一个回音发送回浏览器。现在只需要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;   
?>

<代码>如果你的列表那么大,你可能想考虑使用一些类似的选择:时间从PHP必须加载到内存中,然后解析循环8500次,生成HTML发送出去。您可能需要重新考虑选择要显示的所有8500行。毕竟,强大的选择工具是数据库提供的主要优势之一。为什么要扔掉这个实用程序?如果你的列表那么大,你可能想考虑使用类似的选择:时间从PHP必须加载到内存中,然后解析循环8500次,生成HTML发送出去。您可能需要重新考虑选择要显示的所有8500行。毕竟,强大的选择工具是数据库提供的主要优势之一。为什么要扔掉这个工具?出于好奇,你建议如何跟上列表的变化?@joseph4tw每当发生变化时,缓存都应该被删除并生成一个新的缓存。自动或在下一页加载时。或者缓存可以定期“刷新”,这取决于数据“刷新”的重要性,或者它是否可以在设定的时间段内过期..我同意如何重新缓存它。我说的更多的是如何知道列表何时更改。间隔的想法似乎很简单,但是当用户创建一个新客户并需要立即向他们发送发票时(很抱歉这里有点离题)?假设:客户通过CMS或某种web界面进行管理。当添加客户时,它可以将缓存标记为脏缓存、删除缓存等。出于好奇,您建议如何跟上列表的更改?@joseph4tw每当进行更改时,应删除缓存并生成新的缓存。自动或在下一页加载时。或者缓存可以定期“刷新”,这取决于数据“刷新”的重要性,或者它是否可以在设定的时间段内过期..我同意如何重新缓存它。我说的更多的是如何知道列表何时更改。间隔的想法似乎很简单,但是当用户创建一个新客户并需要立即向他们发送发票时(很抱歉这里有点离题)?假设:客户通过CMS或某种web界面进行管理。添加客户后,可以将缓存标记为脏缓存、删除缓存等。