How to show 3 li in single div inside foreach loop? - php

I have data collection and getting detail by foreach loop, which is working fine.
But I want 3 in a single inside foreach loop.
For example:
<ol>
<div>
<li></li>
<li></li>
<li></li>
</div>
<div>
<li></li>
<li></li>
<li></li>
</div>
<div>
<li></li>
<li></li>
<li></li>
</div>
</ol>
I am using below code for displaying the data:
<ol class="product-items widget-viewed-grid">
<?php $iterator = 1; ?>
<div id="mp-list-items-<?php echo $block->getSliderId(); ?>" class="owl-carousel owl-theme">
<?php foreach ($items as $_item): ?>
<?php echo ($iterator++ == 1) ? '<li class="product-item product-slider"">' : '</li><li class="product-item product-slider"">' ?>
<div class="slider-product-item-info">
<div class="product details slider-product-item-details">
<strong class="product name product-item-name">
<a title="<?php echo $block->escapeHtml($_item->getName()) ?>" href="<?php echo $block->getProductUrl($_item) ?>">
<?php echo $block->escapeHtml($_item->getName()) ?>
</a>
</strong>
</div>
</div>
<?php echo ($iterator == count($items) + 1) ? '</li>' : '' ?>
<?php endforeach ?>
</div>
</ol>

echo "<ol>";
for( $i=0; $i<3; $i++ ) {
echo "<div>";
for($j=0; $j<3; $j++) {
echo "<li>";
echo "</li>";
}
echo "</div>";
}
echo "</ol>";

You have to use nested loops. Outer loop for product <div> and inner loop for product items <li>.
For Example
foreach($products as $product){
echo "<div class='product'>";
//Now you can call sql query to get items by product id
foreach($items as $item){
echo "<li class='list-item'>" . $item->name . "</li>"; //assuming $item has name property
} //end of inner loop
echo "<div class='product'>";
} //end of outer loop

Related

Category Dropdown Filter

Is this possible to put this in dropdown filter? I'm using Wordpress Theme.
<div class="container mb-half">
<ul class="nav nav-<?php echo $filter;?> nav-<?php echo $filter_align ;?> nav-<?php echo $filter_nav;?> nav-uppercase filter-nav">
<li class="active"><?php echo __('All','custom_theme'); ?></li>
<?php
$tax_terms = get_terms('featured_item_category');
foreach ($tax_terms as $key => $value) {
?><li><?php echo $value->name; ?></li><?php
}
?>
</ul>
</div>

how to foreach array inside array

How I can foreach array properly?
Code:
while ($row = mysqli_fetch_assoc($result))
{
if ($use_sef_links == true)
{
$sef_link = "{$row['title']} ({$row['home_team_name']} vs {$row['away_team_name']}) {$row['dt']}";
$sef_link = str_replace("-", " ", $sef_link);
$sef_link = preg_replace('#\s{1,}#', " ", $sef_link);
$sef_link = str_replace(" ", "-", $sef_link);
$sef_link = preg_replace('#[^a-zA-Z0-9-]#', "", $sef_link);
$sef_link .= '_m'. $row['id'] . '.html';
$row['link'] = $sef_link;
}
else
{
$row['link'] = 'match.php?id=' . $row['id'];
}
$matches[$row['season_name']][$row['game_day']][] = $row;
}
foreach ($matches as $match) { ?>
<section class="kode-pagesection margin-bottom-40" style="padding-top: 120px;">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="kode-section-title"> <h2>Coming <?php echo $match['season_name']; ?> season game - <?php echo $match['game_day']; ?> gameday</h2> </div>
<div class="kode-fixer-list">
<ul class="table-head thbg-color">
<li> <h5>Coming games</h5></li>
<li> <h5>Game starts</h5> </li>
<li> <h5>Stadium</h5> </li>
<li> </li>
</ul>
<ul class="table-body">
<li>
<img src="<?php echo $match['home_team_logo']; ?>" class="circle-icon" style="height:50px; width:50px;" alt="<?php echo $match['home_team_name']; ?>"> <?php echo $match['home_team_name']; ?>
<span>VS</span>
<img src="<?php echo $match['away_team_logo']; ?>" class="circle-icon" style="height:50px; width:50px;" alt="<?php echo $match['away_team_name']; ?>"> <?php echo $match['away_team_name']; ?>
</li>
<li><small><?php echo $match['dt']; ?></small></li>
<li><small><?php echo $match['stadium']; ?></small></li>
<li class="fixer-btn">
<?php echo $label_array[103]; ?>
</li>
</ul>
</div>
</div>
</div>
</div>
</section>
<?php } ?>
If I just print_r($matches); then I can see all seasons and games..
But I cant foreach matches as match..
I put image here to show what it shows if I print matches string.
Image:
https://gyazo.com/ad2ebed217e0507cf315103b52273225
I'd have written this as a comment, but there's not enough space....
Given the structure of your array $matches['season_name']['game_day'][]
I guess you want:
foreach($matches as $seasonName => $season) {
// print the season-header
echo $seasonName;
foreach($season as $gamedayID => $gameday) {
// print the gameday header
echo $gamedayID;
foreach($gameday as $match) {
// print the details from the match
echo $match['title'];
}
}
}
This is the version for grouping by season.
See my comments for explanations!
Too many unnecessary nesting though!
Btw you just need to use foreach (foreach array_expression as $key => $value) and array selector properly. For the inner arrays there'll be no advantage via using foreach, since you've got only one index array_expression as :
(foreach $matches as $matchIndex => $matchDetails){
$currentMatch = $matchDetails[0];
print_r($currentMatch);
// or whatever you want to do with that
}

How to display magento product categories in column wise

I'm trying to display all the categories below the main title, so I want to display four categories in each column. The below is my code
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul class="pull-left">
<?php foreach ($collection as $cat):?>
<li>
<a style="color:#fff;" href="<?php echo $helper->getCategoryUrl($cat);?>">
<cite><?php echo $cat->getName();?><?php
?></cite>
</a>
</li>
<?php endforeach;?>
</ul>
HTML Output
<ul class="pull-left">
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
.
.
.
.
</ul>
I want to define four li items in every ul. Like this
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
I also want to display product count and I've tried this
<?php
$products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
?>
but it is not working.
I am using this code on my category page and it is giving me count.
Please have a look at the code I have added before tag.
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo ''.$cat->getName().'</br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()); echo"<br>";
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($subCategoryId)->getProductCollection()->getSize();
echo'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
Please use this code. This will surely work for you.
<?php
$category = Mage::getModel('catalog/category')->load($categoryID);
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($category)->getProductCollection()->getSize();
?>
Thanks,
Please use this one.
You can adjust ul ,li as per wish. Right now this will list four subcategories in a single row.
This code will give you the subcategories of a parent category.
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo ''.$cat->getName().'</br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()).'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
</ul>

For loop, runs once?

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 } ?>

How to put dynamically-generated n-amount of <div>s in <li>

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";
}
?>

Categories