Modifying the first item of the array - php

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

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

foreach array_slice not working

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

how to add a script after showing 10 videos in a php loop

I want to add a script after showing 10 videos in my website. But I don't understand how to use an if condition for this. This is my php code which displays all the videos from my database. I want that after displaying 10 videos it display this script.
<script>
This may contain the code of chitika code.
<script>
This is PHP code.
<section class="videos">
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section> <hr>
<?php } ?>
<?php } ?>
</section>
Inside your loop you just need a simple condition and a counter.
Here's an example. This is a loop that runs 12 times using a while loop like your code.
$i is an incrementing variable ($i++ increments it's value at the end of each loop cycle). The if statement says if $i is equal to 10 then do something. In this case it echos "10 records reached".
Make sure that when you define your counter first ($i = 1) it is before the while loop starts.
<?php
$i = 1;
while ($i <= 12) {
echo "record $i<br/>";
if ($i == 10) {
echo "10 records reached<br/>";
}
$i++;
}
?>
So adapting that thought process to your code would look like this:
<section class="videos">
<?php $i = 1; // This is where the counter is defined ?>
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section><hr>
<?php } ?>
<?php if ($i == 10) { // This is the new condition ?>
<script>
This may contain the code of chitika code.
<script>
<?php } // condition ends here ?>
<?php $i++ // increment the counter ?>
<?php } ?>
</section>
If you don't want to include your null values in the count (your 'coming soon' videos), you can simply move the counter increment $i++ up into the else { ... } portion of your condition.
<?php if (res === NULL) {
...
} else {
...
$i++;
} ?>

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>

generating links dynamically

so i guess this is pretty easy for most of you, but i can't figure this out.
im trying to make the links dynamic eg: href="linkname(#1 or #2 etc)"
any ideas?
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(GENERATE CODE HERE)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>
Suppose below is what you need.
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(#<?php echo $index + 1; ?>)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>

Categories