Rajitha Rajitha - 3 months ago 7
CSS Question

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'] : '';
}


enter image description here

Answer

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