2015-04-16 22 views
1

在我的Magento網店我想每一天,以顯示不同部件的每一天,循環顯示不同的部件。Magento的英文PHP:年底

我有10個不同的天10只不同的小部件。 10天后,第一個小部件應該再次顯示。

該代碼將在從PHTML一部分。

我想要的:第1天

顯示:

<?php 
$filter = new Mage_Widget_Model_Template_Filter(); 
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="1"}}'); 
echo $_widget; 
?> 

展第2天:

<?php 
$filter = new Mage_Widget_Model_Template_Filter(); 
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="2"}}'); 
echo $_widget; 
?> 

展會第3天:

<?php 
$filter = new Mage_Widget_Model_Template_Filter(); 
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="3"}}'); 
echo $_widget; 
?> 

.. ..

顯示在第10天:

<?php 
$filter = new Mage_Widget_Model_Template_Filter(); 
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="10"}}'); 
echo $_widget; 
?> 

而且每天10重啓與1天 這沒有結束日期後...

我怎樣才能做到這一點?

UPDATE 17-04-2015: 在我的示例中,block_id是按順序排列的。不過,也許不會被upfollowing BLOCK_ID的... 所以1日可能有block_id="12",第2天:block_id="4",第3天:block_id="21"

回答

1

使用日期和模量得到的值1-10。

<?php 
$filter = new Mage_Widget_Model_Template_Filter(); 
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="'.((date('z')%10)+1).'"}}'); 
echo $_widget; 
?> 

爲了解釋部分...

date('z'); //the day of the year will be an integer 1-365 
%10 //modulus is the remainder after division, so as the number climbs it will go 0,1,2,3,4,5,6,7,8,9,0,1,2,3.... 
+1 //turns your 0-9 values into 1-10 to match block ids. 

現在更新的問題問...如果我的塊ID不連續的什麼?我認爲在這種情況下,您可以使用鍵1-10和數值爲您的實際塊ID定義數組。

$idMapping = array(
    1 => 3, 
    2 => 5, 
    3 => 9, 
    4 => 13, 
    5 => 17, 
    6 => 23, 
    7 => 29, 
    8 => 31, 
    9 => 37, 
    10 => 41 
); 
$filter = new Mage_Widget_Model_Template_Filter(); 
$_widget = $filter->filter('{{widget type="myextension/widget_block" block_id="'.$idMapping[(date('z')%10)+1].'"}}'); 
echo $_widget; 

另一個關於我的解決方案。天在年數是不是10整除,因此在今年年底當它從365追溯到1,您將無法通過所有10個交易得到一個完整的旋轉。因此,對於該補丁將通過比較固定的日期來產生我們每天遞增的值,所以當你移動進一步遠離該日起只是一直攀升。

$date = "2015-04-17"; 
$diff = abs(strtotime($date) - time()); 
$days = floor($diff/3600/24); //converts seconds to hours, then to days 
//so now you can replace date('z') with $days and you'll loop continuously without any weird gap at the end of the year. 
+0

在我的問題我用下面的塊數。哪個是最優的情況。但它可能發生變化,並不像後續的尼斯一樣。這怎麼解決? – Ronny