PHP while loop, display in pairs of two - php

In Wordpress, I am creating a slider to slide through some content that the user specify. I am using ACF (advanced custom fields) if anyone is familiar with that, however what I am trying to accomplish is to show two content items at a time, while sliding through the jQuery slider.
Here is my loop:
<?php while(has_sub_field('popular_topic')): ?>
<li>
<div class="slide">
<img src="<?php the_sub_field('popular_topic_image'); ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
echo '<p>'.$newContent.'</p>';
?>
<a class="more-arrow" href="<?php the_sub_field('popular_topic_link'); ?>">Read More</a>
</div>
</div>
</li>
<?php endwhile; ?>
This is currently working, however it only shows one slide. I want it to show two slides and a time. Is there something I can do with a counter? Does this make sense?

my syntax might be a little off but maybe give this a shot?
<?php $i = 0; ?>
<?php while(has_sub_field('popular_topic')): ?>
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
$contentVar[$i] = array (
'img' => the_sub_field('popular_topic_image'),
'title' => $newContent,
'link' => the_sub_field('popular_topic_link')
);
$i++;
?>
<?php endwhile; ?>
<?php $j = 0; ?>
<?php while ($j < $i) : ?>
<li>
<div class="slide">
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php $j++; ?>
<?php if ($j <= $i) : ?>
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php endif; ?>
</div>
</li>
<?php $j++; ?>
<?php endwhile; ?>

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"}}

Inserting a block after a specified number of loop iterations

I am trying to fetch some info from database under ZF1 environment. I am using a for loop here. What I am trying to accomplish is that inserting a new css division (div) for each four iteration. In other words, I would like to insert a block followed by four loop iterations. For example, if I run a loop twelve times, there should be three new blocks after each four iterations. It is basically an advertisement block that may contain image and link. I need an idea.
Here is an example block.
<div class="flexblock-image">
<a href="https://offers.example.com/offer/600112?cid=BWO_mh.com_html_getbackinshape&keycode=254194">
<img alt="" title="" typeof="foaf:Image" src="http://www.example.com.gif">
</a>
</div>
Here is the loop I have coded in my app.
<?php if ($this->numArticles > 0) : ?>
<?php for ($i = 0; $i < 13; $i++) : ?>
<div class="channel-image ">
<div class="img">
<h2 class="primary-tag">
<a href="/tags/weight-loss-tips" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
<?php echo $this->articles[$i]->slug ; ?>
</a>
</h2>
<a href="
<?php echo $this->articles[$i]->getViewUrl(); ?>">
<img typeof="foaf:Image" src="
<?php echo $this->articles[$i]->getCover('crop'); ?>" width="650" height="412" alt="
<?php echo addslashes($article->title); ?>" title="
<?php echo addslashes($article->title); ?>">
</a>
</div>
<div class="channel-content">
<p class="date">
<span property="dc:date dc:created" content="2015-06-04T11:06:18-04:00" datatype="xsd:dateTime">
<?php echo $this->dateFormatter()->diff(strtotime($this->articles[0]->activated_date), $this->timeDiffFormats); ?>
</span>
</p>
<h2 class="article-title" id="title-node-93726">
<a href="
<?php echo $this->articles[$i]->getViewUrl(); ?>">
<?php echo addslashes($this->articles[$i]->title); ?>
</a>
</h2>
<div class="byline-container">
<span class="byline-role">By </span>
<span class="field-author">
<a href="#" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
<?php echo sprintf($this->translator()->_('show.by'), $this->articles[0]->getAuthor()); ?>
</a>
</span>
</div>
</div>
</div>
<?php endfor; ?>
<?php endif; ?>
Not an expert on PHP, but wouldn't an if statement inside of your for loop work by using the modulus operator?
<?php for ($i = 0; $i < 13; $i++) : ?>
<?php if ($i % 4 === 0) : ?>
// Custom HTML/CSS here
<?php endif; ?>
<?php endfor; ?>
More on the modulus operator can be found here

I want to apply css class as per below condition

