PHP切换案例多次返回

时间:2013-10-18 09:33:13

标签: php switch-statement

我目前有2个开关案例基本上使用相同的数据,但有不同的回报(一个开关案例返回图片网址,而另一个返回一个数字),我不认为这是非常好的做法(真的尝试有效编码),有没有机会将两个开关盒组合成一个开关盒?

他们是:

function getImage($image,$image_url,$count){
    switch ($image) {
        case 1:
            if($count == 1){
                return "images/menu_slider/960x200/".basename($image_url);
            }else if($count == 2){
                return "images/menu_slider/595x200/".basename($image_url);
            }else if($count == 6){
                return "images/menu_slider/480x200/".basename($image_url);
            }else{
                return "images/menu_slider/470x200/".basename($image_url);
            }
        break;
        case 2:
            if($count == 2){
                return "images/menu_slider/365x200/".basename($image_url);
            }else if($count == 3 || $count == 4  ||$count == 5  ||$count == 6   ){
                return "images/menu_slider/250x200/".basename($image_url);
            }else if ($count == 7){
                return "images/menu_slider/250x300/".basename($image_url);
            }else{
                return "images/menu_slider/250x400/".basename($image_url);
            }
        break;
        case 3:
            if($count == 3){
                return "images/menu_slider/240x200/".basename($image_url);
            }else{
                return "images/menu_slider/240x100/".basename($image_url);
            }
        break;
        case 4:
            return "images/menu_slider/240x100/".basename($image_url);
        break;
        case 5:
            if($count == 5){
                return "images/menu_slider/960x100/".basename($image_url);
            }else if($count == 6){
                return "images/menu_slider/480x100/".basename($image_url);
            }else if($count == 7){
                return "images/menu_slider/235x100/".basename($image_url);
            }else if($count == 8){
                return "images/menu_slider/235x200/".basename($image_url);
            }else{
                return "images/menu_slider/235x141/".basename($image_url);
            }
        break;
        case 6:
            if($count == 6){
                return "images/menu_slider/480x100/".basename($image_url);
            }else if($count == 7){
                return "images/menu_slider/235x100/".basename($image_url);
            }else if($count == 8){
                return "images/menu_slider/235x200/".basename($image_url);
            }else{
                return "images/menu_slider/235x141/".basename($image_url);
            }
        break;
        case 7:
            return "images/menu_slider/240x100/".basename($image_url);
        break;
        case 8:
            return "images/menu_slider/240x100/".basename($image_url);
        break;
        case 9:
            return "images/menu_slider/470x60/".basename($image_url);
        break;
    }
}

function getLinks($links,$count){
    switch ($links) {
        case 1:
            if($count == 1){
                //return "12 Links 4 Cols";
                return 12;
            }else{
                //return "12 Links 2 Cols";
                return 12;
            }
        break;

        case 2:
            if($count == 6){
                //return "6 Links";
                return 6;
            }else if($count == 7){
                //return "12 or 14 Links";
                return 12;
            }else{
                //return "12 Links";
                return 12;
            }
        break;

        case 3:
            if($count == 3){
                //return "6 Links";
                return 6;
            }else{
                //return "3 Links";
                return 3;
            }
        break;

        case 4:
            //return "3 Links";
            return 3;
        break;

        case 5:
            if($count == 5){
                //return "12 Links 4 col";
                return 12;
            }else if($count == 6){
                //return "6 Links 2 cols";
                return 6;
            }else if($count == 7){
                //return "3 Links";
                return 3;
            }else if($count == 8){
                //return "6 or 8 Links";
                return 6;
            }else{
                //return "4 Links";
                return 4;
            }
        break;

        case 6:
            if($count == 6){
                //return "6 Links 2 cols";
                return 6;
            }else if($count == 7){
                //return "3 Links";
                return 3;
            }else if($count == 8){
                //return "6 or 8 Links";
                return 8;
            }else{
                //return "4 Links";
                return 4;
            }
        break;

        case 7:
            //return "3 Links";
            return 3;
        break;

        case 8:
            //return "3 Links";
            return 3;
        break;

        case 9:
            //return "2 Links";
            return 2;
        break;
    }
}

他们被这样称呼:

$linkCount = getLinks($counter,$count);
getImage($image,$image_url,$count);

我不知道我是不是没必要,但这对我来说似乎不符合逻辑......

任何帮助都非常赞赏。

如果需要,这是整个文件:

