Create outer div if counter is divisible by 4 - php

I have a problem looping my html tags. My goal is to enclose every 4 div with class "item" inside the class of "item-wrap". So far here's my code:
$speakers will return 8 rows so "item-wrap" class will be display twice with 4 "item" class inside
<?php
$speaker_ctr = 0;
if(count($speakers) > 0){
?>
<div class="item-wrap">
<?php foreach($speakers as $speaker){ ?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speaker['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speaker['name']; ?></h3>
<p class="no-mn"><?php echo $speaker['position']; ?></p>
<?php echo $speaker['company']; ?>
</div>
<?php } ?>
</div>
<?php
$speaker_ctr++;
}
?>

Try below code:-
<?php
if(count($speakers) > 0){
for($i=0;$i<=count($speakers);$i++){
if($i%4==0){
echo '<div class="item-wrap">';
}
?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speakers[$i]['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speakers[$i]['name']; ?></h3>
<p class="no-mn"><?php echo $speakers[$i]['position']; ?></p>
<?php echo $speakers[$i]['company']; ?>
</div>
<?php
if($i%4==0){
echo '</div>';
}
}
}

Try with -
$speaker_ctr = 0;
if(count($speakers) > 0){
?>
<div class="item-wrap">
<?php
foreach($speakers as $speaker){
$speaker_ctr++; // increment
?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speaker['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speaker['name']; ?></h3>
<p class="no-mn"><?php echo $speaker['position']; ?></p>
<?php echo $speaker['company']; ?>
</div>
<?php
// print if divisible by 4
// every 4th element
if($speaker_ctr%4 == 0 && $speaker_ctr != count($speakers)) {
echo "</div><div class='item-wrap'>";
}
}
?>
</div>
<?php
}

Related

Show random product on homepage through phtml

I have a block being displayed on my homepage which shows a product, whose id we specify. The code on homepage (static block) looks like this:
{{block type="core/template" product_id="2559" template="catalog/product/one_product.phtml"}}
The one_product.phtml file contains this code:
<?php
$productId = $this->getProduct_id();
$product = Mage::getModel('catalog/product')->load($productId); //load product
?>
<div class="product">
<a href="<?php echo $product->getProductUrl() ?>" >
<img class="product-img" src="<?php echo $this->helper('catalog/image')->init($product, 'image'); ?>"alt="<?php echo $this->htmlEscape($product->getName()) ?>" />
</a>
</div>
<div class="product-detail">
<P><?php // echo $this->htmlEscape($product->getName()) ?>
<?php $prod_name = $this->htmlEscape($product->getName()); ?>
<?php
$count_str = strlen($prod_name);
if ($count_str < 40) {
echo $prod_name;
} else {
$offset = 0;
$length = 41;
$prod_name = html_entity_decode($prod_name);
echo htmlentities(mb_substr($prod_name,0,$length,'utf-8')). "...";;
}
?>
</P>
<!--?php $price = $product->getPrice() ; ?-->
<?php $_product = Mage::getModel('catalog/product')->load($product->getId());
$productBlock = $this->getLayout()->createBlock('catalog/product_price');
?>
<span>
<?php echo $productBlock->getPriceHtml($_product); ?>
<?php $tier_price = end($_product->getTierPrice());
if($tier_price !='0'){ ?>
<span>As Low As:</span>
<?php
echo " ₹ ".number_format( $tier_price['price']);
} ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart"
onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($product); ?>')">
</span>
</div>
So basically I want to show random products out of the ones I specify separated by commas. For eg: I specify in the static block product_id="2559,2661,2857,9293" and it should show any one of those 4 products randomly.
What is the way to do that?
Also any way to make it pull products from SKU also? Since we remember all the SKUs but we have to check each product ID everytime we change the product here.
Please excuse me if the question is too basic, I'm not a developer.
You can see it in action at www.giftzila.com
Thanks!
Create a new file called random-product.phtml at app/design/frontend/default/Your_Theme/template/catalog/random-product.phtml then add the following code in that file
<?php
$chosen_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$_productCollection = $this->getLoadedProductCollection();
$number_of_products = $this->getNumProducts();
if (sizeof($_productCollection) < $number_of_products) {
$number_of_products = sizeof($_productCollection);
}
$displayed_products = array();
foreach ($_productCollection as $_product) {
$displayed_products[] = $_product;
}
$random_products = array();
if (sizeof($_productCollection) > 1) {
$random_products = array_rand($displayed_products, $number_of_products);
} else {
$random_products = array('0');
}
?>
<?php if(!$_productCollection->getSize()):?>
<div class="note-msg">
<?=$this->__('There are no products matching the selection.')?>
</div>
<?php else:?>
<div class="main-binder">
<div class="cms-box">
<div class="category-title">
<h2>Random Products</h2>
</div>
<div class="category-products">
<table id="products-grid-table" class="products-grid">
<?php
$k=0;
for ($i=0; $i < $number_of_products; $i++): ?>
<?php if ($k == 0) { ?>
<tr class="first odd">
<?php } if($k==3) { $k=0; ?>
</tr><tr class="first odd even">
<?php } ?>
<td id="td_<?php echo ($k+1); ?>" <?php if($k==3){ ?>class="last"<? } ?> >
<div class="cms-box">
<div id="cont_<?php echo ($k+1); ?>">
<div class="product-name-block">
<?php
$pname=$this->htmlEscape($displayed_products[$random_products[$i]]->getName());
?>
<h3 class="product-name">
<a href="<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>" title="<?php echo $pname; ?>">
<?php if(strlen($pname) > 28) {echo substr($pname,0,25)."...";}else {echo $pname;}?>
</a></h3>
</div>
<div class="image-box">
<a class="product-image" href="<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>"> <img src="<?php echo $this->helper('catalog/image')->init($displayed_products[$random_products[$i]], 'small_image')->resize(140);?>" alt="<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>" title="<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>"/> </a>
</div>
<div class="cms-price-box" style=" text-align:center;">
<span class="regular-price" id="product-price-37">
<span class="price" ><?php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->
getCurrentCurrencyCode())->getSymbol().$displayed_products[$random_products[$i]]->getPrice(); ?></span>
</span>
</div>
<div class="button-row" style="text-align:center;">
<button onclick="setLocation('<?php echo $displayed_products[$random_products[$i]]->getProductUrl();?>')" class="button" type="button"><span><span><span>Details</span></span></span></button>
<button onclick="setLocation('<?php echo $this->getUrl('')."/checkout/cart/add?product=".$displayed_products[$random_products[$i]]->getId()."&qty=1" ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button>
</div>
</div>
</div></td>
<?php $k++; ?>
<?php endfor;?>
</tr>
</table>
</div>
</div>
</div>
<?php endif;?>
Now call block in your cms home page by adding following code:-
{{block type="catalog/product_list" category_id="3" num_products="8" template="catalog/random-product.phtml"}}

Arrange divs on new row every third time

I have this most annoying problem; I'm trying to arrange three divs on a row, and then new row, and another three divs and so on, like this:
<div class="container">
<div class="row">
<div class="col-sm-1">1</div>
<div class="col-sm-1">2</div>
<div class="col-sm-1">3</div>
</div>
<div class="row">
<div class="col-sm-1">4</div>
<div class="col-sm-1">5</div>
<div class="col-sm-1">6</div>
</div>
</div>
As for this accepted answer,
There is one catch: 0 % 3 is equal to 0. This could result in
unexpected results if your counter starts at 0.
So how would i implement this into this code:
<div class="col-md-8">
<?php
foreach($this->movies->movie_data as $key => $movie){
$string = file_get_contents("http://example.com/?t=" . urlencode($movie->movie_titel). "&y=&plot=short&r=json");
$result = json_decode($string);
if($result->Response == 'True'){
?>
<div class="col-sm-4">
<?php if($result->Poster == 'N/A') : ?>
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo Config::get('URL')?>/images/na.png" class="img-responsive img-thumbnail"></a>
<?php else: ?>
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo $result->Poster; ?>" class="img-responsive img-thumbnail"></a>
<?php endif; ?>
<div><b><?php echo $result->Title; ?></b></div>
<div><i><?php // echo $result->Plot; ?></i></div>
</div>
<?php }else{ ?>
<div class="col-sm-4">
<a href="<?php echo Config::get('URL')?>ladybug/day/<?php echo $this->city ?>/<?php echo $movie->movie_id ?>">
<img src="<?php echo Config::get('URL')?>/images/na.png" class="img-responsive img-thumbnail"></a>
<div><b><?php echo $movie->movie_titel ?></b></div>
<div class="plot"><i><?php //echo 'N/A' ?></i></div>
</div>
<?php }}} ?
</div>
For some reason, divs is arranged like this:
My question: How do I arrange thumbnails on a new row, every third time?
Found the answer in the other Q... Didn't read, sorry about that.
<?php }
if (($key + 1) % 3 == 0) { ?>
</div>
<?php }
}} ?>

