0

How can I avoid the following if's by for loop .. ?? also, how can I generates the css on run time .. ??

if ($string == 12 && $string1==23) $style="r16"; 
if ($string == 34 && $string1==45) $style="r17"; 
if ($string == 45  && $string1== 56 ) $style="r18"; 
if ($string == 56 && $string1== 67) $style="r19"; 
if ($string == 67 && $string1== 78 ) $style="r20"; 
if ($string == 78 && $string1== 89 ) $style="r21"; 
if ($string == 89 && $string1== 910) $style="r22"; 
if ($string == 910 && $string1== 1011) $style="r23"; 
if ($string == 1011 && $string1== 1112) $style="r24"; 
if ($string ==  1112 && $string1== 1213) $style="r25"; 
if ($string == 1213 && $string1==1314 ) $style="r26"; 
if ($string == 1314 && $string1==1415 ) $style="r27"; 
if ($string == 1415 && $string1==1516 ) $style="r28"; 
if ($string == 1516 && $string1==1617 ) $style="r29"; 

CSS required .. as follow

.r16{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r17{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r18{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r19{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r20{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r21{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r22{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r23{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r24{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r25{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r26{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r27{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r28{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
.r29{ text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px; }
3
  • 1
    what's the point when all the css is the same? Commented Sep 2, 2013 at 23:53
  • CSS can be differt .. eg margin may be different .. Commented Sep 2, 2013 at 23:58
  • What is the point in doing it in PHP? What's wrong with having all of the CSS on the page at once? Can you set up a JS Fiddle demo of your page and explain what your CSS is doing wrong? Commented Sep 3, 2013 at 0:01

3 Answers 3

2

You can get rid of the ifs by using an array:

$styles = array('12,23' => 'r16',
                '34,45' => 'r17',
                ...
                '1516,1617' => 'r29'
               );
if (isset($styles["$string,$string1"])) {
    $style = $styles["$string,$string1"];
}

You could also use this array to generate the CSS. Make the values associative arrays containing the class name and CSS, e.g.

$styles = array('12,23' => array('class' => 'r16',
                                 'css' => 'text-align: left;padding-left: 100px; border: solid 1px #CCCCCC;  margin: 10px 490px 0px 285px;'),
    ...
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this:

$range_start_arr = array(12, 34, 45, 56, 67, 78, 89, 910, 1011, 1112, 1213, 1314, 1415, 1516);
$range_end_arr = array(23, 45, 56, 67, 78, 89, 910, 1011, 1112, 1213, 1314, 1415, 1516, 1617);
for (i = 16; $i <= 29; $i++)
{
    $k = $i - 16;
    if($string == $range_start_arr[$k] && $string1 == $range_end_arr[$k]) $style = 'r'.$i;
}

I'm assuming $string and $string1 are constants and you are just trying to shorten your code.

2 Comments

Arrays start at zero, you need to subtract 16 from $i.
Ok, it's an array now with for loop ... but I would prefer the "IF" way, if it's not hundreds of values/versions... better readable and maintainable imho
0

How can I avoid the following if's by for loop?

You can't! Where does $string and $string1 come from? Also, your css classes may not be calculated from $string(1), cause your "intervals" are not the same size nor linear.

how can I generates the css on run time?

Instead of calling a .css file, you just put an .php file there which echos the computed styles.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.