简体   繁体   中英

Using one query to populate Multiple Select Menu HTML PHP

This is more of a code optimization question than the usual direct question with a problem that needs solving. I have 4 select menus which get their values from an SQL query. The problem is that I repeat the same query 4 times to populate the 4 select menu list. Here is the SQL code.(I am using PHP with SQL Server 2008)

 <select name="cities" id="" class="brkcity" style="width:120px;">
       <option></option>
        <?php
    foreach($country as $makassi1){ $selectcities = "SELECT City, Country, Rate FROM tblPerdiem_Rates WHERE Country = '$makassi1'";$checkcity = sqlsrv_query($conn,$selectcities, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ))or die(print_r( sqlsrv_errors(), true));

 while($row=sqlsrv_fetch_array($checkcity))
 {

     $countries = ($row['Country']);
     $names =($row['City']) ;
     $rate =($row['Rate']) ;
     $ratess=$names."-".$countries
     ?> 

 <option id="cityoptrates"  value="<?php echo $rate; ?>"><?php echo $ratess; ?></option>
 <?php       
 }
 }  
 sqlsrv_free_stmt($checkcity);      
         ?>
      </select></td>
      <td width="93" id="tdbreakfast"><input name="brkfastchk" class="breko" type="checkbox" value=""    id="" />
        <label for="brkfasttxt"></label>
        <input style="" value=""   name="brkfasttxt" class="breko" type="text" id="" size="3" readonly="readonly" />
      <label for="brkfastchk"></label></td>
      <td width="133" id="tdlnchcities"><select name="cities" id="" class="lnhcity" style="width:120px;">
       <option></option>
        <?php foreach($country as $makassi1) { $selectcities = "SELECT City, Country, Rate FROM tblPerdiem_Rates WHERE Country = '$makassi1'"; $checkcity = sqlsrv_query($conn,$selectcities, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ))or die(print_r( sqlsrv_errors(), true));


 while($row=sqlsrv_fetch_array($checkcity))
 {

     $countries = ($row['Country']);
     $names =($row['City']) ;
     $rate =($row['Rate']) ;
     $ratess=$names."-".$countries
     ?> 

 <option id="cityoptrates"  value="<?php echo $rate; ?>"><?php echo $ratess; ?></option>
 <?php       
 }   
 }sqlsrv_free_stmt($checkcity); 
         ?>
      </select>

That is the code I am using to populate the list menu. But I don't like it since it is inefficient to repeat the same query 4 times in the same page. I tried placing the query and the "for each" part at the top of the page, then using the "while" part in the select menu html part but it only worked for the first menu. The rest appeared blank.

Can I make it better than this or is this as good as it gets? All help appreciated

Cache the results of a single query in an array, then use that array to populate the dropdowns:

$data = array()
while($row = sqlserv_fetch_array($checkcity)) {
   $data[] = $row;
}

Then do a foreach loop instead of the while/fetch:

foreach($data as $row) {
     $countries = $row['Country'];
     $city = $row['City'];
     $rate = $row['rate'];
     etc...
}

update

you can cache "per $makassi1" as well, if need be:

while(...) {
    $data[$makassi1][] = $row;
}

However, consider rewriting your query to use a join or some other structure, so you're not running however many nearly-identical queries so many times.

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