我创建了一个数据库,显示一个投资组合的图像,该投资组合也有类别(Fencing& Decking,Lining,Carparks等)。我正在尝试创建一个只显示所选类别的下拉菜单,默认情况下正是所有人图片..这里是我的代码,目前似乎无法让它工作..
<?php
define('DB_NAME', 'wlarter_portfolio');
define('DB_USER', 'wlarter_user');
define('DB_PASSWORD', 'pw');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$results= mysql_query("SELECT * FROM images")
or die(mysql_error());
?>
<?php
while ($row = mysql_fetch_assoc($results)) { ?>
<div class="box-portfolio"> <?php echo $row['Img']; ?> </div>
<?php
};
?>
<?php
$category = isset($_GET['category']) && is_numeric($_GET['category']) ? $_GET['category'] : 1; // where 1 is a default category to show!
?>
<select onchange="if(this.value != '') document.location = '/portfolio.php?category=<?php echo $category; ?>&order_by=' + this.value">
<option value="">Choose an option</option>
<option value="Fencing & Decking"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'Fencing & Decking') echo ' selected="selected"'; ?>>Fencing & Decking</option>
<option value="Lining"<?php if(isset($_GET['order_by']) && $_GET['order_by'] == 'Lining') echo ' selected="selected"'; ?>>Lining</option>
</select>
答案 0 :(得分:0)
您需要重写查询。使用此代码:
$qry = "SELECT * FROM images ";
if(isset($_GET['category']) && is_numeric($_GET['category'])){
$qry .= "where category = ".$_GET['category'];
}
$results= mysql_query($qry) or die(mysql_error());
假设您在表格中有category_id列。你可以替换名称wh