<?php
$image_url = "";
//Conditional Images
function getLinks($links,$count){
    switch ($links) {
        case 1:
            if($count == 1){
                //return "12 Links 4 Cols";
                return 12;
            }else{
                //return "12 Links 2 Cols";
                return 12;
            }
        break;

        case 2:
            if($count == 6){
                //return "6 Links";
                return 6;
            }else if($count == 7){
                //return "12 or 14 Links";
                return 12;
            }else{
                //return "12 Links";
                return 12;
            }
        break;

        case 3:
            if($count == 3){
                //return "6 Links";
                return 6;
            }else{
                //return "3 Links";
                return 3;
            }
        break;

        case 4:
            //return "3 Links";
            return 3;
        break;

        case 5:
            if($count == 5){
                //return "12 Links 4 col";
                return 12;
            }else if($count == 6){
                //return "6 Links 2 cols";
                return 6;
            }else if($count == 7){
                //return "3 Links";
                return 3;
            }else if($count == 8){
                //return "6 or 8 Links";
                return 6;
            }else{
                //return "4 Links";
                return 4;
            }
        break;

        case 6:
            if($count == 6){
                //return "6 Links 2 cols";
                return 6;
            }else if($count == 7){
                //return "3 Links";
                return 3;
            }else if($count == 8){
                //return "6 or 8 Links";
                return 8;
            }else{
                //return "4 Links";
                return 4;
            }
        break;

        case 7:
            //return "3 Links";
            return 3;
        break;

        case 8:
            //return "3 Links";
            return 3;
        break;

        case 9:
            //return "2 Links";
            return 2;
        break;
    }
}
function getImage($image,$image_url,$count){
    switch ($image) {
        case 1:
            if($count == 1){
                return "images/menu_slider/960x200/".basename($image_url);
            }else if($count == 2){
                return "images/menu_slider/595x200/".basename($image_url);
            }else if($count == 6){
                return "images/menu_slider/480x200/".basename($image_url);
            }else{
                return "images/menu_slider/470x200/".basename($image_url);
            }
        break;
        case 2:
            if($count == 2){
                return "images/menu_slider/365x200/".basename($image_url);
            }else if($count == 3 || $count == 4  ||$count == 5  ||$count == 6   ){
                return "images/menu_slider/250x200/".basename($image_url);
            }else if ($count == 7){
                return "images/menu_slider/250x300/".basename($image_url);
            }else{
                return "images/menu_slider/250x400/".basename($image_url);
            }
        break;
        case 3:
            if($count == 3){
                return "images/menu_slider/240x200/".basename($image_url);
            }else{
                return "images/menu_slider/240x100/".basename($image_url);
            }
        break;
        case 4:
            return "images/menu_slider/240x100/".basename($image_url);
        break;
        case 5:
            if($count == 5){
                return "images/menu_slider/960x100/".basename($image_url);
            }else if($count == 6){
                return "images/menu_slider/480x100/".basename($image_url);
            }else if($count == 7){
                return "images/menu_slider/235x100/".basename($image_url);
            }else if($count == 8){
                return "images/menu_slider/235x200/".basename($image_url);
            }else{
                return "images/menu_slider/235x141/".basename($image_url);
            }
        break;
        case 6:
            if($count == 6){
                return "images/menu_slider/480x100/".basename($image_url);
            }else if($count == 7){
                return "images/menu_slider/235x100/".basename($image_url);
            }else if($count == 8){
                return "images/menu_slider/235x200/".basename($image_url);
            }else{
                return "images/menu_slider/235x141/".basename($image_url);
            }
        break;
        case 7:
            return "images/menu_slider/240x100/".basename($image_url);
        break;
        case 8:
            return "images/menu_slider/240x100/".basename($image_url);
        break;
        case 9:
            return "images/menu_slider/470x60/".basename($image_url);
        break;
    }
}
?>
<?php
    $document = JFactory::getDocument();
    $style= array();
?>
<?php foreach ($menu as $item): ?>
<?php 
    if($item->theme_title){
        $class = $item->theme_title;

        $style[] = '.'.$class.' > .cover {
            color:'.$item->foreground_text_color.';
            overflow: visible !important;
        }';

        $style[] = '.'.$class.' > .slide-background {
            background:'.$item->background_color.';
            color:'.$item->background_text_color.';
        }';

        $style[] = '.'.$class.' > .slide-background > ul > li > a{
            color:'.$item->background_text_color.';
        }';

        $style[] = '.'.$class.' > .cover > span:before {
            color:'.$item->icon_color.';
        }';
    }
?>
<?php endforeach; ?>

<?php foreach ($style as $css): ?>
    <?php $document->addStyleDeclaration( $css ); ?>
<?php endforeach; ?>


