根据表数据编号对不同颜色进行排序 [英] According the table data number to sort different color

查看:61
本文介绍了根据表数据编号对不同颜色进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了该表以在页面中显示数据库表中的数据.我的问题是如何根据表数据前缀号对不同颜色进行排序?

I am created the table to show the data from the database table in the page. My problem is how to according the table data prefix number to sort different color?

下面是我的输出:

我希望前缀号在范围号以下:

I want the prefix number follow below the number of range:

Prefix number |      Color 
--------------------------
100 until 199 |      Blue
200 until 299 |      Red
300 until 399 |      Yellow
400 until 499 |      Orange
500 until 599 |      Green

实际上,我希望输出与示例图片下方相同,示例图片下方未使用编码来实现,只是我使用了画图软件进行编辑,使您轻松理解我想要的内容输出:

Actually I want the output same like below the sample picture, below the sample picture is not using coding to make it, just I am used the paint software to edit and make it easy to let yours understand what I want to get the output:

下面是我的编码:

<table id="" class="table table-bordered table-condensed table-hover table-striped dataTable">
                        <thead>
                            <tr>
                                <th style='width:7%'>No</th>
                                <th class='filter_text_field'>Name</th>
                                <th class='filter_text_field'>Folder Location</th>
                                <th class='filter_text_field'>Category</th>
                                <th class='filter_text_field'>Request By</th>
                                <th class='filter_text_field'>Date Request</th>
                                
                                <!--<th class='filter_text_field'>Date Request</th>
                                <th class='filter_text_field'>Folder Name</th>
                                <th class='filter_text_field'>Category</th>
                                <th class='filter_text_field'>Folder Location</th>
                                <th class='filter_text_field'>Status_1</th>
                                <th class='filter_text_field'>Email Address</th>-->
                                <th class='filter_text_field'>Status</th>
                                <?php if ($module_user_permission['edit'] == 1) { ?>
                                <th style='width:15%'>Action</th>
                                <?php } ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $sql_wtp = "select * from  filing_code_management";
                            $query_wtp = db_conn_select($sql_wtp);
                            foreach ($query_wtp as $rs_wtp) {
                                if ($rs_wtp['status'] == 1) {
                                    $active = 'Active';
                                } elseif ($rs_wtp['status'] == 0) {
                                    $active = 'Inactive';
                                }
                                echo "<tr>";
                                echo "<td>" . (++$no) . "</td>";
                                echo "<td>" . $rs_wtp['name'] . "</td>";
                                echo "<td>" . $rs_wtp['folder_location'] . "</td>";
                                echo "<td>" . $rs_wtp['category'] . "</td>";
                                echo "<td>" . $rs_wtp['request_by'] . "</td>";
                                echo "<td>" . $rs_wtp['date_request'] . "</td>";
                                echo "<td>" . $active . "</td>";
                                if ($module_user_permission['edit'] == 1) {
                                    echo '<td><a href="#wtp_modal" onclick="select_(\'' . md5($rs_wtp['id'].$md5) . '\',\'wtp_content\')" data-toggle="modal" data-original-title="Help" class="btn btn-sm btn-primary" data-color-format="hex">Update</a>';
                                    if ($rs_wtp['status'] == 1) {
                                            echo '<a onclick="delete_(\'' . md5($rs_wtp['id'].$md5) . '\',1)" class="btn btn-sm btn-primary" data-color-format="hex">Deactivate</a>';
                                        } elseif ($rs_wtp['status'] == 0) {
                                            echo '<a onclick="delete_(\'' . md5($rs_wtp['id'].$md5) . '\',0)" class="btn btn-sm btn-primary" data-color-format="hex">Activate</a>';
                                        }
                                    echo '</td>';
                                }
                                echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>

我尝试过的事情:

我做了一个测试数组:$ rs_wtp = array('name'=> array('100-1 Sometext','233-1 Sometext','500 Sometext'),它可以在数组中显示正确在小提琴中: http://sandbox.onlinephpfunctions.com/code/9938a5e6a88de29511d22458a23a39b不知道如何将此测试代码与我上面的代码结合起来,以根据表中的前缀号显示正确的颜色.以下是我的测试数组代码:

I have made a test array: $rs_wtp = array( 'name' => array( '100-1 Sometext', '233-1 Sometext', '500 Sometext' ), it can show correct in the array in the Fiddle: http://sandbox.onlinephpfunctions.com/code/9938a5e6a88de29511d22458a23a39b3a2368995. But I don't know how to combine this test code to my above coding to show the correct color according prefix number inside the table. Below is my testing array code:

<?php
$rs_wtp = array(
    'name' => array(
        '100-1 Sometext',
        '233-1 Sometext',
        '500 Sometext',
        '300 Sometext',
        '300 Sometext',
        '200 Sometext',
        '110-4 Sometext',
        '150 Sometext',
        '455 Sometext',
        '500 Sometext',
        '199 Sometext'
    )  
);
$colors = array (
    1 => "blue",   //--> for all colors within the range of 100-199
    2 => "red",    //--> for all colors within the range of 200-299
    3 => "yellow", //--> for all colors within the range of 300-399
    4 => "purple", //--> for all colors within the range of 400-499
    5 => "green"   //--> for all colors within the range of 500-599
);
$stmt = 'Rules:<br>
1 => "blue"   //--> for all colors within the range of 100-199
2 => "red"    //--> for all colors within the range of 200-299
3 => "yellow" //--> for all colors within the range of 300-399
4 => "purple" //--> for all colors within the range of 400-499
5 => "green"  //--> for all colors within the range of 500-599

';


foreach($rs_wtp['name'] as $name => $valuetocheck){
    $check = $valuetocheck[0];
    $stmt .= '<div style="color:'; 
    foreach ($colors as $key => $color){            
        if($check == $key){
            $stmt .= $color;
        }
    }$stmt .= ';">'.$valuetocheck.'</div>';
}
echo $stmt;




希望有人可以指导我或给我一些想法,以使其能够根据表格页面中的前缀号显示颜色.谢谢.

Hope someone can guide me or give me some ideas to make it work to show the color according the prefix number in the table page. Thanks.

推荐答案

您可以创建映射,使用范围的映射来分析数字,并将颜色名称用作CSS类.

You can create a mapping, analyze the number with the mappings of a range and use the colorname as a CSS class.

如果未找到,则默认为黑色.

Default is black, if none was found.

// create a map with FROM, TO and COLOR
$colorRanges = [
    [100, 199, 'blue'],
    [200, 299, 'red'],
    [300, 399, 'yellow'],
    [400, 499, 'orange'],
    [500, 599, 'green'],
];

$rs_wtp = array(
    'name' => array(
        '100-1 Sometext',
        '233-1 Sometext',
        '500 Sometext',
        '300 Sometext',
        '300 Sometext',
        '200 Sometext',
        '110-4 Sometext',
        '150 Sometext',
        '455 Sometext',
        '500 Sometext',
        '199 Sometext'
    )
);

foreach ($rs_wtp['name'] as $text) {

    // default
    $colorClass = 'black';

    // check for the number at the beginning of the line
    if (preg_match('/^(\d+)/', $text, $matches)) {
        $id = $matches[1];
        // search for the number in the mapping array
        foreach ($colorRanges as $range) {
            list($from, $to, $className) = $range;
            if ($id >= $from && $id <= $to) {
                $colorClass = $className;
                break;
            }
        }
    }

    // colored output
    echo "<div class='$colorClass'>$text</div>", PHP_EOL;
}

输出

<div class='blue'>100-1 Sometext</div>
<div class='red'>233-1 Sometext</div>
<div class='green'>500 Sometext</div>
<div class='yellow'>300 Sometext</div>
<div class='yellow'>300 Sometext</div>
<div class='red'>200 Sometext</div>
<div class='blue'>110-4 Sometext</div>
<div class='blue'>150 Sometext</div>
<div class='orange'>455 Sometext</div>
<div class='green'>500 Sometext</div>
<div class='blue'>199 Sometext</div>

更新

从您的注释中了解如何实现您的代码.只需放在< tbody><?php 之后.

<?php
$colorRanges = [
    [100, 199, 'blue'],
    [200, 299, 'red'],
    [300, 399, 'yellow'],
    [400, 499, 'orange'],
    [500, 599, 'green'],
];

$sql_wtp = "select * from  filing_code_management";
$query_wtp = db_conn_select($sql_wtp);
foreach ($query_wtp as $rs_wtp) {
    if ($rs_wtp['status'] == 1) {
        $active = 'Active';
    } elseif ($rs_wtp['status'] == 0) {
        $active = 'Inactive';
    }

    $colorClass = 'black';

    // check for the number at the beginning of the line
    if (preg_match('/^(\d+)/', $rs_wtp['name'], $matches)) {
        $id = $matches[1];
        // search for the number in the mapping array
        foreach ($colorRanges as $range) {
            list($from, $to, $className) = $range;
            if ($id >= $from && $id <= $to) {
                $colorClass = $className;
                break;
            }
        }
    }

    echo "<tr>";
    echo "<td>" . (++$no) . "</td>";
    echo "<td style='color:{$colorClass}'>" . $rs_wtp['name'] . "</td>";
    echo "<td style='color:{$colorClass}'>" . $rs_wtp['folder_location'] . "</td>";
    echo "<td>" . $rs_wtp['category'] . "</td>";
    echo "<td>" . $rs_wtp['request_by'] . "</td>";
    echo "<td>" . $rs_wtp['date_request'] . "</td>";
    echo "<td>" . $active . "</td>";
    if ($module_user_permission['edit'] == 1) {
        echo '<td><a href="#wtp_modal" onclick="select_(\'' . md5($rs_wtp['id'] . $md5) . '\',\'wtp_content\')" data-toggle="modal" data-original-title="Help" class="btn btn-sm btn-primary" data-color-format="hex">Update</a>';
        if ($rs_wtp['status'] == 1) {
            echo '<a onclick="delete_(\'' . md5($rs_wtp['id'] . $md5) . '\',1)" class="btn btn-sm btn-primary" data-color-format="hex">Deactivate</a>';
        } elseif ($rs_wtp['status'] == 0) {
            echo '<a onclick="delete_(\'' . md5($rs_wtp['id'] . $md5) . '\',0)" class="btn btn-sm btn-primary" data-color-format="hex">Activate</a>';
        }
        echo '</td>';
    }
    echo "</tr>";
}

这篇关于根据表数据编号对不同颜色进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