0

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

奇怪的是,除非我在 isset 之前回显 var,否则 storelocation isset 似乎失败了,有什么想法吗?

4

1 回答 1

0

创建受保护的方法来执行此操作并将 store 作为参数传递。这将解决您的问题。

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']);
  } 
}
于 2010-12-23T12:31:04.927 回答