我想在大多数在线购物网站上显示基于类别的购物项目以及网页上的图片。我创建了两个 mysql表:我的 id , category_name ,第二位 id , categoryid ,产品, 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>`
答案 0 :(得分:0)
SELECT id, name, path FROM table;
...
<select name ...>
<?php
while( $row = mysql_fetch_array( $result ) ) {
?>
<option name="id" value="<?= $row['id']; ?>"><?= $row['name']; ?></option>
<?php
}
?>
</select>
所以通过javascript / jquery打开img,它在POSTed $ row ['id']上回答; (SELECT path FROM table WHERE id =?;)或简单的$ _POST ['id']这是字段的名称
答案 1 :(得分:0)
因为它太长了 - &gt;新答案:
<?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>