2012-10-02 76 views
0

我在創建Magento中的A-Z索引搜索時遇到了一些麻煩。我已經創建的代碼,但是,我得到一個「無效的方法...」錯誤:無效方法... str_replace_once(Array)

str_replace_once(Array ( [0] => & [1] => ? [2] => 127.0.0.1/mgn-default/electronics/cameras.html))

我不熟悉PHP,這裏是用於代碼:

<?php 
$search_array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','ALL'); 

/*Find if the URL already contains any querystring variable or not */ 
if (strstr($this->helper('core/url')->getCurrentUrl(), "&")) 
{ 
$separator = '&'; 
} 
else 
{ 
    $separator = '?'; 
} 
?> 
    <div> 
     <p class="view-mode-list-bot"> 
      <?php 
    $postData = Mage::app()->getRequest()->getParam('alpha'); 
    foreach ($search_array as $search_array_value): 

    /*Clean the URL*/ 
    if (strstr($this->helper('core/url')->getCurrentUrl(), "?")) 
    { 
    $new_Url = $this->str_replace_once('&','?',str_replace("?alpha=".trim($postData['alpha']),'',str_replace($separator."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl()))); 
    } 
    else 
    { 
    $new_Url = str_replace("?alpha=".trim($postData['alpha']),'',str_replace($separator."alpha=".trim($postData['alpha']),'',$this->helper('core/url')->getCurrentUrl())); 
    } 

    $alphaURL = $new_Url.$separator.'alpha='.$search_array_value; 
?> 

        <a href="<?php echo $alphaURL; ?>" title="<?php echo $_label ?>" class="<?php echo strtolower($_code); ?> <?php if($search_array_value == $postData){ echo 'remove_under'; } ?>"><?php echo $search_array_value; ?></a> 

      <?php endforeach; ?> 

     </p> 

任何意見和幫助將不勝感激,非常感謝。

+2

你自己實現了'str_replace_once()'嗎?它是由Magento定義的,並且可以在周圍的對象中使用? – mario

回答

0

我不知道磁電機,但我可以看到,是不需要來測試你的網址。你應該使用請求實例。

Mage::app()->getRequest()->getParam('alpha')

它混淆你實際上是試圖讓你的循環儘可能多的例子引用能有什麼價值varaiables的。但是,下面的可以幫助你看到變量都可以通過請求對象訪問,如:

$alpha = array('a','b','c', 'etc..'); 
/** fetch the request object **/ 
$request = Mage::app()->getRequest(); 
$letter = $request->getParam("letter"); // Would and return "k" from http://mysite.com?letter=k 
/** Test if we found a letter in the url **/ 
if (null !== $letter) { 
    /** output the alphabet as links with each letter value as a link parameter **/ 
    for ($x = 0; $x < 26; $x++) { 
    echo '<p><a href="?letter=$alpha">$alpha[$x]</a></p>'; 
    } 
} 

最後,它看起來好像你正在尋找的URL參數以及引用$_POST數據。根據我的經驗,最好每次只處理一個請求範圍($ _GET/$ _ POST)。

+0

謝謝你的努力。守則本身就是我所假設的。問題是,爲什麼我會得到錯誤「invalid method ... str_replace_once(Array([0] =>&[1] =>?[2] => 127.0.0.1/mgn-default/electronics/cameras.html )) – user1713222