How To Display Select Multiple Drop Down From Using Array In Php?
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>
<?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>
Solution 2:
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
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',
],
]
];
?>
<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.
Post a Comment for "How To Display Select Multiple Drop Down From Using Array In Php?"