2010-12-23 174 views
0

我遇到了我的PHP變量問題,並將它們設置爲正確的值。基本上我有一種方法,看看你是否存在商店,以及該商店是否有姊妹店。然後,我看看是否存儲爲文件系統中的圖像,如果它沒有顯示並等待圖像圖像。PHP變量問題

我的問題是,如果店內有一個姊妹店那麼它只會顯示姊妹店的形象,爲姐姐和這裏的主要商店都爲我的代碼

if(isset($storelocation)) { 
    //var_dump($sofalocation); 
    $path = $_SERVER{'DOCUMENT_ROOT'}.'/webroot/images/stores/'.strtolower(str_replace(' ', '-', $storelocation['storename'])).'.jpg'; 
    if(file_exists($path)) { 
    //echo $path; 
    $this->assign('storeimage', 'images/stores/'.strtolower(str_replace(' ', '-', $storelocation['storename'])).'.jpg'); 
    //var_dump($sofalocation['storename']); 
    } else { 
    echo $path; 
    $this->assign('storeimage', 'images/stores/awaiting-image.jpg'); 
    } 
} 

if(isset($sisterlocation)) { 
    $path = $_SERVER{'DOCUMENT_ROOT'}.'/webroot/images/stores/'.strtolower(str_replace(' ', '-', $sisterlocation['storename'])).'.jpg'; 
    if(file_exists($path)) { 
    $this->assign('sisterimage', 'images/stores/'.strtolower(str_replace(' ', '-', $sisterglocation['storename'])).'.jpg'); 

    } else { 
    echo $path; 
    $this->assign('sisterimage', 'images/stores/awaiting-image.jpg'); 
    // var_dump($sisterlocation['storename']); 
    } 
} 

好奇的是storeset isset似乎失敗,除非我在isset之前回顯var,有沒有任何想法?

+0

您是否將$ storelocation和$ sisterlocation作爲您的方法的參數?如果不是,則需要使用方法頂部的global關鍵字聲明它們:global $ storelocation,$ sisterlocation;看到你的整個方法會很有幫助。 – bigwebguy 2010-12-23 12:56:10

回答

0

創建受保護的方法來執行此操作並將商店作爲參數傳遞。 這將解決您的問題。

protected function _prepareImage($storeName, $storeLocation) 
{ 
    $path = $_SERVER{'DOCUMENT_ROOT'}.'/webroot/images/stores/'.strtolower(str_replace(' ', '-', $storeLocation['storename'])).'.jpg'; 
    if(file_exists($path)) { 
    $this->assign($storeName, 'images/stores/'.strtolower(str_replace(' ', '-', $storeLocation['storename'])).'.jpg'); 

    } else { 
    echo $path; 
    $this->assign($storeName, 'images/stores/awaiting-image.jpg'); 
    // var_dump($storeLocation['storename']); 
    } 
} 
+0

我該怎麼做?我對PHP很新 – 2010-12-23 12:32:09