I have this portion of code where categories are loaded from txt file.
<?php
foreach ($categories as $cat) {
if ($cat['word'] == false)
continue;
?>
<li>
<?php echo $cat['name']?>
</li>
<?php } ?>
The problem is that they are a lot because they are pulled from outside and populated dynamically in the file. Because of that I want to limit them to 15 but I'm unable to do it.
I've tried with for and while, like:
<?php
foreach ($categories as $cat) {
if ($cat['word'] == false)
continue;
for($cat['word'] = 0; $cat['word'] <=15; $cat['word']++){
?>
<li>
<?php echo $cat['name']?>
</li>
<?php }
}?>
And
<?php
foreach ($categories as $cat) {
if ($cat['word'] == false)
continue;
while($cat['word'] = 15;){
?>
<li>
<?php echo $cat['name']?>
</li>
<?php }
}?>
Both variants result in empty names.
Will the following work for you?
<?php $count = 0; ?>
<?php foreach ($categories as $cat) :?>
<?php if ($cat['word'] == false) continue; ?>
<li>
<?php echo $cat['name']?>
</li>
<?php if ($count >= 15) break; ?>
<?php $count++; ?>
<?php endforeach; ?>
Assuming you'd like to filter out the $cat['word'] == false entries before limiting the results to the first 15 (ie so you get up to 15 entries shown), try something like this...
$filtered = array_filter($categories, function($cat) {
return $cat['word']; // only include truthy "word" entries
});
foreach (array_slice($filtered, 0, 15) as $cat) : ?>
<li>
<?= $cat['name'] ?>
</li>
<?php endforeach ?>
<?php
$counter = 0;
foreach ($categories as $cat) {
if ($cat['word'] == false){
continue;
}
if($counter < 15) {
echo '<li>
<?php echo $cat['name']?>
</li>'
$counter++;
}} ?>
Can be refactored.
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>';
}
?>
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>
}
I'm attempting to add a for loop to a php menu that will add a tertiary level to a currently existing WP menu. Unfortunately, the only editable amount of content is that posted below, so please bear with me on this one.
I'm just wondering, before I attempt to implement this code, if anyone can tell me if it will actually work, of if you assume it'll break everything.
What I've done is to take the same semantic forloop that was written for the secondary (or 'sub_nav') menu items, and repeat it in attempts to dynamically generate third level (or 'tert_nav') menu items.
<div class="main-menu-sub-navigation">
<?php
$main_nav_items = wp_get_nav_menu_items(2);
$sub_nav = array();
foreach ($main_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$sub_nav[$parent_id][] = array('item' => $item);
}
}
$tert_nav = array();
foreach ($sub_nav_items as $item) {
if($item->menu_item_parent !== '0'){
$parent_id = $item->menu_item_parent;
$tert_nav[$parent_id][] = array('item' => $item);
}
}
?>
<div class="sub-nav-inner">
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav sub-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($tert_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><a href="<?php print $item['item']->url; ?>"><?php print $item['item']->title; ?>
<?php foreach ($sub_nav as $key => $value) { ?>
<ul class="nav tert-nav" data-parent="<?php print $key; ?>">
<?php foreach ($value as $item) { ?>
<?php if(isset($_GET['archive'])): ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->title == 'Archives') ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php else: ?>
<li class="menu-item-<?php print $item['item']->object_id; ?> <?php print ($item['item']->object_id == get_the_ID()) ? 'current-menu-item' : 'menu-item'; ?>"><?php print $item['item']->title; ?></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</a></li>
<?php endif; ?>
<?php } ?>
</ul>
<?php } ?>
</div>
Any help would be so very greatly appreciated. Thank you!!
How would I achieve this PHP task?
I have an unordered html list and and array.
My code adds list tags to each item in the array to create one big unordered list
<ul>
<?php foreach ($rows as $id => $row): ?>
<li><?php print $row ?></li>
<?php endforeach; ?>
</ul>
current output below
<ul>
<li>01</li>
<li>02</li>
<li>.....</li>
<li>.....</li>
<li>15</li>
</ul>
What I want is to split the list items so that they are in groups of 4 as sub unordered lists. If the number is not divisible by 4 then the remainder should be a smaller list at the end, for example.
<ul>
<ul>
<li>01</li>
<li>02</li>
<li>.....</li>
<li>.....</li>
</ul>
<ul>
<li>05</li>
<li>06</li>
<li>.....</li>
<li>.....</li>
</ul>
<ul>
<li>09</li>
<li>10</li>
</ul>
</ul>
Thanks in advance.
<ul>
<?php foreach ($rows as $id => $row): ?>
<?php if ($id > 0 && $id % 4 === 0): ?>
</ul><ul>
<?php endif ;?>
<li><?php echo $row; ?></li>
<?php endforeach; ?>
</ul>
(Note that if the key of your $rows array is not simply an index number, you'll need to maintain your own counter variable.)
<?php
$id=0;
foreach ($rows as $id => $row)
{
$id+=1;
if ($id % 4 == 0)
{
echo "<ul>";
}
echo "<li>$i</li>";
if ($id % 4 == 0)
{
echo "</ul>";
}
}
if ($id % 4 != 0)
{
echo "</ul>";
}
?>
foreach ($rows as $id => $row) {
if ($id % 4 == 0) {
// do something
}
}
Try something like this :
<?php
$rows = array("2","3","5","6","8","9","0","3","5");
$i =0;
foreach ($rows as $id => $row):
if($i % 4 == 0)
{ echo "</ul><ul>";}
$i++;
?>
<li><?php print $row ?></li>
<?php endforeach; ?>
</ul>
In the end this is the solution I went for. This solution includes a statement to close the tags if there are a number of items not divisible by four, for example if there are five items in total. Thanks to everyone, I couldn't have done it without your input.
$count;
foreach ($rows as $id => $row)
{
if ($count % 4 == 0)
{
echo "<ul>";
}
echo '<li>' . $row . '</li>';
if ($count % 4 == 3 || $count == count($rows)-1)
{
echo "</ul>";
}
$count++;
}
I'm trying to make a nested list with php by using a foreach loop but I'm kind of stuck now. My code uses a foreach loop and checks if the item is a heading, if it is it will start a nested list below it. The problem now is that if it's not a heading I want to put the corresponding list items into a single ul element below its heading. Now as you can see it puts every single list items that isn't a heading into a seperate ul element of its own because of the foreach loop. How can I fix this?
<ul>
<?php foreach($listitems as $listitem) : ?>
<?php if( $listitem['heading'] == 1) : ?>
<li><?php echo $listitem['listitem']; ?><!--begin nested list-->
<?php endif; ?>
<?php if( $listitem['heading'] == 0) : ?>
<ul><li><?php echo $listitem['listitem']; ?></li></ul>
<?php endif; ?>
<?php endforeach; ?>
</li><!--end nested list-->
</ul>
This is the desired html output:
<ul>
<li>Javascript Basics<!--begin nested list-->
<ul>
<li>Getting Started</li>
<li>Data and Variables</li>
<li>Functions</li>
<li>Scope</li>
<li>Working With Objects</li>
<li>Creating Objects</li>
<li>Arrays</li>
<li>Conditions And Decisions</li>
<li>Loops</li>
</ul>
</li><!--end nested list-->
</ul>
You need to set the inner lists start and end tags when the heading changes.
Something like this
<ul>
<?php
$NonHeadCount = 0;
$HeadingOpen = false;
foreach($listitems as $listitem)
{
if( $listitem['heading'] == 1)
{
$HeadingOpen = true;
if ($NonHeadCount != 0)
{
echo "</ul>";
}
echo "<li>".$listitem['listitem'];
$NonHeadCount = 0;
}
if( $listitem['heading'] == 0)
{
if ($NonHeadCount == 0)
{
echo "<ul>";
}
echo "<li>".$listitem['listitem']."</li>";
}
}
if ($NonHeadCount != 0)
{
echo "</ul>";
}
if ($HeadingOpen)
{
echo "</li>";
}
?>
</ul>
You have to change the $listitems format.
Some like this:
<?php
$listitems = array(
'item1' => array(
'item11',
'item12',
'item13'
),
'item2' => array(
'item21',
'item22',
),
'item3',
'item4'
);
?>
Then, do this:
<?php
echo "<ul>";
foreach($listitems as $item => $listitem):
echo "<li>$item</li>";
if(count($listitem) > 0):
echo "<ul>";
foreach($lisitem as $item):
echo "<li>$item</li>";
endforeach;
echo "</ul>";
endif;
endforeach;
echo "<ul>";
?>