I’m developing a client management system for my workplace using PHP and MySQL. Every page loads a header.php file which contains, among other things, the navigation menu and a dropdown box which dynamically loads all of the client names so that I can quickly jump to their contact card. Long names are shortened, with the full name as the ‘title’.
However, every page has to pause at this point while it loads the list before it can continue loading the rest of the page.
Here’s my code:
<select>
<?
$select = "SELECT id, name FROM customers ORDER BY name ASC";
$query = @mysql_query($select,$con) or die(mysql_error());
while($data = @mysql_fetch_array($query)){
$cust_id = $data['id'];
$cust_name = substr($data['name'], 0, 14);
$cust_name_long = "";
if (strlen($data['name']) >= 15){
$cust_name .= "...";
$cust_name_long = $data['name'];
}
echo '<option title="'.$cust_name_long.'" value="customers-edit.php?id='.$cust_id.'">'.$cust_name.'</option>';
}
?>
</select>
What’s a more efficient way of doing this, so that I don’t have the 2-second pause every time a page opens? I could settle with loading the dropdown list after the rest of the page has loaded, or loading a cached version of the dropdown list unless customer names have changed, or……?
i suspect that you have a really large list of customers, if that is the case then dont use drop down, use autosuggest. otherwise check if your customers table has indexes