0

我目前有 2 个 switch case,它们基本上使用相同的数据,但返回不同(一个 switch case 返回一个图像 url,而另一个返回一个数字),我认为这不是很好的做法(真的试图有效地编码), 有没有机会将两个开关盒组合成一个开关盒?

他们来了:

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>
4

3 回答 3

2

You can create a function for building each url string :

            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));
            }

Your function :

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

In this way, if you need to remake your url string in the future you will do it one time instead of rewriting each url.

于 2013-10-18T09:55:11.440 回答
2

我不知道如何使它变得更好,但我可以提出一个替代方案。

您可以简单地使用关联数组。例如:

$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));

希望这可以帮助...

于 2013-10-18T09:42:39.003 回答
0

你的代码有点乱,if/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;
}
于 2013-10-18T10:09:26.303 回答