glennn.php Posted May 26, 2010 Share Posted May 26, 2010 i'm calling about 350 records from a table all at once and displaying the results in different areas of the page: $sql = "select id, makeid, model from models order by model asc"; $rs = mysql_query($sql); $acura = ""; $audi = ""; $bmw= ""; // and so on... while ($row = mysql_fetch_array($rs)) { $makeid = $row['makeid']; $checkbox = "<input type=\"checkbox\" name=\"model\" value=\"".$row['id']."\">".$row['model']." "; switch ($makeid) { case '1' : $acura .= $checkbox; break; case '3' : $audi .= $checkbox; break; case '4' : $bmw .= $checkbox; break; // and so on... } } echo $acura; echo "<br />"; echo $audi; echo "<br />"; echo $bmw; // and so on... this works, but i can't help but think there's a more efficient way to do this. anyone? thanks much. GN Quote Link to comment https://forums.phpfreaks.com/topic/203030-is-there-a-better-way-to-query-this/ Share on other sites More sharing options...
Kryptix Posted May 27, 2010 Share Posted May 27, 2010 Seems fine to me mate... You're only using one query anyway so there's not much the MySQL Help section can do, but the PHP seems fine also. Quote Link to comment https://forums.phpfreaks.com/topic/203030-is-there-a-better-way-to-query-this/#findComment-1063929 Share on other sites More sharing options...
ignace Posted May 27, 2010 Share Posted May 27, 2010 $query = 'SELECT id, makeid, make, model FROM models JOIN make ON make.id = models.makeid ORDER BY make ASC, model ASC'; $result = mysql_query($query); $make = ''; while ($row = mysql_fetch_assoc($result)) { if ($make !== $row['make']) { echo '<h3>', $make = $row['make'], '</h3>'; } echo '<label for="model', $row['id'], '">', $row['model'], '</label>', '<input type="checkbox" id="model', $row['id'], '" name="model" value="', $row['id'], '">'; } Outputs: <h3>Acura</h3> <label for="model1">Acura model #1</label> <input type="checkbox" id="model1" name="model" value="1"> 2.. 3.. <h3>Audi</h3> .. Quote Link to comment https://forums.phpfreaks.com/topic/203030-is-there-a-better-way-to-query-this/#findComment-1063987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.