Tuesday 15 March 2011

php - More efficient way to set a variable? -



php - More efficient way to set a variable? -

i have created if statement script sets variable based on number represents. after realizing script rather long , can done in more efficient way looked on net , did not find much results. here script

if ($row[position] == 1){ $variable = 1; } elseif ($row[position] == 2){ $variable = 1; } elseif ($row[position] == 3){ $variable = 2; } elseif ($row[position] == 4){ $variable = 2; } elseif ($row[position] == 5){ $variable = 3; } elseif ($row[position] == 6){ $variable = 3; } elseif ($row[position] == 7){ $variable = 4; } elseif ($row[position] == 8){ $variable = 4; } elseif ($row[position] == 9){ $variable = 5; } elseif ($row[position] == 10){ $variable = 5; } elseif ($row[position] == 11){ $variable = 6; } elseif ($row[position] == 12){ $variable = 6; } elseif ($row[position] == 13){ $variable = 7; } elseif ($row[position] == 14){ $variable = 7; } elseif ($row[position] == 15){ $variable = 8; } elseif ($row[position] == 16){ $variable = 8; }

as can see, it's pretty basic pattern.

1 = 1 2 = 1 3 = 2 4 = 2 5 = 3 6 = 3

and on...

i'm not looking work done, since may lot, want know how should go this. thanks.

this should work you

<?php $row["position"] = 4; //example //if (($row["position"] >= 1) && ($row["position"] <= 16) ) -> if want set variable if it's between 1 , 16 or else utilize if statement $variable = ceil($row["position"] / 2); echo $variable; ?>

php

No comments:

Post a Comment