The part where the script tells that if there are 1 or 2 script posts double. I only want ( if there are 1 or 2 articles) it only shows 1 or 2 articles
<?php
defined('_JEXEC') or die;
$count = 0;
if (count($list)%3!=0) {
$list = array_merge($list, array_slice($list, 0, 3-count($list)%3));
}
if (count($list)%2!=0) {
$list = array_merge($list, array_slice($list, 0, 2-count($list)%2));
}
if (count($list)%1!=0) {
$list = array_merge($list, array_slice($list, 0, 1-count($list)%1));
}
?>
<?php JLoader::register('fieldattach', 'components/com_fieldsattach/helpers/fieldattach.php'); ?>
<div id="slides">
<div class="slides_container">
<div class='slide'>
<?php foreach ($list as $item) :
if (($count>0) and ($count%3==0)): ?>
</div>
<div class="slide">
<?php endif; ?>
<li class="categories">
<a href="<?php echo $item->link; ?>">
<?php echo fieldattach::getImg($item->id, 6); ?>
<h4><?php echo $item->title; ?></h4>
</a>
</li>
<?php $count++; endforeach; ?>
</div>
</div>
</div>
Related
We use a foreach code and we want to display only the first 3 items.
But for some reason our code does not work, it currently still display all items.
What am I missing here?
CODE:
<?php $items = $_order->getAllItems(); $i = 0; foreach($items as $i): if($i < 3) {?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $i++; } endforeach;?>
You need to use different variable inside foreach():-
<?php
$items = $_order->getAllItems();
$i = 0;
foreach($items as $itm):
if($i >= 3) {break;}else{?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($itm->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $i++; } endforeach;?>
A much better solution using array_slice():-
<?php
$items = $_order->getAllItems();
$item = array_slice($items, 0, 3); // get first three only
foreach($item as $itm):
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($itm->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php endforeach;?>
Sorry, read the question wrong. Here's the updated answer.
Your foreach iterater was same as the count variable $i
<?php
$items = $_order->getAllItems();
$i = 0;
foreach($items as $item) {
?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php
$i++;
if($i == 3) {
break; // because we don't want to continue the loop
}
}
?>
Use for seems like more pretty than foreach:
<?php $items = $_order->getAllItems();
for ($i = 0; $i < count($items) && $i < 3; $i++): ?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($items[$i]->getProductId())->getSmallImageUrl(); ?>"
border="0"/></div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($items[$i]->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php endfor; ?>
if you want to show only 3 items then you should break out of foreach:
if($counter >= 3) break;
else { //rest of the code ...
}
or simply use a for loop instead.
you are resetting your counter $i for every iteration in the loop, use another variable $counter
<?php $items = $_order->getAllItems(); $counter = 0; foreach($items as $i): if($counter < 3) {?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /></div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $counter++; } endforeach;?>
$count='1';
for ( $i = 0; $i <= 100; $i++ ) {
if ( $var[$i] == "value" ) {
print $i.'-'.$var[$i] . "<br>"; // print $i to display row number
if ( $count++ >= 3 ) {
break;
}else{
// have a cup of coffee ;)
}
}
I try to display only the first 3 items of a foreach, but for some reason my code does not seem to work.
It works fine with the default code: <?php foreach ($rma->getItemCollection() as $item):?>
What am I missing?
CODE:
<?php $items = $rma->getItemCollection();
$item = array_slice($items, 0, 3);
foreach($item as $itm): ?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $this->helper('catalog/image')->init($itm->getProduct(), 'thumbnail')->resize(85) ?>" border="0" />
</div>
<div class="order-row-product-name">
<?php echo substr(Mage::helper('rma')->getOrderItemLabel($itm), 0, 30) ?>
</div>
</div>
</li>
<?php endforeach;?>
In case of result of $rma->getItemCollection(); is not array, but some object, which implements Traversable interface, you can use a counter:
<?php
$items = $rma->getItemCollection();
$counter = 0;
foreach($items as $item): ?>
<li>...</li>
<?php
$counter++;
if ($counter == 3) {
break;
}
endforeach;
Other way can be specifying a limit for query which is done under the hood in getItemCollection().
try this code
<?php
$items = $rma->getItemCollection();
$item = array_slice($items, 0, 3);
foreach($item as $itm){
echo'
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="'.
$this->helper('catalog/image')->init($itm->getProduct(),
'thumbnail')->resize(85).' border="0" />
</div>
<div class="order-row-product-name">'.
substr(Mage::helper('rma')->getOrderItemLabel($itm), 0, 30).'
</div>
</div>
</li>';}
?>`
This is the loop I want to limit this to 2 outputs and create another loop for further outputs please help.. I am a beginner
Enter code here
<?php if ($informations) { ?>
<div class="column">
<h3>
<?php echo $text_information; ?>
</h3>
<ul>
<?php foreach ($informations as $information){ ?>
<li><a href="<?php echo $information['href']; ?>">
<?php echo $information['title']; ?>
</a></li>
<?php } ?>
</ul>
</div>
<?php } ?>
Try this. .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i==2)
{
break;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
For other values . .
<?php if ($informations) { ?>
<div class="column">
<h3><?php echo $text_information; ?></h3>
<ul>
<?php
$i=1;
foreach ($informations as $information){
if($i<2)
{
continue;
}
?>
<li><?php echo $information['title']; ?></li>
<?php
$i++;
} ?>
</ul>
</div>
<?php } ?>
Bit vague but I guess you are looking for array_chunk,
$new_array = array_chunk($informations, 2, true);
I am trying to exclude one specific category from a random menu I have. I have searched for answers and have found some potential solutions on here, but none of them worked.
This is what I currently have:
<?php
$catID = $this->category_id;
$catName = $this->category_name;
$catType = $this->category_type;
$category = Mage::getModel('catalog/category')->load($catID);
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail', 'description'))
->addAttributeToFilter('is_active', 1)
->addIdFilter( $category->getChildren() );
$catIdEx = 38;
$categories->getSelect()->join(array('cats' => 'catalog_category_product'), 'cats.product_id = e.entity_id');
$categories->getSelect()->where('cats.category_id', array('neq' => $catIdEx));
$categories->getSelect()->group(array('e.entity_id'));
$categories->getSelect()->order('RAND()');
$categories->getSelect()->limit(1);
?>
<?php foreach ($categories as $category): ?>
<div>
<a href="<?php echo $category->getUrl(); ?>">
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" class="boxedimage" />
</a>
<br />
<h3>Spotlight <?php echo $catName ?></h3>
<?php
$sdesc = $category->getDescription();
$sdesc = trim($sdesc);
$limit = 230;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
}
?>
<?php echo $sdesc."..."; ?>
<div>View <?php echo $category->getName(); ?> <?php echo $catType ?>...</div>
</div>
<?php endforeach; ?>
Your collection call looks pretty over the top for reasons im sure make sense for you, so I'll go with this - if you just want to exclude that category, add this to the foreach;
<?php foreach ($categories as $category): ?>
<?php if($category->getId() !== $catIdEx) { ?>
<div>
<!-- your other code here -->
</div>
<?php } ?>
<?php endforeach; ?>
$excluded_category = array('20','4','6');
<?php foreach ($categories as $category): ?>
<?php if(!in_array($category->getId(),$excluded_category)) { ?>
<div>
<!-- your other code here -->
</div>
<?php } ?>
<?php endforeach; ?>
I wrote the code below to put together a customized menu of categories. Everything works fine, but would like the order of the categories were the same order as defined in the administrator panel where there drap and drop functionality.
<?php
$subCats = Mage::getModel('catalog/category')->load(76)->getChildren();
$dispositosCatIds = explode(',',$subCats);
?>
<ul class="menu">
<?php $controleNum = 0; ?>
<?php foreach($dispositosCatIds as $dispositoCatId): ?>
<?php $aparelhoCat = Mage::getModel('catalog/category')->load($dispositoCatId); ?>
<?php if($aparelhoCat->getIsActive()): ?>
<li class="<?php print $controleNum ? '' : 'submenu first'; ?>"><a class="drop" href="<?php echo $aparelhoCat->getUrl(); ?>"> <span><?php echo $aparelhoCat->getName(); ?></span></a> <!--Begin 6 column Item -->
<div class="dropdown_6columns">
<div class="inner"><span class="title"><?php echo $aparelhoCat->getName(); ?></span>
<div class="col_2">
<div class="col_2 firstcolumn"><img src="<?php echo $aparelhoCat->getImageUrl(); ?>" alt="<?php echo $aparelhoCat->getName(); ?>" /></div>
</div>
<div class="col_4" style="margin-bottom: 20px;">
<?php echo $aparelhoCat->getDescription(); ?>
</div>
<div class="col_2 categorias-super"><span class="title_col">Produtos para <?php echo $aparelhoCat->getName(); ?></span>
<?php $subSubCats = $aparelhoCat->getChildrenCategories();?>
<?php if (count($subSubCats) > 0): ?>
<?php //$controleNumLI = 0; ?>
<ul style="list-style: none; float: none !important;">
<?php foreach($subSubCats as $_subcategory): //Rodando entre as categorias de Um dispositivo ?>
<?php if($_subcategory->getIsActive()): ?>
<li class="level1 <?php //print $controleNumLI ? '' : 'first'; ?>"> <?php echo $_subcategory->getName(); ?></li>
<?php //$controleNumLI += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php $controleNum += 1; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I tried to use other modes (based here) can do this, but I could not. The problem that the function returns getChildren() is a string with IDs in ascending order.
Some Ideas?
This is the code I use to display category in a dropdown box in the order of the admin... the key is setOrder('path','ASC')
$categories = array();
$_categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('path','ASC')
->addAttributeToSelect('name')->load();
foreach($_categories as $cat){
$level = $cat->getLevel() - 1;
$pad = str_repeat("----", ($level > 0) ? $level : 0);
$categories[] = array('value' => $cat->getEntityId(), 'label' => $pad . ' ' . $cat->getName());
}
print_r($categories);
You could do something like this: create array tree from array list
I got it:
$dispositovosCategoryId = 76;
$dispositovosCategoryIds = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$dispositovosCategoryId))
->addAttributeToFilter('is_active',array('eq'=>true))
->addAttributeToSelect('level')
->setOrder('position','ASC')
->addAttributeToSelect('name','url','image')
->load();