我不知道你的數據庫看起來完全像什麼,但你可以做這樣的事情:
function getShoes($manufacturer, $name=''){
$manufacturer = mysqli_real_escape_string($manufacturer);
$sql = 'SELECT id, name, size FROM shoes WHERE manufacturer = "'.$manufacturer'" AND in_stock > 0 ';
if($name){
$name = mysqli_real_escape_string($name);
$sql .= 'AND name = "'.$name.'" ';
}
$sql .= 'ORDER BY name, size';
$result = mysqli_query($sql); //Results like this: id:00001, name:shoe1, size:10
while($row = mysqli_fetch_array($result)){ // id:00002, name:shoe1, size:11
$rows[$row['name']][] = $row; //group them by name (assuming you have more
} //than one shoe type per manufacturer)
foreach($rows as $name=>$shoes){
echo('<select name="'.$name.'">');
foreach($shoes as $shoe){
echo('<option value="'.$shoe['id'].'">'.$shoe['name'].' - '.$shoe['size'].'</option>');
}
echo('</select>');
}
}
現在在你的HTML中,如果你調用了這個函數,你會得到你指定的製造商生產的所有鞋子。例如,getShoes('nike');
將獲得耐克製造的所有鞋子,併爲您提供所有的選擇框。您可能希望更具體一些,因爲您不希望Nike爲耐克製作的Shoe1的頁面設置中的每個鞋子選擇選項。那麼你可以做getShoes('nike', 'shoe1');
,那隻會給你耐克鞋Sh1的選擇框。希望這可以幫助。 :)