<div id="grid-menu-slider" class="menu-slider">
<?php foreach (array_chunk($menu, 9) as $menu): ?>
<?php $counter = 0; ?>
<?php $count = count($menu); ?>
<div class="grid-box">
    <div class="item-<?php echo $count ?>">
    <?php foreach($menu as $items): ?>
        <?php $counter++; ?>
        <?php if ($items->menu_image) {
                $image = $counter;
                $image_url = $items->menu_image;
                $background = "url(" . $theImage = getImage($image,$image_url,$count) .") no-repeat";
                $shadow = 'shadow';
            } else {
                $shadow = NULL;
                $background = $items->foreground_color.';';
            }
            $class = $items->theme_title;
            ?>
             <?php $linkCount = getLinks($counter,$count); ?>
                <div class="slide<?php echo $items->slide_direction; ?> box-<?php echo $counter ?> <?php echo $class; ?>">
                    <div class="cover <?php echo $shadow; ?>" style="background:<?php echo $background ?>;">
                        <span class="<?php echo 'icon-'.$items->icon; ?> <?php echo $shadow; ?>"><?php echo $items->menu_title; ?></span>
                    </div>
                    <div class="slide-background">
                        <?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
                        <ul>
                            <?php foreach($items as $submenu): ?>
                                <?php echo $submenu; ?>
                            <?php endforeach; ?>
                        </ul>
                        <?php endforeach; ?>
                    </div>
                </div>
    <?php endforeach; ?>
    </div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>

3 个答案:

答案 0 :(得分:3)

您可以创建一个用于构建每个网址字符串的函数:

            if($count == 1){
                return build_url("960x200",basename($image_url));
            }else if($count == 2){

                return build_url("595x200",basename($image_url));
            }else if($count == 6){
                return build_url("480x200",basename($image_url));
            }else{
                return build_url("470x200",basename($image_url));
            }

你的职能:

function build_url($size,$img_url)
{
   return strtr("images/menu_slider/{size}/{img_url}",array('{size}'=>$size,'img_url'=>$img_url));
}

通过这种方式,如果您将来需要重新创建网址字符串,您将执行一次而不是重写每个网址。

答案 1 :(得分:2)

我不知道如何做得更好,但我可以提出另一种选择。

您可以简单地使用关联数组。对于egs。:

$images[1][1] = "images/menu_slider/960x200/%s";
$images[1][2] = "images/menu_slider/595x200/%s";

依旧......

然后,要使用$ image为1和$ count为2来访问图像,您只需执行以下操作:

$url = sprintf($images[$image][$count],basename($image_url));

希望这会有所帮助......

答案 2 :(得分:1)

如果/ elseif / else使代码不可读,你的代码有点乱,太多了。 使用数组来管理:

function getImage($image,$image_url,$count=null)
{
$imgArray=array(
1=>array(1=>'960x200',2=>'595x200',6=>'480x200','other'=>'470x200'),
2=>array(2=>'365x200',3=>'250x200',4=>'250x200',5=>'250x200',6=>'250x200',7=>'250x300','other'=>'250x400'),
3=>array(3=>'240x200','other'=>'240x100'),
4=>'240x100',
5=>array(5=>'960x100',6=>'480x100',7=>'235x100',8=>'235x200','other'=>'235x141'),
6=>array(6=>'480x100',7=>'235x100',8=>'235x200','other'=>'235x141'),
7=>'240x100',
8=>'240x100',
9=>'470x60'
);

if(!is_array($imgArray[$image]) && isset($imgArray[$image]))
$folder=$imgArray[$image];
elseif(isset($imgArray[$image]) && isset($imgArray[$image][$count]))
$folder=$imgArray[$image][$count];
elseif(isset($imgArray[$image]))
$folder=$imgArray[$image]['other'];
else
return false;

return "images/menu_slider/{$folder}/".basename($image_url);

}

function getLinks($links,$count=null){
$linkArray=array(
1=>12,
2=>array(6=>6,'other'=>12),
3=>array(3=>6,'other'=>3),
4=>3,
5=>array(5=>12,6=>6,7=>3,8=>6,'other'=>4),
6=>array(6=>6,7=>3,8=>8,'other'=>4),
7=>3,
9=>3,
9=>2
);

if(!is_array($linkArray[$links]) && isset($linkArray[$links]))
return $linkArray[$links];
elseif(isset($linkArray[$links]) && isset($linkArray[$links][$count]))
return $linkArray[$links][$count];
elseif(isset($linkArray[$links]))
return $linkArray[$links]['other'];

return false;
}