Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to set dynamic margin for string length in php

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'; 
        }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading