Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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><?phpforeach($outContentArrResultsAS$courseName=> $valArrMenu){ ?><liclass="menu-item-has-children"><ahref="#"><?phpecho$courseName?></a><ulclass="sub-menu"style="border-right: 2px solid #012340;border-left: 2px solid #012340;"><?phpfor($valArrMenuas$row){?><li><ahref="<?phpecho$config['LIVE_URL'];?>courses/<?phpisset($valArrMenu['slug'][$i]) ? $valArrMenu['slug'][$i]:'';?>"><?phpechoisset($valArrMenu[$i]) ? $valArrMenu[$i]:'';?></a></li><?php } ?></ul></li><?php } ?></ul>

Solution 2:

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

functioncategoryDropDown($categoryArray, $parentId, $level, $options)
{
    $level++;
    foreach ($categoryArrayas$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

<?phpforeach($optionsas$value)
    {
    ?><optionvalue="<?phpecho$value['id']; ?>"><?echo$value['categoryName']; ?></option>

    //Which is similar to <optionvalue="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

Solution 3:

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.

Solution 4:

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',
        ],
    ]
];
?>
<ulclass="sub-menu"><?phpforeach($menu_dataas$course_name => $data): ?><liclass="menu-item-has-children"><ahref="#"><?phpecho$course_name; ?></a><ulclass="sub-menu"><?phpfor ($i = 0; $i < count($data['slug']); $i++): ?><li><ahref="<?phpecho$config['LIVE_URL'];?>courses/<?phpechoisset($data['slug'][$i]) ? $data['slug'][$i]:'';?>"><?phpechoisset($data['title'][$i]) ? $data['title'][$i]:'';?></a></li><?phpendfor; ?></ul></li><?phpendforeach; ?></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.

Post a Comment for "How To Display Select Multiple Drop Down From Using Array In Php?"