开发者

How to indent text in a select drop down option menu using PHP & CSS? [REDUX]

I asked a similar question earlier but I'll ask it again in a different way because I re-worked the code a little.

I was wondering how can I indent categories and endless sub categories that I have in a select drop down menu using PHP & CSS?

Here is my PHP code to display the select drop down.

echo '<select name="parent_id">
      <option value="0">None</option>';

function make_list ($paren开发者_如何学运维t) {

    global $option;

    foreach ($parent as $id => $cat) {

        echo '<option value="' . $cat['id'] . '">' . $cat['category'] . '</option>';

        if (isset($option[$id])) { 

            make_list($option[$id]);

        }                       
    }       
}

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");

if (!$dbc) {
    print mysqli_error();
} 

$option = array();

while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {

    $option[$parent_id][$id] =  array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);

}

make_list($option[0]);

echo '</select>';

Here is the output.

   1. Apple
   2. Arts & Entertainment
         1. Amusement
         2. Art
         3. Artists
               1. A
                     1. a1
                     2. a2
               2. B
               3. C
               4. D
   3. Automotive
   4. Network
   5. Server
   6. Web Design
         1. CSS
         2. HTML

The numbers are just there to see the categories and sub categories easier.


I see you already have the recursion thing down -- try passing a "depth" parameter in your make_list function -- when you first call it, you'd set $depth to zero. Then, when you call it recursively, you'd say makelist($option[$id], $depth+1);

knowing what level of recursion you are on, you could easily construct a string of whitespace. Like:

$whitespace = str_repeat('&nbsp;', $depth);

for more indentation, try str_repeat('&nbsp;', $depth * 2); or similar

then just prepend your option with the whitespace.


You can try this, working example. Seperate with -

  echo '<select name="parent_id">
          <option value="">Seçiniz</option>';
    function make_list ($parent,$depth) {
        global $option;
        foreach ($parent as $id => $cat) {

            $whitespace = str_repeat('-', $depth * 1);
            echo '<option value="' . $cat['id'] . '">'. $whitespace . $cat['category'] . '</option>';

            if (isset($option[$id])) {

                make_list($option[$id], $depth+1);

            }
        }
    }

    $dbc = mysqli_query($vt,"SELECT * FROM menus");
    $option = array();
    while (list($id, $parent_id, $category) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {

        $option[$parent_id][$id] =  array('category' => $category, 'id' => $id, 'parent_id' => $parent_id);

    }
    make_list($option[0], $depth = 0);
    echo '</select>';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