简体   繁体   English

PHP - 基于数组中“分组”值的备用行颜色

[英]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. 我想要做的是将这些数据输出到HTML表格并对行进行颜色编码。 I know how to alternate the row colours, but in this case I need it to be colour coded based on the 'meeting_grouping'. 我知道如何交替行颜色,但在这种情况下,我需要根据'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. 即Nikolay,Konstantin和Andrey(meeting_grouping = 1)将具有灰色背景,Eddie和Neil(meeting_grouping = 4)将具有白色背景并且Ian和Mark(meeting_grouping = 6)将再次具有灰色背景。

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. 将其输出到HTML表时,请将具有分组编号的类名分配给行。 Then you should just define styles for each group in CSS, of course with a default style, in case some new grouping appears. 然后你应该只为CSS中的每个组定义样式,当然还有默认样式,以防出现一些新的分组。

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: 然后在CSS中定义你的样式:

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. 当遍历数组以准备表时,检查前一行的meeting_grouping值以查看它是否相同。 If it isn't the same, switch the style to the alternating one. 如果不相同,请将样式切换为交替样式。 In PHP it may look like this: 在PHP中它可能看起来像这样:

$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: 您可以创建一个将meeting_grouping映射到相应颜色的数组,例如:

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

Then when you iterate over your array just echo $colours[$meeting_grouping_var]; 然后当你迭代你的数组时,只需echo $colours[$meeting_grouping_var]; (where $meeting_grouping_var is the current meeting_grouping value). (其中$meeting_grouping_var是当前的meeting_grouping值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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