Created
April 11, 2018 13:01
-
-
Save radheymkumar/d4afdc70499f866bbd973d95efe15d90 to your computer and use it in GitHub Desktop.
sub category menu php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$server = "localhost"; | |
$username = "xxxx"; | |
$pasword = "xxxx"; | |
$dbName = "xxxx"; | |
$conn = new mysqli($server, $username, $pasword, $dbName); | |
if($conn->connect_error) { | |
die('Connection Error'. $conn->connect_error); | |
} | |
//echo "Connection Successfully"; | |
// SELECT LIST | |
function categoriesTree($parent_id = 0, $sub_mark = '') { | |
global $conn; | |
$query = $conn->query("SELECT * FROM category WHERE parent_id = $parent_id ORDER BY name ASC"); | |
if ($query->num_rows > 0) { | |
while ($row = $query->fetch_assoc()) { | |
echo '<option value="'.$row['id'].'">'.$sub_mark.$row['name'].'</option>'; | |
categoriesTree($row['id'], $sub_mark.'---'); | |
} | |
} | |
} | |
// UL LIST | |
function fetchCategoryTreeList($parent = 0, $user_tree_array = '') { | |
global $conn; | |
if (!is_array($user_tree_array)) | |
$user_tree_array = array(); | |
$sql = $conn->query("SELECT `id`, `name`, `parent_id` FROM `category` WHERE 1 AND `parent_id` = $parent ORDER BY id ASC"); | |
if ($sql->num_rows > 0) { | |
$user_tree_array[] = "<ul>"; | |
while ($row = mysqli_fetch_object($sql)) { | |
$user_tree_array[] = "<li>". $row->name."</li>"; | |
$user_tree_array = fetchCategoryTreeList($row->id, $user_tree_array); | |
} | |
$user_tree_array[] = "</ul>"; | |
} | |
return $user_tree_array; | |
} | |
?> | |
<select name ="categories"> | |
<?php categoriesTree(); ?> | |
</select> | |
<ul> | |
<?php | |
$res = fetchCategoryTreeList(); | |
foreach ($res as $r) { | |
echo $r; | |
} | |
?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment