Jump to content

'IF' you can help on this, that would be great


turn_left_at_mars

Recommended Posts

Hey,

I'm just finishing off my first store project. But i need some help on one small thing, which has defeated me for a while.

 

Basically, I have a category page where I want to display products in rows of '3' products , broken up by a divider line (devider-product-2).

 

Simple as it sounds I can't get the IF statement correct. It works with 2 products but not with 3 (when i replace the variable 2, with 3 - in the If and Colspan). I've tried a few things but it just ain't happening at the moment.

 

The columnCount variable is set to ‘3’ elsewhere.

 

The Code is below, hope someone can help me sort this out.

 

<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
    <table class="products-grid" id="products-grid-table">
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%2==0): ?>
        <tr>
        <?php endif ?>
            <td>
                <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
                <div class="product-image-box"><a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 150); ?>" width="150" height="150" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a></div>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <?php if($_product->isSaleable()): ?>
                <button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
                <?php else: ?>
                <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
                <?php endif; ?>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a class="wishlist-link" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                        <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
            </td>

        <?php if ($i++%2==0): ?>
        </tr>
        <tr>
                <td colspan="2" class="devider-product-2"></td>
        </tr>
       <?php endif ?>

        <?php $i++; endforeach ?>
        <?php if ($i++%2==0): ?>
        </tr>
        <tr>
                <td colspan="2" class="devider-product-2"></td>
        </tr>
        <?php endif ?>
    
</table>
    <script type="text/javascript">decorateTable('products-grid-table')</script>
    <?php endif; ?>
    <?php echo $this->getToolbarHtml() ?> 

 

Thanks for reading this.

 

David

Link to comment
Share on other sites

I think you need some more opening and closing PHP tags! Is that really how you write your code or is that being generated from DreamWeaver or some other app? It's just difficult to try and "read" that code to understand what is taking place.

 

Anyway, I see that you have hard-coded the "2" in several places. Are you sure you have changed the "2" to a "3" in ALL the places needed? You would be better served by creatng a variable to hold the column count. Then you can change the columns by changing that one variable instead of trying to hunt down all the modifications needed.

 

Ok, after reading the code a little bit, I think I see the problem. In all of the conditions to check which row you are on, the value of $i is being increased:

 

Line 6:

<?php if ($i++%2==0): ?>

 

Line :32

<?php if ($i++%2==0): ?>

 

Line : 40

<?php if ($i++%2==0): ?>

 

The first two of those are in the foreach loop. That means $i is getting incremented twice for each record. Instead of incrementing $i in the conditinoal statements, just add a $i++ on the first line aftyer the foreach loops and change your conditionals to just check $i (without incrementing it).

 

Also, define a variable at the top of the script for $max_columns. Then in your conditionals you could use

<?php if ($i++%$max_columns==0): ?>

 

Now you can change the number of columns used by chnaging that variable.

Link to comment
Share on other sites

Try this, I have modified it slightly and removed the if() section from after the foreach, I don't know why you had that there?

 

Set the products per row in $prodsPerRow, as you can see it is set to 3.

 

<table class="products-grid" id="products-grid-table">
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $prodsPerRow = 3; $i=0; foreach ($_productCollection as $_product): ?>
<?php if($i % $prodsPerRow == 0): ?>
<tr>
<?php endif ?>
	<td>
		<h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
		<div class="product-image-box"><a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
			<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 150); ?>" width="150" height="150" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a></div>
		<?php if($_product->getRatingSummary()): ?>
		<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
		<?php endif; ?>
		<?php echo $this->getPriceHtml($_product, true) ?>
		<?php if($_product->isSaleable()): ?>
		<button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
		<?php else: ?>
		<p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
		<?php endif; ?>
		<ul class="add-to-links">
			<?php if ($this->helper('wishlist')->isAllow()) : ?>
				<li><a class="wishlist-link" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
			<?php endif; ?>
			<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
				<li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
			<?php endif; ?>
		</ul>
	</td>

<?php if($i+1 % $prodsPerRow == 0 && $i != 0): ?>
</tr>
<tr>
		<td colspan="2" class="devider-product-2"></td>
</tr>
   <?php endif ?>

<?php $i++; endforeach ?>


</table>

Link to comment
Share on other sites

