使用Mage_Catalog_Model_Resource_Product_Collection进行分页

时间:2014-08-05 11:14:21

标签: php magento

我要在magento上创建分页项目

$page  = $_REQUEST['p'];
$page  = is_numeric($page) ? (int) $page : 1;
$page  = $page > 0 ? $page : 1;
$perpage = 5;

$collection = Mage::getResourceModel('catalog/product_collection');

$collection->addCategoryFilter($aCategory); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
$collection->addPriceData();

if( $_REQUEST['order'] == 'manufacturer' )
{
    $collection->addAttributeToSort('manufacturer', $_REQUEST['dir']);
}
else
{
    $collection->getSelect()->order($_REQUEST['order'].' '. $_REQUEST['dir']); //uncomment to get products in random order
}
$collection->addStoreFilter();
$collection->setPage( $page, $perpage );

我有67项,当我给出? p = 300 $ collection stil返回2项。 它应该返回空。 请帮帮我!

更新:这是查询$ collection-> getSelect()

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `at_status`.`value` AS `status`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, IF(manufacturer_option_value_t2.value_id IS NULL, manufacturer_option_value_t1.value, manufacturer_option_value_t2.value) AS `manufacturer` FROM `hcc_catalog_product_entity` AS `e`
 INNER JOIN `hcc_catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='4' AND cat_index.category_id='25' AND cat_index.is_parent=1
 INNER JOIN `hcc_catalog_product_entity_int` AS `at_status` ON (`at_status`.`entity_id` = `e`.`entity_id`) AND (`at_status`.`attribute_id` = '84') AND (`at_status`.`store_id` = 0)
 INNER JOIN `hcc_catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '2' AND price_index.customer_group_id = 0
 LEFT JOIN `hcc_catalog_product_entity_int` AS `manufacturer_t1` ON e.entity_id=manufacturer_t1.entity_id AND manufacturer_t1.attribute_id='70' AND manufacturer_t1.store_id=0
 LEFT JOIN `hcc_catalog_product_entity_int` AS `manufacturer_t2` ON e.entity_id=manufacturer_t2.entity_id AND manufacturer_t2.attribute_id='70' AND manufacturer_t2.store_id='4'
 LEFT JOIN `hcc_eav_attribute_option_value` AS `manufacturer_option_value_t1` ON manufacturer_option_value_t1.option_id=IF(manufacturer_t2.value_id > 0, manufacturer_t2.value, manufacturer_t1.value) AND manufacturer_option_value_t1.store_id=0
 LEFT JOIN `hcc_eav_attribute_option_value` AS `manufacturer_option_value_t2` ON manufacturer_option_value_t2.option_id=IF(manufacturer_t2.value_id > 0, manufacturer_t2.value, manufacturer_t1.value) AND manufacturer_option_value_t2.store_id=4 
WHERE (at_status.value = '1') ORDER BY `manufacturer` asc 
LIMIT 5 OFFSET 65

你可以看到" LIMIT 5 OFFSET 65 ",这是不正确的。它应该是 LIMIT 5 OFFSET(300-1)* 5

===================更新======================

我解决了我的问题。感谢 @Vishal Sharma

   $total = $collection->getSize();       
     if( $total >= ($page-1)*$perpage)
     {
          // code at here
     }

1 个答案:

答案 0 :(得分:2)

试试这个

$limit = 5;
$page = (int) (!isset($_GET['p'])) ? 1 : $_GET['p'];
$start = $page;
$_productCollection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->clear()
                    ->addAttributeToSelect(array('name','url','small_image'))
                    ->addAttributeToFilter($aCategory)
                    ->addAttributeToFilter('status',1)
                    ->setPageSize($limit)
                    ->setCurPage($start)
                    ->load();

将此条件放在Item Html

之前
$totCat = $_productCollection->getSize();
$pageTot = ceil( $totCat / $limit );
if($pageTot >= $page)
{
  //ITEM HTML
}