我目前有 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>