Random color in foreach loop

4 posts by 2 authors in: Forums > CMS Builder
Last Post: September 11, 2017   (RSS)

By Mikey - September 9, 2017 - edited: September 9, 2017

Anyone have any suggestions on how to get a random rgba() color in this foreach loop?

<?php if ($tagsToCount): ?>
         <?php  $maximumResults = 12;
        $resultCount = 0;
?>
  <?php $totalTagCount = array_sum($tagsToCount); ?>
    <?php
        arsort($tagsToCount);
                $tagsToCount = array_slice($tagsToCount, 0, 99);
    foreach ($tagsToCount as $key => $value): ?>
                'rgba(255, 99, 132, 0.2)', <<<<// I need this to be a random results.
<?php if (++$resultCount == $maximumResults) { break;}?>

  <?php endforeach ?>
<?php endif ?>

Thanks Zicky

By Mikey - September 10, 2017

Got this figured out. Not going to say it's perfect, but it seems to be working so far.

<?php foreach ($namesRecords as $record): ?>
       <?php 
        {
             $r = rand(0,255); 
             $g = rand(0,255); 
             $b = rand(0,255); 
             $color = dechex($r) . dechex($g) . dechex($b);
        }
        ?>
             <?php echo "rgb(".$r.",".$g.",".$b.")"; ?>
  <?php endforeach ?>

Zicky

By Dave - September 11, 2017

Nice work! 

Dave Edis - Senior Developer
interactivetools.com