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


Creating a series of dropdown boxes from a MySQL database

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

+-------------+--------------+---------------+
|  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%说清楚,请不要否决我,只管问,我会解释

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

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!
}
这是基本的递归,你必须根据你的需要调整它…原理是一样的

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

这个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>

我想在一些这样的地方:

$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 />";
}

但你必须一直尝试面对你的问题,直到最后,如果你所有的汗水耗尽,然后你打电话求助。否则你将永远无法提高你的程序员能力。想想…