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 ;)
}
}
Related
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>';}
?>`
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
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>
I have my markup structure as below:
<div>
<div>value1</div>
<div>value2</div>
<div>value3</div>
<div>value4</div>
<div class="clear"></div>
</div>
<div>
<div>value5</div>
<div>value6</div>
<div>value7</div>
<div>value8</div>
<div class="clear"></div>
</div>
I have my data in a PHP result set, let's say I have 9 records so the structure should be as below:
<div>
<div>value1</div>
<div>value2</div>
<div>value3</div>
<div>value4</div>
<div class="clear"></div>
</div>
<div>
<div>value5</div>
<div>value6</div>
<div>value7</div>
<div>value8</div>
<div class="clear"></div>
</div>
<div>
<div>value9</div>
<div class="clear"></div>
</div>
So, the while loop should run in a way so that it will print the parent div after 4 records printed successfully. But in above I have 9 records so it should close the dive if its the last record.
Please help, Thanks!
The preconfig...
<?php
$num_of_results = sizeof($your_array);
$loops = ceil($num_of_results/4);
$k = 0;
?>
In your web
<?php for($p = 0; $p < $loops; $p++) { ?>
<div>
<div>
<?php for($i = 0; $i < 4 && $k < $num_of_results; $i++) { ?>
<div><?php echo $your_array[$k]; $k++;?></div>
<?php } ?>
<div class="clear"></div>
</div>
</div>
<?php } ?>
That's your problem isnt it?
By getting some idea from logic given here I tried following and it works.
<div> <!-- started main div -->
<?php
$icount = 1;
$itotal = mysql_num_rows($result_rs);
while ($rs = mysql_fetch_array($result_rs)) {
echo '<div>'.$rs['value'].'</div>';
if ($icount % 4 == 0 && $icount != $itotal){
echo '<div class="clear"></div>';
echo '</div>'; //closed main div
echo '<div>'; //started new main div
}
$icount++;
}
?>
</div> <!-- closed main div -->
That, solved my problem.
Edited: added itotal condition, so when you will have only 4 records per page then also this will work properly.
Right, now I know what you're after. I've done this before when showing items in a grid and you need to break each row because of that browser.
Anyway, it's ugly but I don't think it gets any easier than this
<?php for ($i = 0, $total = count($resultSet); $i < $total; $i += 4) : ?>
<div>
<?php
for ($j = $i; $j < ($i + 4); $j++) :
if (!isset($resultSet[$j])) :
?>
<div class="clear"></div>
</div>
<?php break 2; endif ?>
<div><?php echo htmlspecialchars($resultSet[$j]) ?></div>
<?php endfor ?>
<div class="clear"></div>
</div>
<?php endfor ?>
<div>
<?php for ($i = 1; $i <= 9; $i++): ?>
<?php if ($i%4 == 1 && $i != 1): ?>
<div class="clear"></div>
</div>
<div>
<?php endif; //$i%4 == 1 && $i != 1 ?>
<div>Value <?php echo $i ?></div>
<?php endfor; //$i = 1; $i <= 9; $i++ ?>
<div class="clear"></div>
</div>
or with an array:
<div>
<?php foreach ($arr as $k=>$v): ?>
<?php if (($k+1)%4 == 1 && $k != 0): ?>
<div class="clear"></div>
</div>
<div>
<?php endif; //($k+1)%4 == 1 && $k != 0 ?>
<div><?php echo $v ?></div>
<?php endforeach; //$arr as $k=>$v ?>
<div class="clear"></div>
</div>
or with a mysqli resultset:
<div>
<?php $count = 1 ?>
<?php while ($row = mysqli_fetch_array($result)): ?>
<?php if ($count%4 == 1 && $count != 1): ?>
<div class="clear"></div>
</div>
<div>
<?php endif; //$count%4 == 1 && $count != 1 ?>
<div><?php echo $row['value'] ?></div>
<?php $count++ ?>
<?php endwhile; ?>
<div class="clear"></div>
</div>
Why not use modulo to "close" a div?
<div>
<?php foreach($data as $key => $value) : ?>
<div><?php echo $value ?></div>
<?php if($key % 4 == 0 && $key != 0) : // add a clearing div, close the first group and open another one ?>
<div class="clear"></div>
</div>
<div>
<? endforeach ?>
<?php if($key % 4 != 0) : // div has not been closed as the number of records % 4 was not equal 0 ?>
<div class="clear"></div>
</div>
<? endif ?>
I have a code:
<?php for ($i = 0; $i < sizeof($categories); $i = $i + 4) { ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { ?>
<?php if (isset($categories[$j])) { ?>
<img src="<?php echo $categories[$j]['thumb']; ?>" title="<?php echo $categories[$j]['name']; ?>" alt="<?php echo $categories[$j]['name']; ?>" style="margin-bottom: 3px;" /><br />
<?php echo $categories[$j]['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
I want to place these categories in 2-column like this:
<div class="span-8">
<div class="product">
product1
</div>
</div>
<div class="span-8 last">
<div class="product">
product2
</div>
</div>
How can I do this?
<?php
for ($i = 0; $i < sizeof($categories); $i = $i + 4) {
for ($j = $i; $j < ($i + 4); $j++) {
if (isset($categories[$j])) {
if($colcount % 2){
$col1+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}else{
$col2+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}
$colcount++;
}
}
}
echo "<div class='span-8'>".$col1."</div><div class='span-8 last'>".$col2."</div>";
Or you could do it the easy way and make it a fluid <ul> that way they would do it automatically
I don't really see the correlation between the php and the html code, but I think you just need to output a different css class for each odd category:
$last = false;
foreach($categories as $c) {
//Output category html
?>
<div class="span <?=($last)?'last':''?>">.....</div>
<?
$last != $last;
}