我想在网页上显示一个基于类别的带有图片的购物项目,可以在大多数在线购物网站上找到。我创建了两个 mysql 表:Ist with id、category_name 和第二个带有 id、categoryid、product、image_path。我可以在页面上一次显示所有产品图片,但我不知道如何显示从页面顶部带有提交按钮的下拉列表中选择的单个类别的产品图片。我希望所有人都清楚我的观点,否则请随时问我。
下面我附上了我的代码,它一次在我的 php 页面上显示所有产品图片,没有任何下拉列表。欢迎就此提出任何想法和建议。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
ul, li {
list-style-type:none;
}
ul.display {
width: 500px;
}
ul.display li {
float: left;
width: 100px;
height: 120px;
margin-left: 5px;
margin-right: 5px;
margin-bottom: 5px;
position: relative;
vertical-align:middle;
text-align:center;
}
ul.display li a img {
width: 94px;
height: 114px;
display: inline;
}
</style>
</head>
<body>
<div align="center">
<?php
include('connect.php');
$SQL = "SELECT * from becuart";
$result = mysql_query( $SQL );
echo "<ul class='display'>";
while( $row = mysql_fetch_array( $result ) ) {
$filepath = $row["path"];
echo "<li>";
echo "<a href=\"$filepath\"><img src=\"$filepath\" border=\"0\"></a>";
echo "</li>";
}
echo "</ul>";
?>
</div>
</body>
</html>`
最佳答案
因为它太长了 -> 新答案:
<?php
include 'dbconnect.php';
?>
<form name="product" method="post" action="">
<table align="right" width="10%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Category</td>
<td>
<select name="category">
<?php
$sql = "SELECT id, art_name, path FROM category;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
<option value="<?= $row['id']; ?>"><?= $row['art_name']; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input name="go" type="submit" value="Go" /></td>
</tr>
</table>
</form>
<div align="center">
<ul class='display'>
<?php
$id = (int)$_POST['category'];
$sql_search = "SELECT id, art_name, path FROM category WHERE id = $id";
$search = mysql_query($sql_search);
if (isset($_POST['go'])) {
while ($row = mysql_fetch_assoc($search)) {
?>
<li><a href="<?= $row['path']; ?>"><img src="<?= $row['path']; ?>" border="0"></a></li>
<?php }
}
else {
}
?>
</div>
关于php - 从数据库中选择下拉类别后,如何显示来自 mysql 表的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740571/