Answer the question
In order to leave comments, you need to log in
How can the code be optimized?
Hello. Wrote a small script and it really looks miserable. Can you advise what ways you can make the code more readable?
echo '<div class="currency"><ul>';
for ($i=0; $i < $count_obj ; $i++) {
if($obj[$i]['percent_change_24h'] > 0) {
$color_change = '#4ac06a';
$plus = '+';
} else {
$color_change = '#ff8d8d';
$plus = '';
}
echo '<li>';
echo '<i class="cc '.$obj[$i]["symbol"].' iconsi" title="'.$obj[$i]["name"].'"></i>';
echo '<div class="block_coin">';
echo '<span class="coin_name">'.$obj[$i]["name"].'</span><span style="color:'.$color_change.';" class="coin_price">'.$obj[$i]["price_usd"].'$</span>';
echo '<span style="color:'.$color_change.';" class="coin_change">(';
echo $plus.$obj[$i]["percent_change_24h"];
echo '%)</span></div></li>';
}
echo '</ul></div>';
Answer the question
In order to leave comments, you need to log in
If I understood everything correctly and did not make a mistake anywhere, then you can try this:
<div class="currency">
<ul>
<? foreach ($obj as $element):?>
<? $color = ($element['percent_change_24h'] > 0) '#4ac06a' : '#ff8d8d'; ?>
<? $plus = ($element['percent_change_24h'] > 0) '+' : ''; ?>
<li>
<i class="cc <?=$element["symbol"];?> iconsi" title="<?=$element["name"];?>"></i>
<div class="block_coin">
<span class="coin_name"><?=$element["name"];?></span>
<span style="color: '<?=$color;?>';" class="coin_price"><?=$element['price_usd'];?></span>
<span style="color: '<?=$color;?>';" class="coin_change">(<?=$plus;?><?=$element["percent_change_24h"];?>%)</span>
</div>
</li>
<? endforeach; ?>
</ul>
</div>
something like this
<div class="currency">
<ul>
<?php
for ( $i = 0; $i < $count_obj; $i++ ) {
if ( $obj[ $i ][ 'percent_change_24h' ] > 0 ) {
$color_change = '#4ac06a';
$plus = '+';
} else {
$color_change = '#ff8d8d';
$plus = '';
}
?>
<li><i class="cc <?=$obj[$i][" symbol "]?> iconsi" title="<?=$obj[$i][" name "]?>"></i>
<div class="block_coin">
<span class="coin_name">
<?=$obj[$i]["name"]?>
</span>
<span style="color:<?=$color_change?>;" class="coin_price">
<?=$obj[$i]["price_usd"]?>$</span>
<span style="color:<?=$color_change?>;" class="coin_change">(<?=$plus.$obj[$i]["percent_change_24h"]?>%)</span>
</div>
</li>
<?php
}
?>
</ul>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question