1
$legend = array();
$color[] = ('000000', 'ff0000', 'fasd3f');
    foreach ($stats as $row) {

        if ($row->countofmsg > 0) {

            array_push($legend, "<div class='legend_label'><div class='color-block' style='background:#".$color.";'></div>".ucwords($row->msg)."<div class='legend_count'>$row->count</div></div><div class='clear'></div>");

        }

    }

Here is my code, what I would like to do is set an array of colors $color[], and then inside the foreach loop array, call the first color in the array and then call the second and third and so forth with each thing the foreach spits out. And then repeat at the beginning of the colors array when it reaches the last color in the array.

Would kick out something like:

(color1) msg - count
(color2) msg - count
(color3) msg - count
etc..

Please let me know if there is a duplicate question, I tried researching for it.

3 Answers 3

2

This assumes that your $stats array is integer indexed

$legend = array();
$color[] = ('000000', 'ff0000', 'fasd3f');
$colorCount = count($color);
foreach ($stats as $k => $row) {
    if ($row->countofmsg > 0) {
            $legend[] = "<div class='legend_label'><div class='color-block' style='background:#".$color[ ($k % $colorCount) ].";'></div>".ucwords($row->msg)."<div class='legend_count'>$row->count</div></div><div class='clear'></div>";
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Jamund 'cause its the "best" way :P
@K_G no problem, glad we could help.
I was stuck at the I++ area and how to make it repeat.
2
$legend = array();
$color[] = ('000000', 'ff0000', 'fasd3f');
$colorSize = count($color);

foreach ($stats as $row) {
    if ($row->countofmsg > 0) {
        array_push($legend, "<div class='legend_label'><div class='color-block' style='background:#".$color[(3 % $colorSize)].";'></div>".ucwords($row->msg)."<div class='legend_count'>$row->count</div></div><div class='clear'></div>");
    }
}

Edited by @watcher advise but I know the right answer(s) were posted much before.

4 Comments

What happens when $i gets to be 3? $color[3] is not defined.
@watcher : I thought that there will be as many colors as many rows is present... That was a bad thought... Sorry for this.
@shaddyyx no worries. you can easily make your code work, just grab the count of the colors array before you enter the loop and instead of referencing $color[$i], reference $color[($i % $colorSize)] instead.
@watcher Edited but I'm not very happy with this one... I'd rather delete it :-)
1

Use modulo as explained here: https://stackoverflow.com/a/7237074/496735

Except in your case do %3 instead of %2

$color = $colors[$i % 3]; // where $i is the current row

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.