如何使级联使用MySQL和PHP的下拉列表 [英] How to make cascading drop-down lists using mysql and php

查看:98
本文介绍了如何使级联使用MySQL和PHP的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定要多下拉菜单修改彼此的一种形式。该菜单/数据库表有:

I have to make a form with multiple drop-down menus that modify one another. The menus/database tables are:

类别,样式,类型和机制

Categories, Styles, Types, and Mechanisms

我试图使用Ajax我有限的知识做到这一点,但似乎只是一次访问MySQL(初始页)来填充类别表,而不能再通过查询下一组更新的样式表的结果。我收到索赔数据库是空的错误。

I have attempted to do this with my limited knowledge of Ajax, but can only seem to access MySQL once (on the initial page) to populate the Categories table without being able to then update the Styles table by querying for the next set of results. I receive an error that claims the database is empty.

我也试过有下拉通过选项组填充同时处理的类别和风格有一个环形的查询,但只有类别标题显示了各具风格的子数值显示出来的空白。我的code是如下:

I also tried having the drop-down populated through an option group to handle both the Categories and Styles with a looped query, but only the Category headings show up with all of the Style sub-values showing up blank. My code is as follows:

                $query1="SELECT categories.category_id, categories.Category_Name ";
                $query1.="FROM categories ";
                $query1.="ORDER BY categories.Category_Name ASC";
                $category_result=mysql_query($query1, $connection);

                if(!$category_result){
                    die("Database query failed: " . mysql_error());
                }

                $options="";

                $con=0;

                while ($category_row=mysql_fetch_array($category_result)) {
                    $category_name=$category_row["Category_Name"];
                    $CategoryID=$category_row["category_id"];

                    $options.="<OPTGROUP LABEL=\"$category_name\"> <br />";

                    $query2="SELECT categories.category_id, categories.Category_Name, ";
                    $query2.="styles.style_id, styles.Style_Name ";
                    $query2.="FROM categories, styles ";
                    $query2.="WHERE styles.Category_ID = $CategoryID ";
                    $style_result=mysql_query($query2, $connection);

                    if(!$style_result){
                        die("Database query failed: " . mysql_error());
                    }

                    while ($style_row=mysql_fetch_array($style_result)) {
                        $style_name=$row["Style_Name"];
                        $id=$row["style_id"];

                        $options.="<OPTION VALUE=\"$id\" <a href=\"#\" onClick=\"javascript:swapContent('$style_name');\" >".$style_name.'</OPTION>';
                    }
                    $options.='</OPTGROUP> <br />';
                }
            ?>

            <SELECT NAME="category_id">
                <OPTION VALUE=0></OPTION>
                <?php echo $options ?>choose
            </SELECT>

任何见识到了什么我做错了会大大AP preciated!

Any insight into what I am doing wrong would be greatly appreciated!

推荐答案

最后只是要弄清楚我自己,但感谢那些谁试图帮助。

Finally just had to figure it out on my own, but thanks for those who tried to help.

我将公布我的类/分类/类型/类型的层叠表我的中间表。首先做一个函数,如下面的:

I will post my middle table from my Class/Category/Style/Type cascading tables. First make a function such as the below:

 <?php //Category Selection
    function Category_Selection($link) {
        global $connection;
        $options="";
        if(isset($_GET["class_id"])) {
            $query="SELECT categories.category_id, categories.Category_Name ";
            $query.="FROM categories ";
            $query.="ORDER BY categories.Category_Name ASC";
            $result=mysql_query($query, $connection);
            $class_id=$_GET['class_id'];

            if(!$result){
                die("Database query failed: " . mysql_error());
            }
            while ($row=mysql_fetch_array($result)) {
                $name=$row["Category_Name"];
                $id=$row["category_id"];
                $link2=$link."&category_id={$id}";
                $options.="<OPTION VALUE=\"$link2\" ";
                if(isset($_GET["category_id"])) {
                    $category_id = $_GET['category_id'];
                    if($id==$category_id) {
                        $options.=" selected=\"selected\" ";
                    }
                }
                $options.=" >".$name.'</OPTION>';
            }
        } else {
            $options.="<OPTION selected=\"selected\" VALUE=0>First Select Class</OPTION>";
        }
        return($options);
    }
?>

然后把你的主页:

Then place on your main page:

<?php session_start() ?>
    //Category
    if(isset($_GET['category_id'])) {
        $category_id=$_GET['category_id'];
        setcookie('category_id',$category_id);
        $link.="&category_id={$category_id}";
    }elseif(isset($_COOKIE['category_id'])) {
        $_GET['category_id']=$_COOKIE['category_id'];
        $category_id=$_COOKIE['category_id'];
        $link.="&category_id={$category_id}";
    }

现在让你选择下拉:

<?php //Category Selection
                $options=Category_Selection($link);
            ?>
            <center>
            <SELECT NAME="category_id" ONCHANGE="location = this.options[this.selectedIndex].value;">
                <OPTION VALUE=0></OPTION>
                <?php echo $options ?>
            </SELECT>
            </center>

重复上述每个要求的下拉

Repeat above for each required drop-down.

祝你好运......,当然使用的mysqli来保护你的网站,而不是上面显示的MYSQL ......

Good luck...and, of course use MYSQLi to protect your site rather than the MYSQL shown above...

这篇关于如何使级联使用MySQL和PHP的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