简体   繁体   English

使用php来填充 <select>在网页上。寻找更快的加载时间

[英]Using php to fill a <select> on webpage. Looking for a faster load time

I added this : 我补充说:

<select>
    <option value=""></option>
    <?php include 'connect/config.php'; ?>
    <?php include 'connect/opendb.php'; ?>

    <?php
        $query = $db->query('SELECT type FROM card_type'); 
        $rows = $query->fetchAll(); 
        foreach($rows as $row) { 
        print '<option value="'.$row['type'].'">'.$row['type'].'</option>';
        }
    ?>
    <?php $db =null ?>
</select>

to my page and now it's takeing about 5 seconds longer to load the page. 到我的页面,现在加载页面需要大约5秒钟。

Is there a more effecient way to fill a option box from a database? 是否有更有效的方法从数据库填充选项框?

These are some issues in your code that affects performance: 这些是您的代码中影响性能的一些问题:

  1. You should not call print for each row of your table . 您不应该为表格的每一行调用print That penalizes performance (if the server is not caching the output) as everytime you call print you will be sending bytes across the net which is a costly operation that is better be done once for one big chunk of data rather than many times for small chunks of data , that is the reason web servers will often cache all your PHP output prior to sending it to the browser. 这会影响性能(如果服务器没有缓存输出),因为每次调用print你都会在网上发送字节,这是一项代价高昂的操作, 对于一大块数据而言,最好是一次,而不是小块的多次数据 ,这就是Web服务器在将其发送到浏览器之前经常缓存所有PHP输出的原因。

  2. You should pass by reference the array value when traversing the array with foreach, to avoid the copy of a variable in each iteration of the loop. 在使用foreach遍历数组时,应该通过引用传递数组值 ,以避免在循环的每次迭代中复制变量。

  3. Echo with commas, not periods . 用逗号回声,而不是句号 If you use periods, PHP has to concatenate the string before it outputs. 如果使用句点,PHP必须在输出之前连接字符串。 If you use commas, it just outputs them in order with no extra processing. 如果你使用逗号,它只是按顺序输出它们而不需要额外的处理。

  4. You should use echo instead of print() . 你应该使用echo而不是print() As a language construct rather than a function, echo has a slight performance advantage over print(). 作为语言结构而不是函数,echo比print()具有轻微的性能优势。

So this is your code with points 2, 3 and 4 above corrected, thus assuming your web server is caching output: 因此,这是您的代码,上面的第2,3和4点已得到纠正,因此假设您的Web服务器正在缓存输出:

<?php
      $query = $db->query('SELECT type FROM card_type'); 
      $rows = $query->fetchAll(); 
      foreach($rows as &$row) { 
          echo '<option value="', $row['type'] ,'">' ,$row['type'] , '</option>';
      }
?>

and this is your code with point 1 and 2 above corrected, thus assuming your web server is not caching the output: 这是您的代码,上面的第1点和第2点已更正,因此假设您的Web服务器没有缓存输出:

<?php
      $query = $db->query('SELECT type FROM card_type'); 
      $rows = $query->fetchAll(); 
      $out = '';
      foreach($rows as &$row) { 
          $out .= '<option value="'.$row['type'].'">'.$row['type'].'</option>';
      }
      echo $out;
?>

Right now, you're putting the entire database table into a php array. 现在,您将整个数据库表放入php数组中。 If your table is large, this may cause the delay in response. 如果您的表很大,这可能会导致响应延迟。

Try this instead, for the part where you fill the <select> : 请尝试使用此选项,对于填充<select>

<?php
    $query = $db->query('SELECT type FROM card_type'); 
    while($row = $query->fetch_array()) { 
        print '<option value="'.$row['type'].'">'.$row['type'].'</option>';
    }
?>

Try this one.. 试试这个......

<select>
  <option value=""></option>  
        <?php 
    include 'connect/config.php';
    include 'connect/opendb.php';
    $query = $db->query('SELECT type FROM card_type'); 
    $rows = $query->fetchAll(); 
    $select_option = '';
    foreach($rows as $row) { 
      $select_option .= '<option value="'.$row['type'].'">'.$row['type'].'</option>';
    }
    echo $select_option;
    unset($db);
    ?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM