Domanda

Ho un modulo personalizzato con il quale siamo in grado di aggiungere immagini banner in admin. Ho implementato questo modulo in admin successo. Ora voglio visualizzare banner nella parte anteriore.

La mia tabella personalizzata è il seguente

table name : banner
     fields: banner_name  |  store_ids  |  images  |   img_path  |  status  

id e memorizzare le immagini sono memorizzati come comma-seperated-values. Ora voglio selezionare una voce di bandiera che corrisponde alla vista attuale del serbatoio. Ecco il mio codice che ho cercato di modulo display banner nella frontend.

<?php
    //get all active banner items as array
    $bannerItems = Mage::getModel('banner/banner') -> getCollection()
                -> addFieldToFilter('status',1) -> getItems(); //select only enabled items                  
    $curStoreId = Mage::app() -> getStore() -> getId(); //get current store id
    $banner = "";
    foreach($bannerItems as $item) 
    {    
         $data = $item -> getData();
         /*
             Store ids are saved as `comma separated values`. Hence making
             all store_ids as an array
         */
         $storearray = explode(",",$data['store_id']);
         foreach($storearray as $store)
         {                          
             if($store == $curStoreId) //check for matches, if yes load the item
             {               
                 $banner = $item;
                 break;               
             }

         }        
         if($banner != "") // ensure to store the first matched item to load
         {
             break;
         }

     }
     //output
     if( empty($banner) ) 
     {
         die("no banner exist for this store.Please add a banner !!!");
     }
     else
     {
         die( print_r( $banner->getId() ) );
     }
?>

Questo mi fornire il modello striscione corrispondono per la vista attuale del serbatoio. Tuttavia ritengo che questo codice è lungo. Così ho bisogno di sapere come posso raggiungere questo obiettivo al suo meglio. Si prega di fornire i vostri suggerimenti. Grazie in anticipo

È stato utile?

Soluzione

Non ho "provato" questo, ma questo dovrebbe essere utile.

$currentStore = 5;

$bannerItems = Mage::getModel('banner/banner') -> getCollection()
            -> addFieldToFilter('status',1);
$bannerItems->getSelect()->where("FIND_IN_SET($currentStore, store_ids) > 0");
$bannerItems->getSelect()->orWhere("store_ids = 0");
$bannerItems->getSelect()->order("(FIND_IN_SET($currentStore, store_ids) > 0 ) * 10 DESC");

Per prima scoprire la STORE_ID corrente (1, utilizzato per esempio) utilizzando FIND_IN_SET e anche cercare store_ids = 0, ma una sorta l'ex risultato se esiste in ordine decrescente in modo che si può ottenere il risultato richiesto in prima fila.

Dopo di che solo l'uso $bannerItems->getFirstItem(), everthing è stato già fatto nella query.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top