使用选定的字段catname过滤记录。需要使用类别过滤器过滤课程记录。类别ID选择正确。我需要按类别名称过滤。表类别中的下拉列表和课程表中要完成的过滤器。示例:如果$ selected = 1,则$ selectedcat ="信息技术",然后将其传递给查询。这样它起作用。但是我需要直接使用类别过滤。
<?php
include('header-basic-light.php');
require_once('dbconnect.php');
$selsql="SELECT catno,catname FROM category";
$res=mysqli_query($con,$selsql);
if ($res == '')
{
echo "No connection";
die(mysqli_error($con));
}
$selected=0;
if (isset($_POST['categories']))
{
$selected=$_POST['categories'];
//echo $selected;
}
$selectsql="SELECT * FROM courses";
$res1=(mysqli_query($con,$selectsql));
if (!mysqli_query($con,$selectsql))
{
die(mysqli_error($con));
}
if ($selected==1)
{
$selectsql="SELECT * FROM courses where ccategory='$selectedcat'";
$res1=(mysqli_query($con,$selectsql));
}
else
if ($selected==2)
{
$selectsql="SELECT * FROM courses where ccategory='$selectedcat'";
$res1=(mysqli_query($con,$selectsql));
}
mysqli_close($con);
?>
<html>
<head>
<title>Drop down list </title>
<meta charset="UTF-8">
<meta name="viewport">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php
while($row=mysqli_fetch_assoc($res)) {
if($selected==$row['catno']) {
$selectedcat= $row['catname'];
/* here you can get $selectedcat*/
}
?>
<option value="<?php echo $row['catno'];?>"
<?php
if($selected == $row['catno']) { echo 'selected="selected"'; }
?>
>
<?php echo $row['catname'];?>
</option>
<?php } ?>
</select>
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r=mysqli_fetch_assoc($res1))
{
?>
<tr>
<td><?php echo $r['cno'];?></td>
<td><?php echo $r['cname'];?></td>
<td><?php echo $r['start_date'];?></td>
<td><?php echo $r['duration'];?></td>
<td><?php echo $r['remarks'];?></td>
<?php if ($r['ccategory']=='Information Technology') {$catnum=1;}
if ($r['ccategory']=='Management') {$catnum=2;} ?>
<td><a href="loadpage.php?id=<?php echo $catnum;?>">Details  </a>
</tr>
<?php } ?>
</table>
</body>
</html>
请尝试此
<?php
while($row=mysqli_fetch_assoc($res)) {
if($selected==$row['catno']) {
$selectedcat= $row['catname'];
/* here you can get $selectedcat*/
}
?>
<option value="<?php echo $row['catno'];?>"
<?php
if($selected == $row['catno']) { echo 'selected="selected"'; }
?>
>
<?php echo $row['catname'];?>
</option>
,请参阅更新的答案,在任何mysqli指令之前,您都不应关闭mysqli连接。
因此,在页面底部或使用Code中的所有MySQLI指令
mysqli_close($con);
<?php
include('header-basic-light.php');
require_once('dbconnect.php');
$selsql="SELECT catno,catname FROM category";
$res=mysqli_query($con,$selsql);
if ($res == '')
{
echo "No connection";
die(mysqli_error($con));
}
$selected=0;
if (isset($_POST['categories']))
{
$selected=$_POST['categories'];
//echo $selected;
}
$selectsql="SELECT * FROM courses";
$res1 = mysqli_query($con,$selectsql);
if (!mysqli_query($con,$selectsql))
{
die(mysqli_error($con));
}
if ($selected != 0)
{
/* fetch $selectedcat from input hidden name hid_selected_cat_name*/
$selectedcat = mysqli_real_escape_string($con,trim($_POST['hid_selected_cat_name']));
$selectsql="SELECT * FROM courses where ccategory='$selectedcat'";
$res1=mysqli_query($con,$selectsql);
}
?>
<html>
<head>
<title>Drop down list </title>
<meta charset="UTF-8">
<meta name="viewport">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php
while($row=mysqli_fetch_assoc($res)) {
if($selected==$row['catno']) {
$selectedcat= $row['catname'];
/* here you can get $selectedcat*/
}
?>
<option value="<?php echo $row['catno'];?>"
<?php
if($selected == $row['catno']) { echo 'selected="selected"'; }
?>
>
<?php echo $row['catname'];?>
</option>
<?php } ?>
</select>
/* store $selectedcat into hidden id*/
<input type="hidden" id="hid_selected_cat_name" name="hid_selected_cat_name" value="<?php echo $selectedcat;?>" >
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r=mysqli_fetch_assoc($res1))
{
?>
<tr>
<td><?php echo $r['cno'];?></td>
<td><?php echo $r['cname'];?></td>
<td><?php echo $r['start_date'];?></td>
<td><?php echo $r['duration'];?></td>
<td><?php echo $r['remarks'];?></td>
<?php if ($r['ccategory']=='Information Technology') {$catnum=1;}
if ($r['ccategory']=='Management') {$catnum=2;} ?>
<td><a href="loadpage.php?id=<?php echo $catnum;?>">Details  </a>
</tr>
<?php } ?>
</table>
</body>
</html>
简单的一个。尝试一下。
<?php
while($row=mysqli_fetch_assoc($res)) {
$selectedFlag = ($selected==$row['catno']) ? "selected" : "";
?>
<option value="<?php echo $row['catno'];?>" <?php echo $selectedFlag ?> >
<?php echo $selectedcat= $row['catname'];?>
</option>
<?php } ?>