简体   繁体   中英

PHP variable issue

I am having an issue with my PHP vars and getting them set to the correct value. Basically I have a method that looks to see if you a store exists and also whether or not that store has a sister store. I then look to see if that store as a image in the file system if it does not i show and awaiting image image.

My issue is that if the store has a sister store then it will only show the image of the sister store, for both the sister and the main store here is my code

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']);
  }  
}

The curious thing is that the storelocation isset seems to fail unless I echo the var before the isset, has any got any ideas?

Create protected method to do this and pass store as an argument. This will solve your problem.

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']);
  } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM