使用下拉菜单筛选MySQL结果


Filtering Out MySQL Results Using Drop-Down Menu

我希望使用下拉菜单从MySQL数据库中根据类别表过滤出诊所表上的诊所名称。有什么帮助吗。。

表单代码

Category :
<?php 
$con=mysqli_connect("localhost","root","","qsearch"); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s'n", mysqli_connect_error()); exit(); 
} 
$result = mysqli_query($con, "select CategoryName from category"); 
echo '<select id="info" name="info">'; 
echo '<option value="">-select-</option>'; 
while ($row = mysqli_fetch_assoc($result)) { 
?> 
<option value="<?php echo $row['CategoryName']; ?>"><?php echo $row['CategoryName'];?> 
</option> 
<?php 
    } echo "</select>";
?>

您可以用这种简单的方法编写代码

<?php
$mysql_hostname = "localhost";
$mysql_user = "sayedsoft";
$mysql_password = "vPVJBmCTsxAHKsKF";
$mysql_database = "emlak";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>

并添加此功能

function optoins_forselector($db,$name,$where) {
$result_urunler = mysql_query("SELECT * FROM $db  WHERE $where");
$style_ = array();
while($result_row = mysql_fetch_array($result_urunler))
 {
  $style_[] = '<option value="'.$result_row['id'].'">'.$result_row[$name].'</option>';
 }
 return $style_;}

然后添加yor html代码

<select name=""><?php foreach (optoins_forselector($db,$name,$where) as $option) {echo $option;} ?> </select>

用数据库名称和$name列名称替换$db,如果您为数据库进行分支,请为exp"WHERE id='1'"进行分支