For loop to repeat and increment day number in code

I need loop some code trough 24 times to create a 24 day Christmas calendar. I have the for loop below but how do i add the '$i' to the code to make sure the wherever there is a day number it changes from 1 through to 24 each time? What would the code look like?
<?php for ($i = 1; $i < 25; $i++) { ?>
<!-- DAY 1 -->
<a href="offer.php?day=1<? echo '&dealership='. $dealership; ?>" class="item <? if ($today[mday] == 1) { echo "current yellow"; } else if ($today[mday] < 2 ) { echo "disabled"; }?>">
<div class="offer">
<h2>Day 1</h2>
<p><? echo Day_1_Offer('CAL_OFFER'); ?></p>
<? echo $termsLink; ?>
</div>
<div class="offer-img">
<img src="img/day1.jpg">
</div>
</a>
<?php } ?>
Just add <?php echo $i; ?> whenever you need the number.
<!-- DAY 1 -->
<a href="offer.php?day=<?php echo $i; ?><? echo '&dealership='. $dealership; ?>" class="item <? if ($today[mday] == 1) { echo "current yellow"; } else if ($today[mday] < 2 ) { echo "disabled"; }?>">
<div class="offer">
<h2>Day <?php echo $i; ?></h2>
<p><? echo call_user_func('Day_'.$i.'_Offer', 'CAL_OFFER'); ?></p>
<? echo $termsLink; ?>
</div>
<div class="offer-img">
<img src="img/day<?php echo $i; ?>.jpg">
</div>
</a>
<?php } ?>
To modify the Day_1_Offer call you must do a things a bit differently, as it's a function and in the PHP and not part of the HTML. To do this, you need call_user_func(). Try this:
<p><? echo call_user_func('Day_'.$i.'_Offer', 'CAL_OFFER'); ?></p>

