Php 从MySQL数据库创建一系列下拉框
我有一个问题,我就是想不出一个合乎逻辑的方法来克服。我有一张类似的桌子:Php 从MySQL数据库创建一系列下拉框,php,mysql,Php,Mysql,我有一个问题,我就是想不出一个合乎逻辑的方法来克服。我有一张类似的桌子: +-------------+--------------+---------------+ | id | catName | parentId | +-------------+--------------+---------------+ | 1 | Category 1 | 0 | | 2 | Category
+-------------+--------------+---------------+
| 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!
}
--编辑这是基本的递归,你必须根据你的需要调整它。。。原理是一样的当我需要生成下拉菜单时,我喜欢使用helper函数 此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 />";
}
但是你必须一直努力面对你的问题,直到最后,如果你的汗水都流光了,那么你就呼救。否则你将永远无法提高你的程序员能力。想想……到目前为止你都试过什么?这并不特别困难。获取/构建父类别,然后是获取/输出每个子类别的循环…我还没有尝试过任何东西,我想不出它背后的逻辑是什么,我真的不知道从哪里开始。这快把我逼疯了!那样的话我们可以在这里帮助解决具体问题。我不知道该怎么做,所以请为我做这类事情。我不是要求任何人为我做这类事情,只是告诉我应该做的方向。我无法让这项工作正常进行:你能扩展这个答案吗?