I am trying to set margin for dynamic string length: My Max String Length is 41. i am trying code like that but how can it possible for string length 41. the condition is when count is added in 3 then margin will minus by -5px
$set_margin_left = '335px';
if(strlen($category['name']) == 3){
$cal_margin = $set_margin_left;
}elseif(strlen($category['name']) == 4){
$cal_margin = '330px';
}elseif(strlen($category['name']) == 5){
$cal_margin = '325px';
}elseif(strlen($category['name']) == 6){
$cal_margin = '320px';
}elseif(strlen($category['name']) == 7){
$cal_margin = '315px';
}elseif(strlen($category['name']) == 8){
$cal_margin = '310px';
}elseif(strlen($category['name']) == 9){
$cal_margin = '305px';
}elseif(strlen($category['name']) == 10){
$cal_margin = '300px';
}elseif(strlen($category['name']) == 11){
$cal_margin = '295px';
}elseif(strlen($category['name']) == 12){
$cal_margin = '290px';
}elseif(strlen($category['name']) == 13){
$cal_margin = '285px';
}elseif(strlen($category['name']) == 14){
$cal_margin = '280px';
}
CodePudding user response:
A simple mathematic function as you 're beginning at 350px
that result from 335 3 * 5 from your first if condition.
$maxMargin = 350;
$calculatedMargin = $maxMargin - strlen($category) * 5;
$cssValue = $calculatedMargin . 'px';
Assuming that the category string length is 3 the margin would be 335. Assuming the category string length is 14 the margin would be 280 (250 - 5 * 14).