Instead of writing:
<ul class="tabs">
<li>1-50</li>
<li>51-100</li>
<li>101-150</li>
<li>151-200</li>
<li>201-250</li>
<li>251-300</li>
<li>300-350</li>
<li>351-400</li>
<li>401-500</li>
</ul>
until 950-1000 which will be tab 20 - is there a way to using a PHP/JavaScript for loop to create more compact code?
I think this should do it for you:
<ul class="tabs">
<?php
$end_at = 1000;
$group_by = 50;
for($i=0;$i<$end_at/$group_by;$i++) {
echo '<li>', $i * $group_by + 1, '-', ($i+1) * $group_by, "</li>\n";
}
?>
</ul>
Example output
Or this:
<ul class="tabs">
<?php for($i=1;$i<=1000;$i++): ?>
<?php if($i % 50 == 0): ?>
<li><?php echo $i-49 ?>-<?php echo $i; ?></li>
<?php endif; ?>
<?php endfor; ?>
</ul>
Related
we are working on frontend design, we want to display our div col33 for 3 times, we are fetching records from mysql and then we want to count total records, for example if records are 60, we want to display 20 records in div col33 then div col33 display 20 records and so on.
<div class="col33">
<ul>
<?php foreach ($lists as $list): ?>
<li><?php echo ucfirst($list->page_name); ?></li>
<?php endforeach; ?>
</ul>
</div><!-- ending of col33 -->
I'm not sure but I hope You want something like this:
<?php
$lists = array("1","2","3","4","5","6","7","8","9","10","11");
$rows = ceil(count($lists) / 3);
$rowsConst = $rows;
$rows = 0; // for first div.
$first = true;
foreach ($lists as $list):
if ($rows == 0):
$rows = $rowsConst;
if (!$first) {
echo "</div>\n";
} else {
$first = false;
}
?>
<div class="col33">
<ul>
<li><?php echo $list; ?></li>
</ul>
<? else: ?>
<ul>
<li><?php echo $list; ?></li>
</ul>
<?
endif;
$rows--;
endforeach;
?>
</div>
?>
WORKING CODE
Hey i think you want something like if you have 60 records and you want 20 records to distribute each of them in the div col33 and that should repeat 3 times. if i am right then i hope here is your solution. otherwise kindly elaborate more. cheers
<div class="col33">
<ul>
<?php $j=1;
foreach ($lists as $list):
if($j%20==0){
?>
<li><?php echo ucfirst($list->page_name); ?></li>
<?php }else{
<li><?php echo ucfirst($list->page_name); ?></li>
}
$j++; ?>
<?php endforeach; ?>
</ul>
</div>
I'm using OpenCart and I'm trying to achieve rendering out the meta_description (used to identify the comic publisher) and use it to make a drop-down list with sub-categories, or to give the illusion of it. Here is my code now, it's an adopted version of the current OpenCart code. ['class'] is how I grab the child categories meta_description.
Basically, the second for statement doesn't work - it only does the first one. I would appreciated any kind of support on this.
<div class="menu">
<div id="top"></div>
<span>
<ul id="nav">
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<div class="subs">
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<h3>DC Comics</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "DC Comics"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
<h3>Marvel</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "Marvel"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</span>
</div>
Use different variables in loops, in you code $i is used in main loop and incremented in inner loops you can use foreach loop like ,
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if (is_array($category['children']) and isset($category['children'])) { ?>
<div class="subs">
<div>
<ul>
<?php
$li1='<li><h3>DC Comics</h3><ul>';
$li2='<li><h3>Marvel</h3><ul>';
foreach($category['children'] as $child)
{
if($child['class'] == "DC Comics")
{
$li1.='<li>'.$child['name'].'</li>';
}
if($child['class'] == "Marvel")
{
$li2.='<li>'.$child['name'].'</li>';
}
}
$li1.='</ul></li>';
$li2.='</ul></li>';
echo $li1;
echo $li2;
?>
</ul>
</div>
</div>
<?php } ?>
</li>
<?php } ?>
I extracted the values of the array on the foreach loop, but I want when it retrieves a certain 'br_title' changed href link to <a href="http://noorresults.moe.sa..how to do that with simple code?..
<ul class="riMenu">
<?php foreach ($last_branches as $last) { ?>
<li> <?php if (!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }; ?>
</ul>
</div>
</div><!-- Menu E
You can change your code as below
<?php
$total_to_show = 5; // take offset
$count = 1; //counter until it reach to offset
foreach ($last_branches as $last) { if($count > $total_to_show){?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<? }else{?>
<li> <?php if(!empty($last['br_title'])) echo $last['br_title'] ?></li>
<?php }
$count++;
} ?>
Hello :) I realy need your help here. I dynamically generate list of items but instead of putting each item in separate <li> I want to get something like this:
<ul>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
</ul>
Here is code I have:
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i++%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
</li>
<?php endforeach; ?>
</ul>
But as the result I get structure like this:
<ul>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
</ul>
Thank you for help
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
<?php if ($i%$itemCount==2): ?>
</li>
<?php endif; $i++; ?>
<?php endforeach; ?>
</ul>
You can try this.
<ul class="some-ul-class">
<?php $itemCount = 3;
$i=0;
foreach ($p->getItems() as $pt):
if ($i%$itemCount==0):
echo '<li class="item">';
endif;
echo "<div>$pt</div>";
if ($i%$itemCount==2):
echo '</li>';
endif; $i++;
endforeach; ?>
</ul>
Try something like this:
<ul class="some-ul-class">
<?php $itemCount = 4; ?>
<li>
<?php $i = 1; foreach ($p->getItems() as $pt): ?>
<?php if ( $i % $itemCount == 0): ?>
</li><li>
<?php endif; ?>
<?php $i++; ?>
<div><?php echo $pt; ?></div>
<?php endforeach; ?>
</li>
</ul>
This generates:
<ul class="some-ul-class">
<li>
<div>1</div>
<div>2</div>
<div>3</div>
</li><li>
<div>4</div>
<div>5</div>
<div>6</div>
</li>
</ul>
Demo
Your code will not achieve nested DIVS in LI because you need a multidimensional array to nest the items within the container. The solution is to break the initial DB result set in to chunks with array chunk.
This just splits your array (1,2,3,4,5,6) to ([0] => array(1,2,3), [2] => array(4,5,6)
Run through the loop below you will get two LI with 3 nested DIV. The code is not tested but should be something like operational.
<?php
$items = array(1,2,3,4,5,6,8,9,10,11,12,13,14,15);
// Your initial item array
$rows = 3;
// Number of rows in each li
$items = array_chunk($items, $rows);
// Final nested array in blocks of 3
if ($items) {
echo "<ul class='some-ul-class'>\n";
foreach ( $items as $item ) {
echo "<li class='items'>\n";
foreach ($item as $divs) {
echo "<div>{$divs}</div>\n";
}
echo "</li>\n";
}
echo "</ul>\n";
}
?>
I am trying to give an alternate class to each LI foreach.
i.e.
<li class="odd">
text
</li>
<li class="even">
text
</li>
<li class="odd">
text
</li>
<li class="even">
text
</li>
This is my code:
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Please can anyone help...
Thanks
try :
<?php $count = 0; // need to set first value
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo (++$count % 2) ? "odd" : "even"; ?> type <?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
Link to pre/post increment docs if needed
I would do it that way .. it's key independent and you better see what the li-class will look like.
<ul>
<?php
foreach ($this->item->extra_fields as $key=>$extraField) {
if($extraField->value) {
$toggle = ($toggle=="odd"?"even":"odd");
echo '<li class="',$toggle,' type',ucfirst($extraField->type),' group', $extraField->group,'">';
echo 'Text here';
echo '</li>';
}
}
?>
</ul>
jQuery can easily do this if that's an option for you. It's :odd and :even selectors should do just what you're looking for without too much hassle.
I know this is old, but I humbly present a cleaner solution:
<ul>
<?php
$odd = true;
foreach ( $this->item->extra_fields as $key => $extraField ) {
// Output template.
$template = '<li class="%1$s">%2$s</li>';
// Create our classes.
$classes = [
( $odd ) ? 'odd' : 'even',
'type' . ucfirst($extraField->type),
'group' . $extraField->group
];
// Create our class string.
$class_string = implode( ' ', $classes );
// Print our content out.
printf( $template, $class_string, 'Text here' );
// Flip the classname for next time.
$odd = ! $odd;
}
?>
</ul>
It could of course be cleaner still, by using fewer variables. This is just my style.