3

i have this php block

<?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 } ?>

but this seems a bit messy..any suggestions

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321

10 Answers10

10

Why all the cases in the first place? You're just picking what image to use.

$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" />';

Added bonus: the purpose of the logic is clearer.

VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
3
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;
Michael J.V.
  • 5,499
  • 1
  • 20
  • 16
1

I agree, that code is crazy.

Write all the PHP code in a single <?php ?> block. When you have to print something, use the print function or echo construct.

Declare variables that can help you read and understand the code.

For example:

<?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...
}

?>

Isn't it a bit less messy?

gd1
  • 11,300
  • 7
  • 49
  • 88
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="" />

You don't need to open and close php tags every line.

Tesserex
  • 17,166
  • 5
  • 66
  • 106
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="" />

You can remove line breaks as you see fit... I format things consistently as a principle, and you should too ! :)

David Fells
  • 6,678
  • 1
  • 22
  • 34
  • +1 This is good but I don't like it relies on PHP duck typing (see `if (!$img)`) which looks smart but in fact is less clear. However, well done – gd1 May 06 '11 at 16:54
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].'" />';
Kavi Siegel
  • 2,964
  • 2
  • 24
  • 33
1

I would go with actually putting this in a block and use a few predefined variables. A little bit cleaner, at least to me...

<?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=\"\" />";

?>

Chad
  • 265
  • 1
  • 14
1

Yes...

<?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 "\" />";
?>
Ry-
  • 218,210
  • 55
  • 464
  • 476
0

You can echo your img tags within the case statement

Brett
  • 721
  • 2
  • 10
  • 20
0

If you want to use the switch, I'd suggest something like the following.

<?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="" />
Alex Jillard
  • 2,792
  • 2
  • 19
  • 20