mysql中选择用户_当用户在选择字段中选择新选项时,如何运行mysql查询?

我想要一个列出产品类别的选择框。选择类别时,我想同时从数据库中选择该类别的产品。我需要在这个应用程序中使用AJAX吗?任何关于这样做的例子?以下是我正在使用的代码:

这些功能构建每个选择字段的选项。

function buildCategoryOptions($catId = 0)

{

$sql = "SELECT cat_id, cat_parent_id, cat_name

FROM tbl_category

ORDER BY cat_id";

$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());

$categories = array();

while($row = dbFetchArray($result)) {

list($id, $parentId, $name) = $row;

if ($parentId == 0) {

// we create a new array for each top level categories

$categories[$id] = array('name' => $name, 'children' => array());

} else {

// the child categories are put int the parent category's array

$categories[$parentId]['children'][] = array('id' => $id, 'name' =>

$name);

}

}

// build combo box options

$list = '';

foreach ($categories as $key => $value) {

$name = $value['name'];

$children = $value['children'];

$list .= "

if ($key == $catId) {

$list.= " selected";

}

$list .= ">$name

\r\n";

foreach ($children as $child) {

$list .= "

if ($child['id'] == $catId) {

$list.= " selected";

}

$list .= ">  {$child['name']}

\r\n";

}

}

return $list;

}/ *

构建收音机选项的产品选项列表

* /

function buildProductOptions($catId = 0)

{

$sql = "SELECT pd_id, pd_name, cat_id

FROM tbl_product

WHERE cat_id = $catId

ORDER BY pd_name";

$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());

$numProduct = dbNumRows($result);

$products = array();

while($row = dbFetchArray($result)) {

list($id, $name) = $row;

// we create a new array for each top level categories

$products[$id] = array('name' => $name);

}

// build combo box options

$list = '';

foreach ($products as $key => $value) {

$name = $value['name'];

$list .= "

$list .= ">$name

\r\n";

}

return $list;}

这是选择字段的页面:

$catId = (isset($_GET['catId']) && $_GET['catId'] > 0) ? $_GET['catId'] : 0;

$categoryList = buildCategoryOptions($catId);

$productList = buildProductOptions($catId);

-- Choose Category --

echo $categoryList;

?>

--Pick the other options--

echo $productList;

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值