English 中文(简体)
从MySQL数据库中创建一系列落式箱
原标题:Creating a series of dropdown boxes from a MySQL database
  • 时间:2011-10-17 15:12:11
  •  标签:
  • 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功能,以产生一系列与所选择的母类别 drop倒的箱子。 例如:

如果我通过该功能,请上<代码>5。 产出如下:

<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%,那么我就只能要求我放弃表决。

问题回答

You ll need to build a recursive function, something like the following:

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!
}

——这是基本的再入侵,你不得不为了你们的需要而 t。

当我需要产生下降的菜单时,我谨发挥帮助作用。

This PHP function:

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);

?>

产生这一超文本:

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

但是,如果你们的血汗 out尽一切,你们总是要努力面对你们的问题,那么你们就会要求帮助。 否则,你就永远不会提高你的方案能力。 思考......





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签