Home > database >  What's the best approach to display price ($$$) dynamically from table?
What's the best approach to display price ($$$) dynamically from table?

Time:10-16

I have a table field that stores the price range. (Out of 5) I want to display it in this manner. If the range is 3, then display $$$ and other 2 ($) muted. What is the best approach or what is this called? (Check the below image for reference)

enter image description here

CodePudding user response:

You could make use of for loop as follows:

$range = 3;

for($i=0; $i<5; $i  ){
    if($i < $range){
        echo "<strong>$</strong>";
    }else{
        echo "<span class='text-muted'>$</span>";
    }
}

and you have to add CSS like:

.text-muted {
    color: #777;
}

Update

You can also use it as a function:

<?php


function displayRangeAsDollar($range){
    
    $boldText = '';
    $mutedText = '';
    
    for($i=0; $i<5; $i  ){
        if($i < $range){
             $boldText .= '$';
        }else{
            $mutedText .= '$';
        }
    }

return  "<strong>".$boldText."</strong>"."<span class='text-muted'>".$mutedText."</span>";
    
}

echo displayRangeAsDollar(3);

  • Related