douhuan1905 2010-01-05 05:42
浏览 70

PHP按年龄和颜色排序编码结果?

I have a program which presents a single random selection from the database each time it is run. What I would like to do is have the selections sorted by activity level and then have the selection title shown in a color which represents how active the random selection is.

For example: Item 1 is 45 days old, Item 2 is 61 days old and Item 3 is 10 days old.

The time range is: 1-45days (black), 46-60days (purple) and over 61days (blue)

I would like the PHP to sort the Items and when the program is run I would want the random item selected to be color coded so that: If Item 3 was chosen the text for the items title would be in color:#000000 If Item 1 was chosen the text for the items title would be in color:#770077 If Item 2 was chosen the text for the items title would be in color:#0000ff

The existing PHP program does not have any variable dealing with the age of the item or with coloring the items title. I am a almost complete novice with PHP (just bought Larry Ullman's book on the subject) so I do not even know if this can be done but I figured I would ask and see...

  • 写回答

2条回答 默认 最新

  • drctyr2869 2010-01-05 19:01
    关注

    You could try the following method. Note that I have reduced the min/max by assuming the next array index in the sequence is always greater than the previous range.

    // a mapping of ranges and their associated color
    $range_map = array(
        array('color' => '#000000', 'min' => 1, 'max' => 46), 
        array('color' => '#770077', 'min' => 46, 'max' => 61),
        array('color' => '#0000ff', 'min' => 61, 'max' => 1000000)
    );
    
    $output = '';
    foreach ($items as $item) {
        $color = '';
        foreach ($range_map as $range) {
            if ($item->days_old >= $range['min'] && $item->days_old < $range['max']) {
                $color = $range['color'];
                break;
            }
        }
    
        if (!empty($color)) {
            $output .= '<span style="color:' . $color . '">' . $YOUR_OUTPUT_HERE . '</span>';
        } else {
            $output .= $YOUR_OUTPUT_HERE;
        }
    }
    
    echo $output;
    

    This is not the fastest solution but it can handle any number of cases with minimal modification.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据