Note that there are some explanatory texts on larger screens.

plurals
  1. POProperly echo a bunch of CSS options using PHP?
    primarykey
    data
    text
    <p>Consider a following function with <b>one option</b> echoed out:</p> <pre><code>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 '&lt;style type="text/css"&gt;'; } if($getheadercolor !='blank') { echo "\n"."#header a{ color:#$getheadercolor; }"; }//End If $getheadercolor if($getheadercolor !='blank'){ echo "\n".'&lt;/style&gt;'; } }// End Dynamic options </code></pre> <p>It outputs something like this into my header (<i>it works flawlessly and does exactly as I want</i>)</p> <pre><code>&lt;style type="text/css"&gt; #header a{ color:#30409b; } &lt;/style&gt; </code></pre> <p>Now here is the problem: This function will not have only one option but a <b>bunch of options</b> (20-30 options). So to illustrate my point let's say that for now my function will have <b>five</b> options. So it will look like this:</p> <pre><code>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 '&lt;style type="text/css"&gt;'; } 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".'&lt;/style&gt;'; } </code></pre> <p>So my function above <i>will work</i> but this part <br/><code>if($getheadercolor !='blank' || $sitecolor !='' || $textcolor !='' || $backgroundcolor !='' || $menucolor !='')</code> just seem wrong to me. <br/>Because this IF statement will have more than <b>20</b> options I am afraid that my code will be Slow and Inefficient.<br/>My PHP Force is not strong... so my only (not ideal) solution is to simply omit those two IF statement like so: </p> <pre><code> function dynamic_options() { $getheadercolor = get_header_textcolor(); $options_social = get_option('sandbox_theme_social_options'); $wrapper_background_color = get_option('wrapper_background_color'); echo '&lt;style type="text/css"&gt;'; 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".'&lt;/style&gt;'; }// End Dynamic options </code></pre> <p>Now my code <b>without</b> those IF statements <i>will work too</i> but now problem is, if there are no options my function will still echo an <b>empty</b> CSS style tag inside my header:<code>&lt;style type="text/css"&gt;&lt;/style&gt;</code> and that is something I don't want. <br/><br/>Can somebody give me an example or advice for making this function work better? <br/><br/>P.S. I am considering myself as PHP <i>noob</i> so if someone can give me a nice and clear advice or example it would be much appreciated! Thank You!!! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload