Code for Tree structure using ul, li in php...
*In the output i am missing ul or in some place.*
I want this for checkboxes when we click the parent check box their childs must be selected and sub childs too.
<?php
$groups = array(
array('depth'=>0,'value'=>'DEF'),
array('depth'=>1,'value'=>'DEF 1'),
array('depth'=>2,'value'=>'DEF_1'),
array('depth'=>1,'value'=>'DEF2'),
array('depth'=>0,'value'=>'GROUP'),
array('depth'=>1,'value'=>'GROUP1'),
array('depth'=>1,'value'=>'GROUP2'),
array('depth'=>2,'value'=>'GROUP2_1'),
array('depth'=>2,'value'=>'TELUGU'),
array('depth'=>3,'value'=>'RAM'),
array('depth'=>0,'value'=>'GROUP3'),
array('depth'=>1,'value'=>'GROUP3_1'),
array('depth'=>1,'value'=>'GROUP3_2'),
array('depth'=>1,'value'=>'GROUP3_3'),
array('depth'=>1,'value'=>'DEF2_1'),
array('depth'=>0,'value'=>'GROUP4'),
array('depth'=>0,'value'=>'T8')
);
$count = count($groups);
$old_depth = 0;
echo '<ul>'.chr(13).chr(10);
for($i = 0; $i<$count; $i++)
{
if($old_depth==$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if($old_depth!=$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
}
}
}
if($old_depth<$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if(($groups[$i]['depth'])+1==$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
if(!($groups[$i]['depth']==$groups[$i+1]['depth']))
{
echo '</ul></li>';
}
if($groups[$i+1]['depth']==0)
{
echo '</ul></li>';
}
}
}
else
{
echo '</ul>';
}
}
}
echo '</li></ul>';
?>
I want the output in this format.
<ul>
<li>DEF
<ul>
<li>DEF1
<ul>
<li>DEF_1</li>
</ul>
</li>
<li>DEF2</li>
</ul>
</li>
<li>GROUP
<ul>
<li>GROUP1</li>
<li>GROUP2
<ul>
<li>GROUP2_1</li>
<li>TELUGU
<ul>
<li>RAM</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>GROUP3
<ul>
<li>GROUP3_1</li>
<li>GROUP3_2</li>
<li>GROUP3_3</li>
<li>DEF2_1</li>
</ul>
</li>
<li>GROUP4</li>
<li>T8</li>
</ul>
Related
I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</ul>';
}
?>
I am trying to do something fit with the design, so i have to show 2 item in 1 loop. Also, according to this design, should be line break in each 4 loop. Line breaks applying automatically, but its fixed 4. So, in 4 loop it shows actually 8 items. However, the point I'm stuck on is; items are not displaying in the placement I want. You can see the placement I want to do with more detail in the picture.
Red Numbers = Current placement
Green Numbers = Placement it should be
Yellow boxes = For loop items ( 8 items )
Black boxes = Items in array (16 items )
.
ul,li {
padding:0;
margin:0;
list-style:none;
}
li {
width:30px;
}
.gen {
width: 140px
}
li {
display: inline-block;
}
.ic {
width: 100%
}
<ul class="gen">
<li>
<ul>
<li class="ic">a</li>
<li class="ic">b</li>
</ul>
</li>
<li>
<ul>
<li class="ic">c</li>
<li class="ic">d</li>
</ul>
</li>
<li>
<ul>
<li class="ic">e</li>
<li class="ic">f</li>
</ul>
</li>
<li>
<ul>
<li class="ic">g</li>
<li class="ic">h</li>
</ul>
</li>
<li>
<ul>
<li class="ic">i</li>
<li class="ic">j</li>
</ul>
</li>
<li>
<ul>
<li class="ic">k</li>
<li class="ic">l</li>
</ul>
</li>
<li>
<ul>
<li class="ic">m</li>
<li class="ic">n</li>
</ul>
</li>
<li>
<ul>
<li class="ic">o</li>
<li class="ic">p</li>
</ul>
</li>
</ul>
As you can see, items are not placing alphabetically from left to right.
Here is the loop i used :
// 16 items
$items = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p');
$half = count($items) / 2;
$k = 0;
echo '<ul class="gen">';
for($i = 0; $i<$half; $i++) {
echo '<li>';
echo '<ul>';
echo '<li class="ic">'.$items[$k].'</li>';
echo '<li class="ic">'.$items[$k+1].'</li>';
echo '</ul>';
echo '</li>';
$k = $k+2;
}
echo '</ul>';
I hope this helps. You can run this code and see the output.
// chaganged the array to match your picture.
$items = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
//used to format the output
echo "<pre>";
for ( $i = 0; $i<count($items) ; $i++ ) {
echo "loop start \n";
// access 1st item
echo $items[$i]."\n";
// based on your example, it seems that you need the item that is 4 places after the first item of the loop
echo $items[$i+4]."\n";
echo "loop end\n";
// we can use mod to check if we have reached the 4th element (index 3), we use $i+1 to check the second element of the loop
if ( ( $i+1 )%4 == 0 ) {
echo "\n---new line---\n";
$i = $i + 4;
}
echo "\n";
}
echo "</pre>";
Assuming that you will take care of the formatting, your code could be changed to this:
$items = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p');
for($first = 0; $first < count($items); $first += 8) {
for($add = 0; $add < 4; $add++) {
echo $items[$first + $add]; //first item
echo $items[$first + $add + 4]; //second item
}
}
May I see your HTML code? Here's a suggestion, display your results like this (run code snippet and use my css and php loop);
#wrapper{
background-color:rgba(0,0,0,0.6);
padding:10px;
}
.result{
display:inline-block;
padding:7px;
border:3px solid #FEBF01;
width:120px;
}
.result-child{
display:block;
padding:10px 10px;
background-color:rgba(0,0,0,0.8);
color:rgba(50,255,100,1);
font-size:2em;
text-align:right;
}
.result .result-child:first-of-type{
margin-bottom:10px;
}
<div id="wrapper">
<div class="result">
<div class="result-child">1</div>
<div class="result-child">1</div>
</div>
<div class="result">
<div class="result-child">2</div>
<div class="result-child">2</div>
</div>
</div>
Loop code:
for($i = 0; $i<$half; $i++) {
echo "<div class='result'>";
echo "<div class='result-child'>".$items[$k]."</div>"; //first item
echo "<div class='result-child'>".$items[$k+1]."</div>"; //second item
echo "</div>";
$k = $k+2;
}
Is there a more elegant way to output the given html structure with an array? I found some recursive solutions but therefore a parent_id is needed (if I understand correctly).
HTML
<ul>
<li>Level 1</li>
<li>
<ul>
<li>Level 2</li>
<li>
<ul>
<li>Level 3</li>
</ul>
</li>
</ul>
</li>
</ul>
Array
Array
(
[0] => Level 1
[1] => Array
(
[0] => Level 2
[1] => Array
(
[0] => Level 3
)
)
)
Loops
<ul>
<?php foreach($array as $arr) : ?>
<li>
<?php if(is_array($arr)) : ?>
<ul>
<?php foreach($arr as $a) : ?>
<li>
<?php if(is_array($a)) : ?>
<ul>
<?php foreach($a as $aa) : ?>
<li><?php echo $aa; ?></li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<?php echo $a; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<?php echo $arr; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
I´m a bit worried about the performance of this approach :-D
do it just recursive:
<?PHP
function doOutputList($TreeArray)
{
echo '<ul>';
foreach($TreeArray as $arr)
{
echo '<li>';
if(is_array($arr))
{
doOutputList($arr);
}
else
{
echo $arr;
}
echo '</li>';
}
echo '</ul>';
}
doOutputList($array);
?>
Or if you like good readable HTML Code try this:
<?PHP
function doOutputList($TreeArray, $deep=0)
{
$padding = str_repeat(' ', $deep*3);
echo $padding . "<ul>\n";
foreach($TreeArray as $arr)
{
echo $padding . " <li>\n";
if(is_array($arr))
{
doOutputList($arr, $deep+1);
}
else
{
echo $padding .' '. $arr;
}
echo $padding . " </li>\n";
}
echo $padding . "</ul>\n";
}
doOutputList($array);
?>
I want add left, center & right class to my ordered lists while loop.
Code
<?php while ($fetch) { ?>
<li>haha</li>
<?php } ?>
Results should be
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
Let me know
<?php $classes = array("left","center","right");
$i = 0;
while ($fetch) {
?>
<li class="<?php echo $classes[$i++ % 3] ?>">haha</li>
<?php } ?>
$cnt=0;
while ($fetch)
{
switch ($cnt%3)
{
case 0 : $class = 'left'; break;
case 1 : $class = 'center'; break;
case 2 : $class = 'right'; break;
}
echo '<li class="', $class, '">haha</li>';
++$cnt;
}
I've just tested the following code and verified that it produces the desired output:
<?php
$items = array('haha', 'haha', 'haha', 'haha', 'haha', 'haha');
$cssClasses = array('left', 'center', 'right');
echo "<ul>\n";
$i=0;
foreach ($items as $item) {
echo "\t<li class=\"" . $cssClasses[$i++ % 3] . '">' . $item . "</li>\n";
}
echo "</ul>\n";
?>
The output is:
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
I have this issue with OpenCart where I want to display my shop categories in a custom way and count the parent categories.
I currently modified the code so far that I get the following output
<ul id="catOpContainer">
<li id="switchCatOp1">Parent Cat 1
<ul id="catOp1">
<li>Child cat 1</li>
Child Cat 2</li>
</ul>
</li>
Parent Cat 2
<ul id="catOp1">
<li>Child cat 1</li>
Child cat 2</li>
</ul>
</li>
Parent Cat 3</li>
</ul>
</ul>
instead of the desired
<ul id="catOpContainer">
<li id="switchCatOp1">Parent Cat 1
<ul id="catOp1">
<li>Child Cat 1</li>
<li>Child Cat 2</li>
</ul>
</li>
<li id="switchCatOp2">Parent Cat 2
<ul id="catOp2">
<li>Child Cat 1</li>
<li>Child Cat 2</li>
<li>Child Cat 3</li>
</ul>
</li>
</ul>
It's obvious that there are some missing elements, but I have no clue about a possible solution. I also don't have a clue how to count the parent categories, so that I can toggle the subcategories.
I currently have the following code snippet:
<?php
class ControllerModuleCategory extends Controller {
protected $category_id = 0;
protected $path = array();
protected function index() {
$this->language->load('module/category');
$this->data['heading_title'] = $this->language->get('heading_title');
$this->load->model('catalog/category');
$this->load->model('tool/seo_url');
if (isset($this->request->get['path'])) {
$this->path = explode('_', $this->request->get['path']);
$this->category_id = end($this->path);
}
$this->data['category'] = $this->getCategories(0);
$this->id = 'category';
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
} else {
$this->template = 'default/template/module/category.tpl';
}
$this->render();
}
protected function getCategories($parent_id, $current_path = '') {
$category_id = array_shift($this->path);
$output = '';
$results = $this->model_catalog_category->getCategories($parent_id);
if ($results) {
if ($parent_id == 0) {
$output .= '<li id="switchCatOp1">';
} else {
$output .= '<ul id="catOp1"><li>';
}
}
foreach ($results as $result) {
if (!$current_path) {
$new_path = $result['category_id'];
} else {
$new_path = $current_path . '_' . $result['category_id'];
}
$output .= '';
$children = '';
// if ($category_id == $result['category_id']) {
$children = $this->getCategories($result['category_id'], $new_path);
// }
if ($this->category_id == $result['category_id']) {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '">' . $result['name'] . '</a>';
} else {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '">' . $result['name'] . '</a>';
}
$output .= $children;
$output .= '</li>';
}
if ($results) {
$output .= '</ul>';
}
return $output;
}
}
?>
I really hope someone knows a solution.
You do not need to customize controller. It has already Parent and child category listing, Just open the category module, and past below code.
<div class="box-category">
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<?php echo $category['name']; ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul>
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
<?php echo $child['name']; ?>
<?php } else { ?>
<?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>