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 に答える