I have a list of returned subcategories. I only have access to the template display and not to the MySQL query so no, I can't limit the query.
Right now all the results are returned. I would like to limit the list to 5, and then add a "more" link if there are more than 5 results.
I know I did this wrong because I don't think count is actually tied to the foreach:
<ul class="sub-categories">
<?php
while (count($category->getChildren()) <= 5) { // line I added for while loop
foreach ($category->getChildren() as $child) {
if (!$child->totalItemCount())
continue;
$link = $this->app->route->category($child);
$item_count = ($this->params->get('template.show_sub_categories_item_count')) ? ' <span>('.$child->totalItemCount().')</span>' : '';
echo '<li>'.$child->name.''.$item_count.'</li>';
}
} // line I added for while loop
?>
</ul>
Keep track within the foreach and break; when you've reached the limit:
<ul class="sub-categories">
<?php
$i = 0;
foreach ($category->getChildren() as $child) {
if (!$child->totalItemCount()) continue;
$i++; if($i>5) break;
$link = $this->app->route->category($child);
$item_count = ($this->params->get('template.show_sub_categories_item_count')) ? ' <span>('.$child->totalItemCount().')</span>' : '';
echo '<li>'.$child->name.''.$item_count.'</li>';
}
?>
</ul>
Related
I am trying to display the below table value in list and sub-list.
and here is my for loop to display
$sql ="SELECT *FROM objectives";
$result = $conn->query($sql);
$categories = array();
foreach ($result as $result) {
$category = $result['content'];
$categories[$category][] = $result['sub_content'];
}
?>
<ul>
<?php foreach ($categories as $category => $subcategories): ?>
<li>
<?php echo $category; ?>
<ul>
<?php foreach ($subcategories as $subcategory):?>
<li><?php echo $subcategory; ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
The data is displayed in list and with sub list. I don't want to display the 0 value in the sub-list.
Everything is fine except the display of 0 in sub-list. Please advise.
try this if you just don't want to display 0
<?php echo ($subcategory != '0')? '<li>'.$test.'</li>' : ''; ?>
and if you don't want to store in array then put this if condition
foreach ($result as $result) {
$category = $result['content'];
if($result['sub_content'] != '0'){
$categories[$category][] = $result['sub_content'];
}
}
Simply implementing echo ($subcategory != '0')? '<li>'.$test.'</li>' : ''; will result in needless markup in the dom. Specifically, you will have empty <ul></ul> tags as nested lists where only a single row containing $subcategory is 0. (Demonstration) These extra bits of markup may cause funky side-effects when css/styling is applied.
Further refinements are advisable as a matter of best practice:
When querying the database, only SELECT the columns that you specifically require for your task.
Add stability to your process by using an ORDER BY clause that will group the rows by content and possibly sort sub_content
Never use more loops than necessary. This task can be (and therefore, theoretically, should be) performed in a single loop.
Recommended Code: (Demo)
$result = $conn->query("SELECT content, sub_content FROM objectives");
$category = null;
$output = '';
foreach ($result as $row) {
if ($category !== $row['content']) { // new parent
if ($category !== null) { // not first iteration
$output .= "<li>$category"; // print parent
if ($sublist) {
$output .= "<ul>$sublist</ul>"; // print all children
}
$output .= "</li>";
}
$category = $row['content']; // overwrite $category
$sublist = ''; // reset sublist
}
if ($row['sub_content'] !== '0'){ // filter row
$sublist .= "<li>{$row['sub_content']}</li>";
}
}
if ($result) { // in case the resultset is empty
echo "<ul>";
echo $output; // print stored markup
echo "<li>$category"; // print last parent
if ($sublist) {
echo "<ul>$sublist</ul>"; // print all children from last parent
}
echo "</li>";
echo "</ul>";
}
Source Code Output:
<ul>
<li>Demonstrate where to find the following documentation:
<ul>
<li>Operating and Safety Strategy</li>
</ul>
</li>
<li>Explain the different turbine main operating states:
<ul>
<li>Power Production</li>
<li>Idle</li>
<li>Stop</li>
</ul>
</li>
<li>Explain how to recognise the current operating mode on the display of the operating panel</li>
<li>Explain the subsystem operating modes:
<ul>
<li>Stop</li>
<li>Manual</li>
</ul>
</li>
<li>Explain the difference between local and remote point of operation</li>
<li>Explain that only one point of operation can be active at a time</li>
</ul>
Rendered Output: (courtesy of phptester.net)
Right now i am working around one PHP script based on http://simplehtmldom.sourceforge.net/.
Here is my code:
<?PHP
foreach ($html->find('li.tooltip') as $ul) {
$id = $ul->id;
$dt = "data-text";
$dt = "data-text";
$cid = "colvar-id";
$datatext = $ul->$dt;
$colvarid = $ul->$cid;
$countN = count($id);
$number = 1;
$N = $i++;
if ($N == "") {
$N = 0;
}
}
?>
<?PHP echo "Result is: $countN"; ?>
This code is suposed to count the number of founded occurencies, but it displays nothing.
All i want is to count the founded occurencies and simply display the number of occurencies.
Thanks in advance!
You can use count():
$html = str_get_html(<<<EOF
<ul>
<li>not a tooltip</li>
<li class="tooltip">tooltip</li>
<li class="tooltip">also a tooltip</li>
</ul>
EOF
);
echo count($html->find('li.tooltip'));
// 2
So i have a CMS which uses a foreach loop to generate the navigation which consists of individual list items inside a ul.
Basically what I want is to have my logo inserted in the center of these links, with an equal number of links on either side.
I've got my code to split up into two different columns of navigation with a gap, but I cant figure out where to place the logo div so it doesn't repeat more than once, current code also throws out some empty list items which I don't need.
<ul>
<?php $i = 0; foreach($items as $item): ?>
<li><a<?php ecco($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo html($item->title());?></a></li>
<?php if (++$i % 3 === 0 && $i !== count($items)) echo "</li><li>"; endforeach ?>
</ul>
Thanks.
How about something like this?
UPDATE
Since your count doesn't work, try this:
<ul>
<?php
$j = 0;
$breakPoint = 0;
foreach($items as $item) {
$j++;
$breakPoint = $j;
}
$i = 0;
foreach($items as $item) { ?>
if ($i === $breakPoint) {
/* insert Logo Code */
} else { ?>
<li><a<?php ecco($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo html($item->title());?></a></li>
<?php
}
$i++;
} ?>
</ul>
Haven't tested it. And it depends if you have a odd or even number of $item elements.
I have this code:
foreach($html->find('ul.results') as $article) {
$item['date'] = $article->find('span.result_date', 0)->plaintext;
$item['title'] = $article->find('a.result_title', 0);
$item['text'] = $article->find('span.result_text', 0)->plaintext;
$item['read'] = $article->find('a.read_more', 0);
$articles[] = $item;
}
foreach ($articles as &$item) {
while ($i < 5) {
echo $item['date']." ";
echo $item['title'].'</br>';
echo $item['text']." ";
echo $item['read'].'</br></br>';
$i++;
}
}
And I am trying to echo the results. Right now the second foreach isn't doing anything. It is just displaying five of the same articles. The articles are in the format of: date, title, text, read more. I am trying to eco the first five $articles, but I can't find a proper way to do so that isn't print_r.
The whole loop inside the for each will just loop on the first element 5 times, move to the next element and loops 5 times, etc.
Try something like this:
$i = 0;
foreach ($articles as $item){
echo items stuff...
$i++;
if ($i == 5) break;
}
I have looked and looked and tried to find an answer for what I've been looking for, but I have yet to see an answer for this:
I am trying to generate a Wordpress loop that takes all the posts from a single category and displays them three at a time inside <li></li> tags.
Output should look like this:
<li>My post title | Another Title | Third title</li>
<li>The next post title | A different post | Post #6</li>
<li>And so on | And so forth</li>
I need this to loop through all the entries in the category until finished, and then exit the loop.
My code is completely non-working at this point, but I've provided what I'm working with below. If anyone has any solution to this, I'd love to give mad props to you, as this has hounded me for three days without any solution so far.
<?php // Loop through posts three at a time
$recoffsetinit = '0';
$recoffset = '3';
query_posts('cat=1&showposts=0');
$post = get_posts('category=1&numberposts=3&offset='.$recoffsetinit.');
while (have_posts()) : the_post();
?>
<li>
<?php
$postslist = get_posts('cat=1&order=ASC&orderby=title');
foreach ($postslist as $post) : setup_postdata($post);
static $count = 0; if ($count == "3") { break; } else { ?>
<?php $count++; } ?>
<?php endforeach; ?>
<?php $recoffsetinit = $recoffset + $recoffsetinit; ?>
</li>
<?php endwhile; ?>
I hacked up your solution to make it work. It took a little doing, since my code-fu is not what you'd call "good." Here's the solution:
<ul>
<?php
query_posts('category=1&showposts=0');
$posts = get_posts('category_name=my_cat&order=ASC&orderby=title&numberposts=0');
$postsPerLine = 3;
$currentPostNumber = 0;
foreach ($posts as $post) :
if ($currentPostNumber == 0) {
echo '<li>';
}
?>
<?php
$currentPostNumber++;
if ($currentPostNumber >= $postsPerLine) {
$currentPostNumber = 0;
echo '</li>';
}
endforeach;
?>
</ul>
Thanks for the input!
No wordpress to test with, and no time, but something like this might be a better way of going about it?
<?php
$postList = get_posts('cat=1&order=ASC&orderby=title');
$postsPerLine = 3;
echo "<ul>";
echo buildPosts($postList, $postsPerLine);
echo "</ul>";
function buildPosts($list, $perLine) {
$out = '';
$currentPostNumber = 0;
foreach ($list as $post) {
if ($currentPostNumber == 0) {
$out .= '<li>';
}
$out .= "<a href='" . the_permalink() . "'></a> ";
$currentPostNumber++;
if ($currentPostNumber <= $perLine) {
$currentPostNumber = 0;
$out .= '</li>';
}
}
return $out;
}
?>
Just snag all the posts for a category, at once, then iterate over it. Create a link to every post, toss in the separator, and on every third post start a new <li>
<ul>
<?php
global $post;
$postsPerLine = 3;
$counter = 0;
$myposts = get_posts('category=1&orderby=title&order=ASC');
foreach($myposts as $post) :
echo (++$counter % postsPerLine) ? : '<li>';
?>
<?php the_title(); ?></li>
<?php
echo ($counter % postsPerLine) ? ' | ' : '</li>';
endforeach;
?>
</ul>