I didn't modify your code aside from clean it up. Before, your code was hard to read. This should be easier. It's also how most people code (roughly. One of the perks of PHP is that you can "freestyle" your code, and it will still work).
[code=php:0]
<?php
echo '<table class="products-grid" id="products-grid-table">';
$_columnCount = $this->getColumnCount();
$prodsPerRow = 3;
$i=0;
foreach ($_productCollection as $_product){
$product_image_url = $_product->getProductUrl();
$product_image_title = $this->htmlEscape($_product->getName());
$product_image_tag = $this->htmlEscape($this->getImageLabel($_product, 'small_image'));
$product_image = $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 150);
$product_image_alt = $this->htmlEscape($this->getImageLabel($_product, 'small_image'));
if($i % $prodsPerRow == 0){
	echo "<tr>";
}
echo<<<EOF
<td>
<h3 class="product-name"><a href="$product_image_url" title="$product_image_title">$product_image_title</a></h3>
<div class="product-image-box"><a class="product-image"	href="$product_image_url" title="$product_image_tag">
<img src="$product_image" width="150" height="150" alt="$product_image_tag"	title="$product_image_tag" /></a></div>
EOF;
if($_product->getRatingSummary()){
	echo $this->getReviewsSummaryHtml($_product, 'short');
}
echo $this->getPriceHtml($_product, true);
if($_product->isSaleable()){
	echo '<button type="button" class="button" onclick="setLocation(\''.$this->getAddToCartUrl($_product).'\')"><span><span><span>'.$this->__('Add to Cart').'</span></span></span></button>';
}
else{
	echo '<p class="availability"><span class="out-of-stock">'.$this->__('Out of stock').'
</span>
</p>';
}
echo '<ul class="add-to-links">';
if ($this->helper('wishlist')->isAllow()){
	echo '<li><a class="wishlist-link"
	href="'.$this->helper('wishlist')->getAddUrl($_product).'">'.$this->__('Add to Wishlist');
}
if($_compareUrl=$this->getAddToCompareUrl($_product)){
	echo '<li><span class="separator">|</span> <a
	href="'.$_compareUrl.'">'.$this->__('Add to Compare').'</a></li>';
}
echo "</ul>
</td>";
if($i+1 % $prodsPerRow == 0 && $i != 0){
	echo "</tr>
	<tr>
	<td colspan=\"2\" class=\"devider-product-2\"></td>
	</tr>";
}
$i++;
}

all errors are still there, but I figured I could show you how to write the code that everybody can easily read.

Link to comment
Share on other sites

Ignore my code in the above post, there is an error in it. Here it is fixed:

 

The code below allows you to choose whether the seperator shows under the last row by changing showSeperatorUnderLastRow to true/false

 


<table class="products-grid" id="products-grid-table">
    <?php $_columnCount = $this->getColumnCount(); ?>

    <?php

// set these two variables
$showSeperatorUnderLastRow = false;
$prodsPerRow = 3;

// don't change these
$i=0;
$totalProds = count($_productCollection);

foreach ($_productCollection as $_product):

	$showOpenTR = $i % $prodsPerRow == 0 || $i == 0 ? true : false;
	$needToCloseRow = $i % $prodsPerRow == $prodsPerRow - 1 || $i + 1 == $totalProds ? true : false;

	$showCloseTROnly = $showSeperatorUnderLastRow == false && $needToCloseRow && $i + 1 == $totalProds ? true : false;
	$showCloseTRAndSeperator = $needToCloseRow && $showCloseTROnly == false ? true : false;

?>
        <?php if($showOpenTR): ?>
		<tr>
        <?php endif; ?>
            <td>
                <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
                <div class="product-image-box"><a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 150); ?>" width="150" height="150" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a></div>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <?php if($_product->isSaleable()): ?>
                <button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
                <?php else: ?>
                <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
                <?php endif; ?>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a class="wishlist-link" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                        <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
            </td>

        <?php if($showCloseTROnly): ?>
		</tr>
	<?php endif; ?>
	<?php if($showCloseTRAndSeperator): ?>
        </tr>
        <tr>
                <td colspan="2" class="devider-product-2"></td>
        </tr>
       <?php endif; ?>

        <?php $i++; endforeach; ?>
    
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.