1

我有洗牌和切片数组的问题。

我有这个代码:

$maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
$selectedProducts = array_slice($selectedProducts, 0, $maxDisplayItem);

foreach ($selectedProducts as $_id) {
  shuffle($products);
  foreach ($products as $_product) {        
....
  }
}

我的代码限制了显示项目的数量,但根本没有洗牌。

当我更改操作顺序时:

shuffle($selectedProducts);
foreach ($selectedProducts as $_id) {
  $maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
  $products = array_slice($products, 0, $maxDisplayItem);
  foreach ($products as $_product) {
....
  }
}

代码改组和切片结果,但仅来自具有 50 个项目的整个数组的第一个(例如 3 个结果)。

谁能帮我解决这个问题?

这是整个功能:

function displayProductList()
    {
        // Store View
        $store = $this->getStoreViewCode();

        $selectedProducts = $this->getSelectedProducts();
        $products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());

        // Load Template File
        $templateHtml       = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
        $productListHtml    = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
        $productHtml        = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');

        $subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';

        shuffle($selectedProducts);

        foreach ($selectedProducts as $_id) {

          $maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
          $products = array_slice($products, 0, $maxDisplayItem);

            foreach ($products as $_product) {

                if ($_id === $_product['product_id']) {
                    $markers = $this->_products->getProductMarkers($_product);

                    // Even/Odd CSS Class Determination
                    if ($even === true) {
                        $line = 'even';
                        $even = false;
                    } else {
                        $line = 'odd';
                        $even = true;
                    }

                    // Class Determination First/Last
                    if ($item == 0) {
                        $markers['###EVENODD###'] = $line . ' ' . 'first'; 
                    } else if ($item == $items-1) {
                        $markers['###EVENODD###'] = $line . ' ' . 'last';  
                    } else {
                        $markers['###EVENODD###'] = $line; 
                    }

                    // Check if the product has an image
                    $imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
                    if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
                        $imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
                    }

                    $p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
                    $subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
                    $item++;
                }
            }
        }
        return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
    }
4

1 回答 1

0
function displayProductList(){
    // Store View
    $store = $this->getStoreViewCode();

    $selectedProducts = $this->getSelectedProducts();
    $products = $this->_products->getProductsFromDb($selectedProducts, $store, $this->getProductsStoragePid());

    // Load Template File
    $templateHtml       = $this->cObj->fileResource( $this->_getConfig('templateProductList') );
    $productListHtml    = $this->cObj->getSubpart($templateHtml, '###PRODUCT_LIST###');
    $productHtml        = $this->cObj->getSubpart($productListHtml, '###PRODUCT_ITEM###');

    $subPartContent = ''; $item = 0; $items = count($products); $even = true; $line = '';

    $maxDisplayItem = $this->_getFlexformConfig('max_item_to_display', 'product_setting');
    $products = array_slice($products, 0, $maxDisplayItem);

    shuffle($products);

    foreach ($products as $_product) {

        $markers = $this->_products->getProductMarkers($_product);

        // Even/Odd CSS Class Determination
        if ($even === true) {
            $line = 'even';
            $even = false;
        } else {
            $line = 'odd';
            $even = true;
        }

        // Class Determination First/Last
        if ($item == 0) {
            $markers['###EVENODD###'] = $line . ' ' . 'first'; 
        } else if ($item == $items-1) {
            $markers['###EVENODD###'] = $line . ' ' . 'last';  
        } else {
            $markers['###EVENODD###'] = $line; 
        }

        // Check if the product has an image
        $imageHtml = '<p>'.$this->pi_getLL('template_label_no_image_available').'</p>';
        if ($markers['###DETAIL_IMAGE###'] != 'no_selection') {
            $imageHtml = $this->cObj->getSubpart($productHtml, '###PRODUCT_IMAGE###');
        }

        $p = $this->cObj->substituteSubpart($productHtml, '###PRODUCT_IMAGE###', $imageHtml);
        $subPartContent .= $this->cObj->substituteMarkerArray($p, $markers);
        $item++;

    }

    return $this->cObj->substituteSubpart($productListHtml, '###PRODUCT_ITEM###', $subPartContent);
}
于 2013-07-19T12:32:03.110 回答