当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to display select multiple drop down from using array in php?

问题描述:

I want drop down like parent,child means multilevel tasking using bootstrap CSS.

I tried the below one but I am getting all courses name in child.I want drop down like

Linux

--Linux Basics

CSS

--CSS Basics

Below is my code:

<ul class="sub-menu" style="border-right: 2px solid #012340;">

<?php foreach($outContentArrResults AS $courseName=> $valArrMenu){ ?>

<li class="menu-item-has-children">

<a href="#"><?php echo isset($valArrMenu[0]) ? $valArrMenu[0]:'';? ></a>

<ul class="sub-menu" style="border-right: 2px solid #012340;border-left: 2px solid #012340;">

<?php for($i=0; $i<count($valArrMenu['slug']); $i++){?>

<li>

<a href="<?php echo $config['LIVE_URL'];?>courses/<?php isset($valArrMenu['slug'][$i]) ? $valArrMenu['slug'][$i]:'';?>"><?php echo isset($valArrMenu[$i]) ? $valArrMenu[$i]:'';?></a>

</li>

<?php } ?>

</ul>

</li>

<?php } ?>

</ul>

Below is my array Value:

 Array

(

[mainCourse] => Array

(

[0] => linux

[slug] => Array

(

[0] => linux

[1] => linux-basics

[2] => first-steps

[3] => last

[4] => sdd

[5] => css

[6] => css-basics

[7] => css-introduction

[8] => linux-sub

[9] => test-sub-sub-linux

)

[1] => linux Basics

[2] => first steps

[3] => last

[4] => sdd

[5] => css

[6] => css Basics

[7] => css Introduction

[8] => linux sub

[9] => test sub sub linux

)

)

$arrCourses = $this->objWsCoursesModel->getMainMenuCourses();

//print_r($arrCourses);

$outResults = array();

for($i=0; $i<count($arrCourses); $i++){

$courseName = isset($arrCourses[$i]['course_name']) ? $arrCourses[$i]['course_name'] : '';

$outResults['mainCourse'][] = $courseName;

$outResults['mainCourse']['slug'][] = isset($arrCourses[$i]['course_slug_name']) ? $arrCourses[$i]['course_slug_name'] : '';

}

网友答案:

You are making a very complex array which will be hard to implement ;)

Try this array :

$arrCourses = $this->objWsCoursesModel->getMainMenuCourses();
$outResults = array();
for($i=0; $i<count($arrCourses); $i++){
    $courseName = isset($arrCourses[$i]['course_name']) ?    $arrCourses[$i]['course_name'] : '';
    $outResults['mainCourse'][$courseName][] = isset($arrCourses[$i]['course_slug_name']) ? $arrCourses[$i]['course_slug_name'] : '';;
}

And try this code. This is kind of rough code. I am not sure any syntax error but I think it will work like this:

<ul>
<?php foreach($outContentArrResults AS $courseName=> $valArrMenu){ ?>
<li class="menu-item-has-children">
    <a href="#"><?php echo $courseName ?></a>
    <ul class="sub-menu" style="border-right: 2px solid #012340;border-left: 2px solid #012340;">
        <?php for($valArrMenu as $row){?>
        <li>
            <a href="<?php echo $config['LIVE_URL'];?>courses/<?php isset($valArrMenu['slug'][$i]) ? $valArrMenu['slug'][$i]:'';?>"><?php echo isset($valArrMenu[$i]) ? $valArrMenu[$i]:'';?></a>
        </li>
        <?php } ?>
    </ul>
</li>
<?php } ?>
</ul>
网友答案:

You may try like this, which returns value in a Array format. I used this function for Item category Tree,

   function categoryDropDown($categoryArray, $parentId, $level, $options)
{
    $level++;
    foreach ($categoryArray  as  $array)
    {
        if($array['parentId'] == $parentId)
        {
            $opt = $array['name'] ;
            $categoryLevel = $level -1;
            $options[$array['id']] =  array("id"=> $array['id'], "categoryName"=>"$opt","level" => "$categoryLevel", "status" => $array['status']);
            $newParent = $array['id'];
            $options = categoryDropDown($categoryArray, $newParent, $level , $options);
        }
    }
    return $options;
}

----output----

    $options= categoryDropDown();      //now it will converted into an Array.

so you can print options like this

  <?php
    foreach($options as $value)
    {
    ?>

    <option value="<?php echo $value['id']; ?>"> <?echo $value['categoryName']; ?> </option>

    //Which is similar to <option value="option name/id">Option 1</option>

    <?php
    }
    ?>

