我有这个php块
<?php switch ($_GET['class_id']) { ?>
<?php case 1: ?>
<img src="<?php print $result['standard_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['standard_image']}" ?>" alt="" />
<?php break; ?>
<?php case 2: ?>
<img src="<?php print $result['business_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['business_image']}" ?>" alt="" />
<?php break; ?>
<?php case 3: ?>
<img src="<?php print $result['premium_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['premium_image']}" ?>" alt="" />
<?php break; ?>
<?php default: ?>
<img src="<?php print $result['standard_image'] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['standard_image']}" ?>" alt="" />
<?php break; ?>
<?php } ?>
但这看起来有点乱。任何建议
答案 0 :(得分:10)
为什么所有案例都放在第一位?您只是选择要使用的图像。
$images = array(
1 => 'standard_image',
2 => 'business_image',
3 => 'premium_image'
);
$id = $_GET['class_id'];
$image_key = (isset($images[$id])) ? $images[$id] : 'standard_image';
$image = '/pre_config/css/images/blue-png2.png';
if ($result[$image_key] != '')
$image = $result[$image_key];
echo '<img src="' . $image . '" alt="Logo" />';
增加奖励:逻辑的目的更清晰。
答案 1 :(得分:2)
switch($_GET['class_id'])
{
case 1:
$source = '/images/src1.png';
$class = 'yourClass';
$alt = 'yourAlt';
break;
case 2:
$source = '/images/src2.png';
$class = 'yourClass2';
$alt = 'yourAlt2';
break;
case 3:
$source = '/images/src3.png';
$class = 'yourClass3';
$alt = 'yourAlt3';
break;
default:
$source = '/images/src4.png';
$class = 'yourClass4';
$alt = 'yourAlt4';
break;
}
$image = <<<EOF
<img src="$source" class="$class" alt="$alt" />
EOF;
echo $image;
答案 2 :(得分:1)
我同意,代码很疯狂。
将所有PHP代码写在一个<?php ?>
块中。当您必须打印某些内容时,请使用print函数或echo构造。
声明可以帮助您阅读和理解代码的变量。
例如:
<?php
switch ($_GET['class_id']) {
case 1:
$imgpath = ($result['standard_image'] == "") ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['standard_image']}";
echo '<img src="'.$imgpath.'" alt="" />';
break;
case 2:
// and so on for the other cases...
}
?>
是不是不那么乱?
答案 3 :(得分:1)
<?php
$image = $result['standard_image'];
switch ($_GET['class_id']) {
case 2: $image = $result['business_image']; break;
case 3: $image = $result['premium_image']; break;
}
?>
<img src="<?php print $image == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$image}"; ?>" alt="" />
您不需要每行打开和关闭php标记。
答案 4 :(得分:1)
<?php
$img = '';
switch ($_GET['class_id']) {
case 1 :
$img = $result['standard_image'];
break;
case 2 :
$img = $result['business_image'];
break;
case 3 :
$img = $result['premium_image'];
break;
default :
$img = $result['standard_image'];
break;
}
if (!$img) {
$img = 'blue-png2.png';
}
?>
<img src="<?php print $img; ?>" alt="" />
你可以在你认为合适的时候删除换行符......我将一贯的格式作为一个原则,你也应该这样做! :)
答案 5 :(得分:1)
$array = array(
array('/images/src1.png', 'yourClass', 'yourAlt'),
array('/images/src2.png', 'yourClass2', 'yourAlt2'),
array('/images/src3.png', 'yourClass3', 'yourAlt3'));
echo '<img src="'.$array[$_GET['class_id']][0].'" class="'.$array[$_GET['class_id']][1].'" alt="'.$array[$_GET['class_id']][2].'" />';
答案 6 :(得分:1)
我会把它放在一个块中并使用一些预定义的变量。有点干净,至少对我来说......
<?php
$p = "/pre_config/css/images/blue-png2.png";
$s = "/shop_possystems/image/";
switch ($_GET['class_id']) {
case 1:
$x = (empty($result['standard_image']) ? $p : "{$s}/{$result['standard_image']}";
break;
case 2:
$x = (empty($result['business_image']) ? $p : "{$s}/{$result['business_image']}";
break;
case 3:
$x = (empty($result['premium_image']) ? $p : "{$s}/{$result['standard_image']}";
break;
default:
$x = (empty($result['standard_image']) ? $p : "{$s}/{$result['standard_image']}";
break;
}
echo "<img src=\"{$x}\" alt=\"\" />";
&GT;
答案 7 :(得分:1)
...是
<?php
$a = array(0, 0, 'business_image', 'premium_image');
$t = $a[(int)$_GET['class_id']] || "standard_image";
echo "<img src=\"";
echo $result[$t] === "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result['standard_image']}";
echo "\" />";
?>
答案 8 :(得分:0)
您可以在案例陈述中回显您的img标签
答案 9 :(得分:0)
如果你想使用这个开关,我会建议如下。
<?php
$img_type = '';
switch ($_GET['class_id']) {
case 1:
$img_type = 'standard_image';
break;
case 2:
$img_type = 'business_image';
break;
case 3:
$img_type = 'premium_image';
break;
default:
$img_type = 'standard_image';
break;
}
$src = $result[$img_type] == "" ? "/pre_config/css/images/blue-png2.png" : "/shop_possystems/image/{$result[$img_type]}";
?>
<img src="<?php print $src ?>" alt="" />