I want to apply css class as per below condition but after applying this code each content is repeated by 8 times and if i complete for loop before div then css classes are not getting applied.
<?php $size=count($Spacategories);
for($i=0;$i<8;$i++){
if($i==0 || $i==5){ ?>
<li class="valign">
<?php } elseif($i==1 || $i==7) { ?>
<li class="lalign">
<?php } elseif($i==2 || $i==6) { ?>
<li class="talign">
<?php } else { ?>
<li class="ralign">
<?php } ?>
<div class="image">
<img src="<?php echo $spacategory['thumb']; ?>" title="<?php echo $spacategory['name']; ?>" alt="<?php echo $spacategory['name']; ?>" />
</div>
<div class="text">
<h3><?php echo $spacategory['name']; ?></h3>
<p><?php echo substr($spacategory['description'],0,150); ?></p>
<?php echo $text_view_more; ?>
</div>
</li>
<?php } } ?>
Change all instances of
$spacategory
to
$Spacategories[$i]
This will fix it.
Also, I don't see what's populating $text_view_more. You should probably change that to some data point from $Spacategories?
Finally, it seems you need to be looping based on the number of categories, so change
for($i=0;$i<8;$i++){
to
for($i=0;$i<$size;$i++){
In your code, $spacategory is not an item from $spacategories with the index $i in each iteration. You should have started the for:
$spacategory = $spacategories[$i];
or use a foreach instead:
<?php
$classesIndexMap = array(
0 => 'valign',
1 => 'lalign',
2 => 'talign',
5 => 'valign',
6 => 'talign',
7 => 'lalign',
);
$html_view_more = htmlspecialchars($text_view_more, ENT_NOQUOTES, 'UTF-8');
?>
<?php foreach($spacategories as $i => $spacategory) : ?>
<li class="<?= isset($spacategory[$i]) ? $spacategory[$i] : 'ralign' ?>">
<div class="image">
<img src="<?= htmlspecialchars($spacategory['thumb'], ENT_QUOTES, 'UTF-8') ?>"
title="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>"
alt="<?= htmlspecialchars($spacategory['name'], ENT_QUOTES, 'UTF-8') ?>" />
</div>
<div class="text">
<h3><?= htmlspecialchars($spacategory['name'], ENT_NOQUOTES, 'UTF-8') ?></h3>
<p><?= substr($spacategory['description'],0,150); ?></p>
<a href="<?= htmlspecialchars($spacategory['href'], ENT_QUOTES, 'UTF-8'); ?>"
class="learmmore"><?= $html_view_more ?></a>
</div>
</li>
<?php endforeach; ?>

Navigation menu with product thumbnails Magento

Hi guys I am trying to integrate CSS3 Mashmenu onto my Magento store http://www.mybloggerlab.com/2012/07/mashable-drop-down-navigation-menu-for.html
I would like to setup the menu to show product categories which when hovered over show product thumbnails with short description for each product in that category.
The prodblem I am having is setting this up so it is dynamic as I would prefer to have the menu be controllable via admin.
If someone could tell me where I am going wrong I would be very grateful as the current code isn't working.
<div id="pageContainer">
<div class="mashmenu">
<div class="fnav"><?php echo $this->__('BROWSE PRODUCTS'); ?>+
<div class="allContent">
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<div class="snav"><a href="#" class="slink">
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)</a>
<?php $collection = $_category->getProductCollection()->addAttributeToSort('name', 'asc'); ?>
<?php foreach ($collection as $_product) : ?>
<div class="insideContent">
<a href="<?php echo $this->getProductUrl($_product) ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(75) ?>" width="75" height="75" alt="<?php echo $this->stripTags($_product->getName(), null, true) ?>" />
</a>
<span> <?php $sdesc = $_product->getShortDescription();
$sdesc = trim($sdesc);
$limit = 170;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
} ?>
<?php echo $sdesc."..."; ?></span>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
<!-- end insideContent -->
</div>
</div>
</div>
</div>
<script>
$j(document).ready(function(){
$j('div.mashmenu img').css({"width":"100px","height":"60px"});
$j('div.mashmenu').find('.allContent').css({"top":"38px"});
$j('div.mashmenu').mouseleave(function(){
$j('div.mashmenu .allContent').show('50');
$j('div.mashmenu .insideContent').fadeOut('50');
});
$j('.flink').mouseenter(function(){
$j('div.mashmenu .allContent').show('50');
$j(this).parent('.fnav').children('.allContent').show(200);
});
$j('.slink').mouseenter(function(){
if($j(this).parent('.snav').children('.insideContent').find('a').size() != 0 )
$j(this).parents('.allContent').css({"width":"640px","height":"500px"});
else $j(this).parents('.allContent').css({"width":"auto","height":"auto"});
$j('div.mashmenu .insideContent').fadeOut('50');
$j(this).parent('.snav').children('.insideContent').fadeIn(200);
});
$j('.snav').mouseleave(function(){
$j(this).parents('.allContent').css({"width":"auto","height":"auto"});
});
$j('.snav').mouseenter(function(){
$j(this).children('.insideContent').css({"display":"block","position":"absolute","width":"auto","height":"450px"});
});
});
</script>
<?php endif; ?>
<div class="insideContent">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(75) ?>" width="75" height="75" alt="<?php echo $this->stripTags($_product->getName(), null, true) ?>" /> <span> <?php $sdesc = $_product->getShortDescription();
$sdesc = trim($sdesc);
$limit = 170;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
} ?>
<?php echo $sdesc."..."; ?></span>
</div>
Your $_product is undefined here, moreover you'll have only one product per category so which one to show ?
replace this by something like :
<?php $collection = Mage::getModel('catalog/product')->getCollection()->addCategoryFilter($_category)->setPageSize(4);
<div class="insideContent">
<?php foreach ($collection as $_product) { ?>
//Your product here, copy your code
<?php } /*end foreach*/ ?>
</div>
It should work better like this.

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