Horizontal Div Inside While Loop showing the same products PHP

I am trying to create a horizontal div from while loop but the result is that i am getting the same products in both the column div. here is my code.
<div class="ui-grid-a">
<?php $query = mysql_query("select * from table");
while($sdeals = mysql_fetch_array($query){ ?>
<div class="ui-block-a">
<a href="">
<div style="height:120px; text-align: center;">
<div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
<img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
<?php if(!empty($sdeals['coupon_code'])) { ?>
<div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>
<?php } ?>
<div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>
</div>
</a>
<div class="ui-block-b">
<a href="">
<div style="height:120px; text-align: center;">
<div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
<img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
<?php if(!empty($sdeals['coupon_code'])) { ?>
<div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>
<?php } ?>
<div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>
</div>
</a>
</div>
<?php } ?>
</div>
the output i am getting is that
block-a block-b
pro1 pro1
pro2 pro2
pro3 pro3
pro4 pro4
the correct output should be
block-a block-b
pro1 pro2
pro3 pro4
pro5 pro6
pro7 pro8
pro9 pro10
<div class="ui-grid-a">
<?php
$num=0;
$class1 = "";
$query = mysql_query("select * from table");
while($sdeals = mysql_fetch_array($query)
{
if($num%2==0)
{ $class1 = 'class="ui-block-a"';}
else
{ $class1 = 'class="ui-block-b"';}
?>
<div <?php echo $class1;?>>
<a href="">
<div style="height:120px; text-align: center;">
<div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
<img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
<?php if(!empty($sdeals['coupon_code'])) { ?>
<div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>
<?php } ?>
<div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>
</div>
</a>
</div>
<?php
$num++;
} //while loop closing
?>
</div>
You have not moved the row pointer onto the next row between the 2 DIV's so $sdeals contains the same result in both DIV's.
<div class="ui-grid-a">
<?
php $query = mysql_query("select * from table");
$blockA = true;
while($sdeals = mysql_fetch_array($query){
if ( $blockA ) {
echo '<div class="ui-block-a">';
} else {
echo '<div class="ui-block-b">';
}
$blockA = ! $blockA;
?>
<a href="">
<div style="height:120px;text-align:center;">
<div style="font-size:60%;margin-top:5px;"><?php echo $sdeals['productname']; ?></div>
<img style="margin-top:5px;" height="67" width=50" src="../thumbnails/<?php echo $sdeals['imageg1']; ?>" >
<?php
if(!empty($sdeals['coupon_code'])) { ?>
<div style="font-size:60%;margin-top:5px;"> Use Coupon: <?php echo $sdeals['coupon_code']; ?></div>
<?php } ?>
<div style="font-size:60%;margin-top:5px;">Rs.<?php echo $sdeals['price']; ?></div>
</div>
</a>
</div>
<?php
}
?>
</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