简体   繁体   中英

PHP - Alternate row colours based on a 'grouping' value in an array

There's probably an easy answer to this, but I just haven't been able to get my head around it.

I have the following array:

Array
(
    [0] => Array
        (
            [meeting_grouping] => 1
            [client_first_name] => Nikolay
        )

    [1] => Array
        (
            [meeting_grouping] => 1
            [client_first_name] => Konstantin
        )

    [2] => Array
        (
            [meeting_grouping] => 1
            [client_first_name] => Andrey
        )

    [3] => Array
        (
            [meeting_grouping] => 4
            [client_first_name] => Eddie
        )

    [4] => Array
        (
            [meeting_grouping] => 4
            [client_first_name] => Neil
        )

    [5] => Array
        (
            [meeting_grouping] => 6
            [client_first_name] => Ian
        )

    [6] => Array
        (
            [meeting_grouping] => 6
            [client_first_name] => Mark
        )
)

What I would like to do is output this data to an HTML table and colour code the rows. I know how to alternate the row colours, but in this case I need it to be colour coded based on the 'meeting_grouping'. Ie Nikolay, Konstantin and Andrey (meeting_grouping = 1) would have a grey background, Eddie and Neil (meeting_grouping = 4) would have a white background and Ian and Mark (meeting_grouping = 6) would have a grey background again.

If someone could point me in the right direction it would be much appreciated.

Thanks in advance!

In case you need arbitrary style for different groupings

When you output it to the HTML table, assign class names with the grouping number to the rows. Then you should just define styles for each group in CSS, of course with a default style, in case some new grouping appears.

For example, the table could come out to be:

<table>
    <tr class="meeting_grouping1">
        <td>Konstantin</td>
    </tr>
    <tr class="meeting_grouping1">
        <td>Andrey</td>
    </tr>
    <tr class="meeting_grouping4">
        <td>Eddie</td>
    </tr>
    <tr class="meeting_grouping4">
        <td>Neil</td>
    </tr>
    <tr class="meeting_grouping6">
        <td>Ivan</td>
    </tr>
    <tr class="meeting_grouping6">
        <td>Mark</td>
    </tr>
</table>

Then in CSS define your styles:

tr td
{
    background-color: white;
}

tr.metting_grouping1 td
{
    background-color: grey;
}

tr.metting_grouping4 td
{
    background-color: white;
}

tr.metting_grouping1 td
{
    background-color: grey;
}

In case you just need alternating colors for different groupings

When going over the array to prepare the table, check against the previous row's meeting_grouping value to see if it's the same. If it isn't the same, switch the style to the alternating one. In PHP it may look like this:

$use_alternate_style = false;
$previous_grouping = false;

foreach ($meeting_groupings as $entry)
{
    if ($entry['meeting_grouping'] != $previous_grouping)
    {
        // Inverses the alternate style indicator
        $use_alternate_style = !$use_alternate_style;
    }

    // Stores the grouping number for the next cycle
    $previous_grouping = $entry['meeting_grouping'];

    // Depending on whether $use_alternate_style is true or not, you
    // add a class name to the table row and then assign background colors
    // similarly as in the above HTML/CSS example.
}

You could create an array mapping the meeting_grouping to the corresponding colour, eg:

$colours = array(
  1 => 'grey',
  4 => 'white', 
  6 => 'grey'
);

Then when you iterate over your array just echo $colours[$meeting_grouping_var]; (where $meeting_grouping_var is the current meeting_grouping value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM