title: Division by zero in Magento Bestseller Script
body: <p>I found this script for displaying magentos bestsellers but I get a <strong>ERR (3): Warning: Division by zero</strong> on line 135 and 163 (see code below). I have been trying to fix it hours and gave up on it but the warning in my error logs bugs me. Can someone take a look and see if they can tell me what is wrong?  Thanks</p>
  
  <pre><code>&lt;?php
  
  /**
   * @author Branko Ajzele | http://activecodeline.com | branko.ajzele@surgeworks.com
   * @license GPL
   */
  
  /**
  *
  *
  05/06/2011 Modified by Erik Gabor | erikvail@gmail.com | http://www.mage-contacts.com
  * @license GPL
  
  */ 
  
  $storeId = Mage::app()-&gt;getStore()-&gt;getId();
  
  $totalPerPage = ($this-&gt;show_total) ? $this-&gt;show_total :1;
  $counter = 1;
  $visibility = array(
                        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                    );
  
  $storeId = Mage::app()-&gt;getStore()-&gt;getId();
  $_productCollection = Mage::getResourceModel('reports/product_collection')
                                -&gt;addAttributeToSelect('*')
                                -&gt;setStoreId($storeId)
                    -&gt;addStoreFilter($storeId)
                                -&gt;addOrderedQty()
                                -&gt;addAttributeToFilter('visibility', $visibility);
  
  
          if ($current_category = Mage::registry('current_category'))
      $_productCollection-&gt;addCategoryFilter($current_category);
      $_productCollection-&gt;setPage(1,$totalPerPage);  
          $_productCollection-&gt;setOrder('ordered_qty', 'desc');
  
          Mage::getSingleton('catalog/product_status')-&gt;addVisibleFilterToCollection($_productCollection);
          Mage::getSingleton('catalog/product_visibility')-&gt;addVisibleInCatalogFilterToCollection($_productCollection);
  
          $_helper = $this-&gt;helper('catalog/output');
  
  
  //Mage_Reports_Model_Mysql4_Product_Collection
  ?&gt;
  
  &lt;?php
  
   $_collectionSize = $_productCollection-&gt;count();
  
  
   if($current_category &amp;&amp; $_collectionSize) :
  
  ?&gt;
  &lt;div class="block"&gt;
  &lt;div class="block-title"&gt;
  &lt;strong&gt;&lt;span&gt;&lt;?php echo $current_category-&gt;getName()  ?&gt; &lt;?php echo $this-&gt;__('Bestseller') ?&gt;&lt;/span&gt;&lt;/strong&gt;
  &lt;/div&gt;
  &lt;?php else: ?&gt;
  &lt;div class="block"&gt;
  &lt;div class="block-title"&gt;
  &lt;strong&gt;&lt;span&gt; &lt;?php echo $this-&gt;__('Bestseller') ?&gt;&lt;/span&gt;&lt;/strong&gt;
  &lt;/div&gt;
   &lt;?php endif;?&gt;
    &lt;?php $_collectionSize = $_productCollection-&gt;count() ?&gt;
      &lt;?php $_columnCount = $this-&gt;getColumnCount(); ?&gt;
      &lt;?php $i=0; foreach ($_productCollection as $_product): ?&gt;
  RIGHT HERE LINE 135 ----&gt;   &lt;?php if ($i++%$_columnCount==0): ?&gt;
          &lt;ul&gt;
          &lt;?php endif ?&gt;
              &lt;li id="category-bettseller"&gt;
                  &lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" class="product-image"&gt;&lt;img src="&lt;?php echo $this-&gt;helper('catalog/image')-&gt;init($_product, 'small_image')-&gt;constrainOnly(TRUE)-&gt;keepAspectRatio(TRUE)-&gt;keepFrame(FALSE)-&gt;resize(195); ?&gt;" width="195" height="284" alt="&lt;?php echo $this-&gt;stripTags($this-&gt;getImageLabel($_product, 'small_image'), null, true) ?&gt;" /&gt;&lt;/a&gt;
  
                  &lt;h2 class="product-name" style="float:left;width:180px;margin:7px"&gt;&lt;a href="&lt;?php echo $_product-&gt;getProductUrl() ?&gt;" title="&lt;?php echo $this-&gt;stripTags($_product-&gt;getName(), null, true) ?&gt;"&gt;&lt;?php echo $_helper-&gt;productAttribute($_product, $_product-&gt;getName(), 'name') ?&gt;&lt;/a&gt;&lt;/h2&gt;
                   &lt;div style="float:left;width:180px;margin:7px"&gt;
                  &lt;?php if($_product-&gt;getRatingSummary()): ?&gt;
                  &lt;?php echo $this-&gt;getReviewsSummaryHtml($_product, 'short') ?&gt;
                  &lt;?php endif; ?&gt;
                  &lt;?php  echo Mage::helper('core')-&gt;currency($_product-&gt;getPrice()) ?&gt;
  
                      &lt;?php if($_product-&gt;isSaleable()): ?&gt;
                          &lt;button type="button" title="&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;" class="button btn-cart" onclick="setLocation('&lt;?php echo Mage::getUrl('checkout/cart/add', array('product' =&gt; $_product-&gt;getId())) ?&gt;')"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Add to Cart') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;
                      &lt;?php else: ?&gt;
                          &lt;p class="availability out-of-stock"&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Out of stock') ?&gt;&lt;/span&gt;&lt;/p&gt;
                      &lt;?php endif; ?&gt;
                      &lt;ul class="add-to-links"&gt;
                          &lt;?php if ($this-&gt;helper('wishlist')-&gt;isAllow()) : ?&gt;
                              &lt;li&gt;&lt;a id="wish" href="&lt;?php echo $this-&gt;helper('wishlist')-&gt;getAddUrl($_product) ?&gt;" class="link-wishlist"&gt;&lt;?php echo $this-&gt;__('Add to Wishlist') ?&gt;&lt;/a&gt;&lt;/li&gt;
                          &lt;?php endif; ?&gt;
                          &lt;?php if($_compareUrl=$this-&gt;getAddToCompareUrl($_product)): ?&gt;
                              &lt;li&gt;&lt;a id="compare" href="&lt;?php echo $_compareUrl ?&gt;" class="link-compare"&gt;&lt;?php echo $this-&gt;__('Add to Compare') ?&gt;&lt;/a&gt;&lt;/li&gt;
                          &lt;?php endif; ?&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
              &lt;/li&gt;
  RIGHT HERE LINE 163------&gt;   &lt;?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?&gt;
          &lt;/ul&gt;
  &lt;/div&gt;
          &lt;?php endif ?&gt;
          &lt;?php endforeach ?&gt;
  </code></pre>