how to write the condition? - php

$tree = taxonomy_get_tree($vid);
print "<li>"; // 1 line
foreach ($tree as $term ) { // 2 lines
$diffdepth=0;
if ($term->depth > $depth) {
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
the above is the original code, now I want according to $term->depth > $depth make a condition to output.( print "<li>"; ) this line.
namely,
if ($term->depth > $depth) {
echo '<li class="parent">';
}
else {
print "<li>";
}
but $term->depth can use after foreach loop, but i want to use it at 1 line, how do i do?

Instead of printing in-line, assign your desired output to variables and then send it to the browser after the logic is complete.
$tree = taxonomy_get_tree($vid);
$parent = ""; // 1 line
$child = "";
foreach ($tree as $term) { // 2 line
$diffdepth=0;
if ($term->depth > $depth) {
$parent = "<li class='parent'>";
$child .= "<ul class='haschild'><li>";
$depth = $term->depth;
} else {
$parent = "<li>";
}
}
echo $parent . $child;
Please note you'll need to finish this up by adding all the applicable </li>s and whatnot, but this should get you started.

Use a counter:
$tree = taxonomy_get_tree($vid);
$counter = 0;
foreach ($tree as $term)
{
...
if ($term->depth > $depth)
{
if ($counter == 0) { echo '<li class="parent">'; }
else { echo '<li>'; }
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
...
$counter++;
}
If you need to write more differences based on your condition, you can turn tha above into:
$tree = taxonomy_get_tree($vid);
$counter = 0;
foreach ($tree as $term)
{
...
if ($term->depth > $depth)
{
if ($counter == 0) { echo '<li class="parent">'; }
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
else
{
if ($counter == 0) { echo '<li>'; }
...
}
...
$counter++;
}

if you want to build something like parent child hierarchy then you should check it by click here

Related

Yii2 recursion of the tree menu?

Good night all! Help please in the recursion, where I made a mistake, I need to output the categories, as in the last example!
Model:
protected function buildTree($data, $rootID = 0)
{
$tree = [];
foreach ($data as $id => $node) {
if ($node->parent_id == $rootID) {
unset($data[$id]);
$node->childs = $this->buildTree($data, $node->id);
$tree[] = $node;
}
}
return $tree;
}
public function getTree()
{
$data = Category::find()->all();
return $this->buildTree($data);
}
View:
<?php $form = ActiveForm::begin()
foreach ($tree as $cat) {
echo '<br><div class="spoiler-title">' . $cat['title'] . '</div>';
printNode($cat);
}
function printNode($cat, $level = 1)
{
if (count($cat['childs']) > 0) {
foreach ($cat['childs'] as $child) {
for ($j = 1; $j < $level; $j++) {
echo '------- <b>';
}
echo '' . $child['title'] . '</b><br>';
//echo $form->field($model, $child['title'])->checkbox();
//I want to do a checkbox, get an error, do not understand $
//form and $ model
if (count($child['childs']) > 0) {
printNode($child, $level + 1);
}
}
}
}
$form = ActiveForm::end() ?>
That's how I get the tree, this is with your code
root
category
category
category
---- <b>subcategory</b
---- <b>subcategory</b
---- <b>subcategory</b
---- <b>subcategory</b
category
category
I need to make checkboxes in subcategories, but I get an error, in the code I wrote in the comments, your code works, but I can not change it my needs as a picture.
You should also add recursion in view when you print tree.
Try this in view:
$form = \yii\bootstrap\ActiveForm::begin();
foreach ($tree as $cat) {
printNode($cat);
}
function printNode($cat, $level = 1) {
echo '<br><div class="spoiler-title">' . $cat['title'] . '</div>';
if (count($cat['childs']) > 0) {
foreach ($cat['childs'] as $child) {
echo \yii\helpers\Html::checkbox('someName[]') . ' ' . $child['title'] . '<br>';
if (count($child['childs']) > 0) {
printNode($child, $level + 1);
}
}
}
}
\yii\bootstrap\ActiveForm::end();

PHP - Drop down not showing all items in mysql

I am Trying to make a drop down menu, and list multiple items as well.
like for example:
here is my database:
if you see in that image you can see only one of the rows have a parent if i want multiple rows to have a parent of 'Far Far Away123' it only shows one of the items
<?php
function build_dropdown ($parent, $pages){
$items = "";
foreach($pages as $page){
// $items = "";
if($page['parent'] == $parent){
$items = $page;
} // END if
} // END foreach
if(is_array($items)){ // If a sub
echo '<ul id="sub_menu" class="sub_navagation'. $items['id'] .'">';
echo '<li>'.$items['page_name'].'</li>';
echo '</ul>';
} // END if
}// End Function
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row; // Add each row to $pages array to use later
}
foreach($pages as $key => $page){
if($page['parent'] == 'none'){ ?>
<li id = "<?php echo $page['id']; ?>">
<a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo $page['page_title']; ?>">
<?php echo $page['page_name']; ?>
</a>
<?php ?>
<?php
} // END if
build_dropdown($page['page_name'], $pages); // If there are child items then build them out
echo "</li> ";
} // END foreach
?>
Thanks
It is because you add only one.
You have a foreach searching all pages for subitems and remember only one of them for the latter if (is_array())
function build_dropdown($parent, $pages)
{
// item is an array
$items = array();
foreach($pages as $page) {
if ($page['parent'] == $parent) {
// add an element to the array
$items[] = $page;
} // END if
} // END foreach
if ($items) {
echo '<ul id="sub_menu" class="sub_navagation">';
foreach ($items as $item) {
echo '<li>'.$item['page_name'];
build_dropdown($item['page_name'], $pages);
echo '</li>';
} // END foreach
echo '</ul>';
} // END if
}// End Function
By the way, it would be better to start with the function giving build_dropdown($parent = 'none', $pages)
Try this inside your buildDropdown() function..
$items = array();
foreach($pages as $page)
{
if($page['parent'] == $parent)
{
$items[] = $page;
}
}
if (count($items) > 0)
{
echo '<ul id="sub_menu_'.$parent.'" class="sub_navagation">';
foreach($items as $item)
{
echo '<li>'.$item['page_name'].'</li>';
}
echo '</ul>';
}

Multilevel Hierarchical select option using php

I want to generate a multilevel hierarchical select option that is indented right as we move from parent to child. I tried using recursion in PHP(CI) for this but can't generate what I needed.
This is the function I tried to generate the select option.
<?php
$category_list = $this->helper_model->get_category();
/* $category list contains
[{"id":"1","name":"Information Technology","parent_id":"0"},
{"id":"2","name":"Programming","parent_id":"1"},
{"id":"3","name":"Civil","parent_id":"5"},
{"id":"4","name":"Networking","parent_id":"1"},
{"id":"5","name":"Engineering","parent_id":"0"},
{"id":"6","name":"Electrical","parent_id":"5"},
{"id":"11","name":"Hardware","parent_id":"1"},
{"id":"12","name":"Car Driver","parent_id":"13"},
{"id":"13","name":"Driver","parent_id":"0"},
{"id":"14","name":"java","parent_id":"2"},
{"id":"15","name":"javascript","parent_id":"2"}]
*/
$select_option = $this->multilevel_select($category_list);
echo '<select name = "subCategory">
<option>Select Category</option>' . $select_option;
function multilevel_select($array,$parent_id = 0,$parents = array()) {
static $i=0;
if($parent_id==0)
{
foreach ($array as $element) {
if (($element['parent_id'] != 0) && !in_array($element['parent_id'],$parents)) {
$parents[] = $element['parent_id'];
}
}
}
$menu_html = '';
foreach($array as $element){
if($element['parent_id']==$parent_id){
$menu_html .= '<option>';
for($j=0; $j<$i; $j++) {
$menu_html .= '—';
}
$menu_html .= $element['name'].'</option>';
if(in_array($element['id'], $parents)){
$i++;
$menu_html .= $this->multilevel_select($array, $element['id'], $parents);
}
}
}
$i=0;
return $menu_html;
}
?>
Can anyone help me get through this?

Limit number of sub_menu items to 4

I am trying to limit the number of sub_menu items (li) to maximum of 4. I'm no php developer but gave it a go with some code as provided below.
This is the existing code, which will just keep displaying it all, no limit set right now.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
foreach ($sub_menu_array as $sub_menu_row) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Here is what I tried but it ended up displaying nothing instead.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Syntax of PHP foreach statement has two different variants
foreach (array_expression as $value){
statement
}
foreach (array_expression as $key => $value)
statement
When you chanched code from
foreach ($sub_menu_array as $sub_menu_row) {
to
foreach ($sub_menu_array as $sub_menu_row => $v) {
You also changed values what appropriated to $sub_menu_row. For example:
$sub_menu_array = array ('a','b');
In the first variant at first iteration
$sub_menu_row=='a'
and in second variant
$sub_menu_row==0 #array's element key
$v=='a' #value
There are two solutions
Simply remove '=> $v'
Change $sub_menu_row to $v inside foreach statement
You have changed foreach, you should now use $v as a value.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($v['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
This should be enough for the menu part:
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
if($i < 4) {
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
$i++;
}
echo '</ul></nav>';
Insert this line before displaying the menu
$sliced_sub_menu_array = array_slice($sub_menu_array, 0, 4);
and then
foreach ($sliced_sub_menu_array as $sub_menu_row => $v) {
// displaying here
}
Use this code .
<?php
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if ($i == 3)
{
break;
}
$i++;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
?>

How to build multi level navigation with PHP

Im working on a prototype, and would like to build a multi level navigation - however not by looping through an array. I have a $depth and a $children, which should determine the depth of the navigation and the number of children on each level. So $depth = 4, $children = 8 would yield 4096 menu items.
This is a snippet af the output I would like:
<ul>
<li class="level-1">
Subject 1
<ul>
<li class="level-2">
Subject 1.1
<ul>
<li class="level-3">
Subject 1.1.1
</li>
...
</ul>
</li>
...
</ul>
</li>
...
</ul>
So far I have tried this, but I cant get my head around it :(
function draw_list ($depth, $children) {
echo '<ul>';
for ($i = 0; $i < $children; $i++) {
echo '<li>' . ($i++);
$depth--;
if ($depth > 0) {
echo draw_list($depth, $children);
}
echo '</li>';
}
echo '</ul>';
}
Several things required as I see it...
The $depth--; needs to be outside of the for loop
You were incrementing $i twice, once in the for statement, and then again on the echo '<li>' . ($i++);
The $depth check was stopping one early, so make >= instead of just > (Edit, thinking about it, this is an incorrect statement)
That should give you...
function draw_list ($depth, $children) {
echo '<ul>';
$depth--;
for ($i = 0; $i < $children; $i++) {
echo '<li>' . $i;
if ($depth > 0) {
echo draw_list($depth, $children);
}
echo '</li>';
}
echo '</ul>';
}
Update
For the display of the level numbering, try passing a string value through as a parameter...
function draw_list ($depth, $children, $display=''){
echo '<ul>';
$depth--;
for ($i = 0; $i < $children; $i++) {
echo '<li>' . $display . ($i + 1);
if ($depth > 0) {
echo draw_list($depth, $children, $display . ($i + 1) . '.');
}
echo '</li>';
}
echo '</ul>';
}
I ended up doing this:
function build_nav ($depth, $children, $levels = array()) {
echo '<ul>';
$depth--;
for ($i = 0; $i < $children; $i++) {
$levels[$depth] = $i+1;
echo '<li>';
echo 'Subject: ' . implode('.', $levels);
if ($depth > 0) {
build_nav($depth, $children, $levels);
}
echo '</li>';
}
echo '</ul>';
}

Categories