2013-08-27 12 views
0

考慮一個下面的函數與一個選項呼應了:正確地回顯一堆使用PHP的CSS選項?

function dynamic_options() { 
$getheadercolor = get_header_textcolor(); 
$options_social = get_option('sandbox_theme_social_options'); 
$wrapper_background_color = get_option('wrapper_background_color'); 

if($getheadercolor !='blank'){ 
echo '<style type="text/css">'; 
} 

    if($getheadercolor !='blank') { 
    echo "\n"."#header a{ 
    color:#$getheadercolor; 
    }";  
    }//End If $getheadercolor 

    if($getheadercolor !='blank'){ 
     echo "\n".'</style>'; 
    } 

}// End Dynamic options 

它輸出這樣的事情在我的頭(它完美的作品,準確地做,因爲我想

<style type="text/css"> 

#header a{ 

color:#30409b; 
} 
</style> 

現在這裏是問題:這個功能不會有隻有一個選項,但一堆選項(20-30選項)。所以爲了說明我的觀點,讓我們說現在我的功能將有五個選項。因此,它看起來就像這樣:

function dynamic_options() { 
$getheadercolor = get_header_textcolor(); 
$options_social = get_option('sandbox_theme_social_options'); 
$wrapper_background_color = get_option('wrapper_background_color'); 

//My main "problem" is an IF Statement below because it will look like a mess 
//With 20 options or more and with all those OR inside it... 
if($getheadercolor !='blank' || $sitecolor !='' || $textcolor !='' 
|| $backgroundcolor !='' || $menucolor !=''){ 
echo '<style type="text/css">'; 
} 

    if($getheadercolor !='blank') { 
    echo "\n"."#header a{ 
    color:#$getheadercolor; 
    }";  
    }//End If $getheadercolor 

    if($sitecolor !='blank') { 
    echo "\n"."#wrapper{  
    background-color:#$sitecolor; 
    }";  
    }//End If $sitecolor 

    if($textcolor !='blank') { 
    echo "\n".".entry p{  
    color:#$textcolor; 
    }";  
    }//End If $textcolor 

    if($backgroundcolor !='blank') { 
    echo "\n"."body{  
    background-color:#$backgroundcolor; 
    }";  
    }//End If $backgroundcolor 

    if($menucolor !='blank') { 
    echo "\n".".nav{  
    background-color:#$menucolor; 
    }";  
    }//End If $menucolor 

    //So to even close my style tag i need a bunch of those statments 
    if($getheadercolor !='blank' || $sitecolor !='' || $textcolor !='' 
     || $backgroundcolor !='' || $menucolor !=''){ 
     echo "\n".'</style>'; 
    } 

所以我上面的功能將工作但這部分
if($getheadercolor !='blank' || $sitecolor !='' || $textcolor !='' || $backgroundcolor !='' || $menucolor !='')似乎只是我錯了。
因爲這個IF語句將有多於選項,所以我恐怕我的代碼會變得緩慢和低效。
我的PHP力不強......所以我唯一的(不理想)的解決方案是簡單地忽略這兩個IF語句,像這樣:

 function dynamic_options() { 
    $getheadercolor = get_header_textcolor(); 
    $options_social = get_option('sandbox_theme_social_options'); 
    $wrapper_background_color = get_option('wrapper_background_color'); 
    echo '<style type="text/css">';  

if($getheadercolor !='blank') { 
    echo "\n"."#header a{ 
    color:#$getheadercolor; 
    }";  
    }//End If $getheadercolor 

    if($sitecolor !='blank') { 
    echo "\n"."#wrapper{  
    background-color:#$sitecolor; 
    }";  
    }//End If $sitecolor 

    if($textcolor !='blank') { 
    echo "\n".".entry p{  
    color:#$textcolor; 
    }";  
    }//End If $textcolor 

    if($backgroundcolor !='blank') { 
    echo "\n"."body{  
    background-color:#$backgroundcolor; 
    }";  
    }//End If $backgroundcolor 

    if($menucolor !='blank') { 
    echo "\n".".nav{  
    background-color:#$menucolor; 
    }";  
    }//End If $menucolor 

    echo "\n".'</style>'; 
}// End Dynamic options 

現在我的代碼沒有那些IF語句將工作太但現在的問題是,如果沒有選項,我的函數仍然會在我的頭文件中回顯一個空的 CSS樣式標記:<style type="text/css"></style>,這是我不想要的。

有人可以給我一個例子或建議,使這個功能更好地工作?

P.S.我正在考慮自己作爲PHP noob所以如果有人可以給我一個很好和明確的建議或例子,它將不勝感激!謝謝!!!

+0

使用[less](http://lesscss.org/)和[php](http://leafo.net/lessphp/)? – Prasanth

回答

1

而不是if(){}語句我只使用switch(){case ..}語句,因爲使用您提供的代碼看起來更整齊。

<?php 
    function dynamic_options() { 
     echo "<style type='text/css'> 
     $css = array('headercolor'=>'blank', 
        'options_social'=>'blank', 
        'wrapper_background_color'=>'blank'); 
     $css['headercolor'] = 'c0c0c0'; // get_header_textcolor(); 
     //$css['options_social'] = 'dont set this one'; //get_option('sandbox_theme_social_options'); 
     $css['wrapper_background_color'] = 'f00'; //get_option('wrapper_background_color'); 

    foreach($css as $itm => $value) { 
     if($value != 'blank') { 
      switch($itm) { 
       case 'headercolor'    : echo "\n"."#header a{color:#{$value};}"; break; 
       case 'wrapper_background_color' : echo "\n"."#wrapper{background-color:#{$value};}"; break; 
      } 
     } 
    } 
    echo "</style>"; 
    } 
?> 

如果最終輸出是因爲它對瀏覽器沒有什麼區別,那並不重要。

感謝賞金的提供,但我會對其進行降雨檢查 - 有一天我可能需要您的幫助。

+0

謝謝傑夫我從來沒有使用過任何foreach語句,現在我看起來更加清楚,我從中學到了一些新的東西:) –

0

也許你可以把樣式放入一個變量中。執行if語句後,您可以檢查變量是否爲空。如果沒有,您可以打印樣式標籤。

1

你有沒有想過使用關聯數組的選項:

$css = array('headercolor'=>'', 'options_social'=>'' ....); 

設置爲你現在做的值:

$getheadercolor = get_header_textcolor(); 
$options_social = get_option('sandbox_theme_social_options'); 
$wrapper_background_color = get_option('wrapper_background_color'); 

然後通過使用foreach值迭代()語句。這樣你就可以得到屬性名稱和價值。

+0

是的,我想這樣做,但我並不熟悉PHP這麼多...如果你可以澄清一點,我怎麼能做一個foreach()循環爲我的例子,我會很樂意獎勵你一個賞金(50) ; –

1

您應該將選項存儲到關聯數組中並移除包含'空白'或''字符串的項目。所以,如果數組不爲空,就回顯。

$options = array(); 
$options['header_color'] = get_header_color(); 
$options['text_color'] = get_text_color(); 
$remove = array('','blank'); 
$options = array_diff($options, $remove); 
if (!empty($options)) { 
echo '<style type="text/css">'; 
if(isset($options['header_color'])) { 
    echo "\n"."#header a{ 
    color:#".$options['header_color']."; 
    }";  
} 
if(isset($options['text_color'])) { 
    echo "\n".".entry p{ 
    color:#".$options['text_color']."; 
    }";  
} 
    echo '</style>'; 
} 
+0

一個好的答案值得一些獎勵。 Thx哥們! –