简体   繁体   English

PHP变量问题

[英]PHP variable issue

I am having an issue with my PHP vars and getting them set to the correct value. 我的PHP变量有问题,并将它们设置为正确的值。 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? 奇怪的是,除非我在isset之前回显var,否则storelocation isset似乎会失败,有什么想法吗?

Create protected method to do this and pass store as an argument. 创建受保护的方法来执行此操作,并将store作为参数传递。 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']);
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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