Array output is like below (here index is a primary key from my DataBase)

Array
(
    [1] => Array
        (
            [id] => 1
            [categoryName] => Electronics
            [level] => 0
            [status] => d
        )

    [3] => Array
        (
            [id] => 3
            [categoryName] => Laptops
            [level] => 1
            [status] => a
        )

    [6] => Array
        (
            [id] => 6
            [categoryName] => Laptop Accessories
            [level] => 2
            [status] => a
        )

    [2] => Array
        (
            [id] => 2
            [categoryName] => Mobile
            [level] => 1
            [status] => a
        )

    [5] => Array
        (
            [id] => 5
            [categoryName] => Mobile Accessories
            [level] => 2
            [status] => d
        )

    [4] => Array
        (
            [id] => 4
            [categoryName] => Tablet
            [level] => 1
            [status] => a
        )

    [12] => Array
        (
            [id] => 12
            [categoryName] => Fashion
            [level] => 0
            [status] => a
        )

    [13] => Array
        (
            [id] => 13
            [categoryName] => Men
            [level] => 1
            [status] => a
        )

    [15] => Array
        (
            [id] => 15
            [categoryName] => Jeans
            [level] => 2
            [status] => a
        )

    [14] => Array
        (
            [id] => 14
            [categoryName] => Women
            [level] => 1
            [status] => a
        )

    [16] => Array
        (
            [id] => 16
            [categoryName] => Jeans
            [level] => 2
            [status] => a
        )

    [11] => Array
        (
            [id] => 11
            [categoryName] => Main Category 2
            [level] => 0
            [status] => a
        )

    [33] => Array
        (
            [id] => 33
            [categoryName] => temp
            [level] => 0
            [status] => a
        )

)

You can also refer Simple recursive tree in PHP / MySQL

网友答案:

Yes , munjal's solution is correct. and also you are making very complex structure of array. If you good with jquery and json parsing than simply, On page load call js function where you create json array for your menu tree. and from db you can manage easily.

网友答案:

So here's my edit to the problem, not sure if I interpreted the problem correctly:

$config['LIVE_URL'] = 'https://example.com/'
$arr_courses = $this->objWsCoursesModel->getMainMenuCourses();
$menu_data = [];
for ($i = 0; $i < count($arr_courses); $i++)
{
    $course_name = isset($arr_courses[$i]['course_name']) ? $arr_courses[$i]['course_name'] : '';
    $menu_data['mainCourse'][$course_name]['title'][$i] = $course_name;
    $menu_data['mainCourse'][$course_name]['slug'][$i] = isset($arr_courses[$i]['course_slug_name']) ? $arr_courses[$i]['course_slug_name'] : '';
}
// $menu_data should look something like this
$menu_data = [
    'linux' => [
        'title' => [
            'linux Basics',
            'first steps',
            'last',
            'sdd',
            'linux sub',
            'test sub sub linux',
        ],
        'slug' => [
            'linux-basics',
            'first-steps',
            'last',
            'sdd',
            'linux-sub',
            'test-sub-sub-linux',
        ],
    ],
    'css' => [
        'title' => [
            'css',
            'css Basics',
            'css Introduction',
        ],
        'slug' => [
            'css',
            'css-basics',
            'css-introduction',
        ],
    ]
];
?>
<ul class="sub-menu">
    <?php foreach($menu_data as $course_name => $data): ?>
        <li class="menu-item-has-children">
            <a href="#"><?php echo $course_name; ?></a>

            <ul class="sub-menu">                
                <?php for ($i = 0; $i < count($data['slug']); $i++): ?>
                    <li>
                        <a href="<?php echo $config['LIVE_URL'];?>courses/<?php echo isset($data['slug'][$i]) ? $data['slug'][$i]:'';?>"><?php echo isset($data['title'][$i]) ? $data['title'][$i]:'';?></a>
                    </li>
                <?php endfor; ?>
            </ul>
        </li>
    <?php endforeach; ?>
</ul>

Which outputs:

linux                          (href="#")
    linux Basics               (href=https://example.com/courses/linux-basics)
    first steps                (...)
    last
    sdd
    linux sub
    test sub sub linux
css                            (href ="#")
    css                        (href=https://example.com/courses/css)
    css Basics                 (...)
    css Introduction

From this you can add css to style it properly, which shouldn't be too much of a problem.

分享给朋友:
您可能感兴趣的文章:
随机阅读: