PHP MySQL 两个 while 列表,第二个列表采用前一个列表的值,其中两个列表都是动态的(从数据库获取值)
这些列表用于“类别”和“子类别”,我希望如果用户选择第一个类别,那么第二个列表将显示与所选类别相关的所有子类别,我使用 PHP 和 MySQL,
这是我的第一个列表代码:
<?php
require("connect.php");
$extract = mysql_query("SELECT * FROM categories ORDER BY catg_code ASC") or die (mysql_error());
$numrows = mysql_num_rows($extract);
while ($row = mysql_fetch_assoc($extract))
{
$catgname = $row['catg_name'];
$id = $row['catg_code'];
echo "<option value='$id' onClick='$var=$id' > $catgname </option> ";
}
?>
这是我的第二个列表代码:
<?php
require("connect.php");
//$select4 = $_POST['select2'];
$extract = mysql_query("SELECT * FROM subcategories WHERE catg_code='$var' ORDER BY subCatg_code ASC") or die (mysql_error());
$numrows = mysql_num_rows($extract);
while ($row = mysql_fetch_assoc($extract))
{
$scatgname = $row['subCatg_name'];
$sid = $row['subCatg_code'];
echo "<option name='mm' value='$sid'> $scatgname </option> ";
}
?>
数据库中的表包括(类别、子类别和产品)类别包括(catg_code 和 catg_name) subCategories 表包括(subcatg_code、subCatg_name 和 catg_code)
the lists are for "Categories" and "Sub-Categories" i want if the user select the first category then the second list will display all the sub-categories that related to the selected category, am using PHP and MySQL,
this is my fisrt list code:
<?php
require("connect.php");
$extract = mysql_query("SELECT * FROM categories ORDER BY catg_code ASC") or die (mysql_error());
$numrows = mysql_num_rows($extract);
while ($row = mysql_fetch_assoc($extract))
{
$catgname = $row['catg_name'];
$id = $row['catg_code'];
echo "<option value='$id' onClick='$var=$id' > $catgname </option> ";
}
?>
this is my second list code:
<?php
require("connect.php");
//$select4 = $_POST['select2'];
$extract = mysql_query("SELECT * FROM subcategories WHERE catg_code='$var' ORDER BY subCatg_code ASC") or die (mysql_error());
$numrows = mysql_num_rows($extract);
while ($row = mysql_fetch_assoc($extract))
{
$scatgname = $row['subCatg_name'];
$sid = $row['subCatg_code'];
echo "<option name='mm' value='$sid'> $scatgname </option> ";
}
?>
the tables in database includes (categories, subcategories and products) category includes (catg_code and catg_name)
subCategories table include (subcatg_code, subCatg_name and catg_code)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要了解主键和外键。
如果您从子类别表中引用了主类别,则只需运行查询一次。阅读有关连接查询的内容,例如内部连接和外部连接,那里有很多教程(Google mysql 连接)。
You need to know how about primary and foreign keys.
If you have a reference to the main category from your subcategory table you would only need to run the query once. Have a read up on join queries such as inner join and outer join there are plenty of tutorials out there (Google mysql joins).