foreach array_slice not working - php

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>';}
?>`

Related

foreach display only first 3 items

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 ;)
}
}

Modifying the first item of the array

I have to modify the first item of the array, however I failed, but I manage to do a code:
<div class="carousel-inner">
<?php if($results) {
$x= 1;
foreach ($results as $data) { $x++; ?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php }
} ?>
</div>
Assuming that I will get these result:
<div class="item active">
<img alt="" src="http://localhost/ideal_visa/uploads/fd5fa6cfbe64c8b68664ddbf0546d81b.jpg">
</div>
<div class="item">
<img alt="" src="http://localhost/ideal_visa/uploads/c43c064de5cb9e751c723eb9791f2107.jpg">
</div>
<div class="item">
<img alt="" src="http://localhost/ideal_visa/uploads/e3137475f6330d52e9cd9c6fc5efba1e.jpg">
</div>
I only have to add active on the first item of the array.
You don't need to do extra $x for this.You can use foreach with key => value and check if it's first index
<?php if($results) {
foreach ($results as $key=>$data) { ?>
<div class="item <?php echo ($key == 0)? 'active':''; ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php }
} ?>
Problem
You have put $x=1 before the foreach, then increment it at the start.
That means when you show the first image, $x==2.
Solution
You can either start with $x=0 or move the line $x++ to the end of the loop:
foreach ($results as $data) { ?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php $x++; }
You need to increment $x after the iteration
<?php
if($results) {
$x= 1;
foreach ($results as $data) {?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php
$x++;
}
} ?>
<div class="carousel-inner">
<?php if($results) {
$x= 1;
foreach ($results as $data) { ?>
<div class="item <?php if($x == 1) { echo 'active';} ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php $x++; }
} ?>
</div>
you need to increment the x value after the iteration
OR
<div class="carousel-inner">
<?php if($results) {
foreach ($results as $x=>$data) { ?>
<div class="item <?php if($x==0) echo 'active'; ?>">
<img alt="" src="<?=base_url('uploads/'.$data->file)?>">
</div>
<?php }
} ?>
</div>
you can just utilize associative key of the array $data and change the condition to $x==0

(count($list) for 1 or 2 items

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>

Looping an image inside a div and loop the div after it will contain 4 images

currently i have this structure
<div class="col">
img1
img2
img3
img4
</div>
<div class="col">
img1
img2
img3
img4
</div>
then when i tried to loop it
i got this results
img[0]
img[1]
img[2]
how can i achieve this structure
<div class ="col"[0]>
img[0]
img[1]
img[2]
img[3]
</div>
<div class ="col"[1]>
img[0]
img[1]
img[2]
img[3]
</div>
Loop code:
<?php
foreach ($images as $k => $v){
$imagesrc ='graph.facebook.com/'.$v['fbid'].'/picture';;
?>
<div class="col" >
<img [<?php echo $k; ?>] src="<?php echo $imagesrc; ?>" width="29px" height="31px" alt="name">
</div>
<?php
}
?>
In the loop you want to add a <br> or <p> after each image.
Example inside loop
$counter = 0;
$firstTime = true;
$column = 0;
<?php foreach ($images as $k => $v):
$imagesrc ='graph.facebook.com/'.$v['fbid'].'/picture';
if ($firstTime) {
echo '<div class="col'.$column.'" >';
$firstTime = false;
}
if ($counter > 3) {
echo '</div>';
$column++;
echo '<div class="col'.$column.'" >';
$counter = 0;
} ?>
<img src="<?php echo $imagesrc; ?>" width="29px" height="31px" alt="name">
<?php $counter++; ?>
<?php endforeach; ?>
</div>

Get Magento Categories with Especific Sort of Admin Panel

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();

Categories