简体   繁体   中英

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.

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 . 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.

  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.

  3. Echo with commas, not periods . If you use periods, PHP has to concatenate the string before it outputs. If you use commas, it just outputs them in order with no extra processing.

  4. You should use echo instead of print() . As a language construct rather than a function, echo has a slight performance advantage over print().

So this is your code with points 2, 3 and 4 above corrected, thus assuming your web server is caching output:

<?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:

<?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. If your table is large, this may cause the delay in response.

Try this instead, for the part where you fill the <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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