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++;
} ?>
Related
How could I limit this foraech statement to just 5 loops? I think I should just use Break but I'm not sure where to put it.
<?php if(!empty($locations)): foreach($locations as $location): ?>
<?php if(empty($location["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>
You can use array_slice() first to get a new array with no more than 5 elements.
$locations = array_slice($locations, 0, 5);
Then everything unchanged.
There are three methods:
Method 1: foreach with a counter var
$counter = 1;
foreach($locations as $location) {
// use $location here
if($counter++ == 5) {
break;
}
}
Method 2: foreach using $key=>$val
foreach($locations as $key=>$val) {
// Your content goes here
if($key === 4) {
break;
}
}
Method 3: for loop
for($i = 0; $i < 5; $i++) {
// Use $locations[$i] here and do something with it
}
Using a counter variable into your loop you can control/limit to any number.
Example:
$counter = 0;
foreach($locations as $location):
if($counter++ == 5):
break;
// Your other content goes here
endif;
endforeach;
Add a variable ... increment it each iteration ... after reach 5 just break loop.
<?php $i = 1;?>
<?php if(!empty($locations)): foreach($locations as $location): ?>
<?php if(empty($location["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
<?php if ($i++ == 5) break; ?>
<?php endforeach; ?>
You can use for():
<?php if(!empty($locations)):
for($i=0; $i<5; $i++) {
$location = $locations[$i];
<?php if(empty($locations["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
}
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 have a list of < li >'s but without classes, I want to make for EACH < li > a different class.. something like this
< li class="1" >
< li class="2" >
< li class="3" >
Here is the code that I have:
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li>
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
You can try:
<?php
$i = 1;
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo "className$i"; ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
$i++;
} ?>
Or, instead, you could use the $key from your foreach instead of creating, and incrementing, a counter variable.
So this is how you would do it using the key from $lists. Note I have added a prefix of li- as classes cannot start with a digit. See: Which characters are valid in CSS class names/selectors?
<ul id="tralila">
<?php
foreach($lists as $key=>$region)
{
?>
<li class="<?php echo 'li-' . htmlentities($key); ?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
As all of the classes are unique I would use IDs instead of a class though.
Something like this?:
<ul id="tralila">
<?php
$class_name = 0;
foreach($lists as $key=>$region)
{
?>
<li class="<?=++$class_name;?>">
<?php $regionLink = "index.php?option=$option&Itemid=$listitemid&task=regions.region&rid=$region->id";
echo ''.$region->title.'';?>
</li>
<?php
$location = $key+1;
} ?>
</ul>
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>