从MySQL数据库创建一系列下拉框

时间:2011-10-17 15:12:12

标签: php mysql

我有一个问题,我只是想不出一个合乎逻辑的方法来克服。我有一张类似的表:

+-------------+--------------+---------------+
|  id         |  catName     |  parentId     |
+-------------+--------------+---------------+
|  1          | Category 1   |  0            |
|  2          | Category 2   |  0            |
|  3          | Sub Cat 1    |  1            |
|  4          | Sub Cat 2    |  1            |
|  5          | Sub sub cat 1|  4            |
|  6          | Sub sub cat 2|  4            |
|  7          | Category 3   |  0            |
+-------------+--------------+---------------+

我需要编写一个PHP函数来输出一系列选择了父类别的下拉框。例如:

如果我传递了函数id 5,将输出以下HTML结构:

<select name="level-1">
   <option selected="selected">Category 1</option>
   <option>Category 2</option>
   <option>Category 3</option>
</select>

<select name="level-2">
   <option>Sub Cat 1</option>
   <option selected="selected">Sub Cat 2</option>
</select>

<select name="level-3">
   <option selected="selected">Sub sub cat 1</option> // This is the category with id=5
   <option>Sub sub cat 1</option>
</select>

很难找到一个问这个问题的方法,所以如果我没有让自己100%清楚,请不要投票给我,只要问我就解释

3 个答案:

答案 0 :(得分:0)

您需要构建一个递归函数,如下所示:

function recursive_dropdown($category_id) {
    $category = // fetch category from database
    echo '<option value="'. $category['id'] .'">'. $category['name'] .'</option>';

    $parent = // fetch it's parent
    if( // parent exists ) {
        return recursive_dropdown($parent['id']);
    }
    return true; // done!
}

---编辑这是基本的递归,你必须根据自己的需要进行调整......原理是一样的

答案 1 :(得分:0)

当我需要生成下拉菜单时,我喜欢使用辅助函数。

这个PHP函数:

function CreateDropdown($name, $array, $selected="")
{
    $result = "<select name='$name'>";

    foreach ($array as $key => $value) {
        $result .= "<option value='$key'";
        if ($key == $selected)
            $result .= " selected='selected'";
        $result .= ">";
        $result .= $value."</option>";
    }

    $result .= "</select>";
    return $result;
}

echo CreateDropdown('level-1',
                    array(1=>'category 1', 2=>'category 2', 3=>'category 3'),
                    3);

?>

生成此HTML:

<select name='level-1'>
    <option value='1'>category 1</option>
    <option value='2'>category 2</option>
    <option value='3' selected='selected'>category 3</option>
</select>

答案 2 :(得分:0)

我觉得有点像这样:

$id = 5; // Your ID

// DB data => ID - Description - Category
$data = array(
1 => array("Category 1",    0),
2 => array("Category 2",    0),
3 => array("Sub Cat 1",     1),
4 => array("Sub Cat 2",     1),
5 => array("Sub sub cat 1", 4),
6 => array("Sub sub cat 2", 4),
7 => array("Category 3",    0));

$dds = array(); // Array that will hold the result

function reverse_recursively($id, $data, &$dds)
{
    $dd = "<select>"; // Open select tag
    $cat = $data[$id][1]; // Find the current category

    foreach ($data as $i => $d) // Fetch data
    {
        if ($d[1] == $cat) // If is in the category
        {
            $sel = $id == $i ? " selected='selected'" : "";

            // The option tag
            $dd.= "
                <option value='$i'$sel>" . $d[0] . "</option>";
        }
    }

    $dds[] = $dd . "</select>"; // Close select tag

    if ($cat != 0) // Category 0(zero) means the end so it have to be diferent to proceed.
        reverse_recursively($cat, $data, $dds);
}

reverse_recursively($id, $data, $dds);

// In each DDS item you got the dropdown ;-)
foreach ($dds as $dropdown)
{
    echo $dropdown . "<br /><br />";
}

但是你总是试图面对你的问题,直到最后,如果你的汗水耗尽,那么你就会求助。否则你永远不会增加你的程序员能力。想想......