本文介绍了有条件地格式化HTML表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的Web应用程序,它从FileMaker数据库中获取数据并将其吐出为XML-> PHP-> HTML.

I have a custom web-app, getting data from a FileMaker database and spitting it out XML -> PHP -> HTML.

我目前正在一个大的FOR循环中生成一个表,并像这样回显结果:

I'm currently generating a table in a big FOR loop and echoing out the results like so:

echo '
<tr>
    <td><strong>Qty Approved</strong></td>
    <td><strong>' . $record['qty1 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty2 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty3 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty4 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty5 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty6 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty7 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty8 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty9 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty10 approved'][0] . '</strong></td>
    <td><strong>' . $record['qty11 approved'][0] . '</strong></td>
    <td>'. $approved_string . '</td>
</tr>

';

我想有条件地突出显示表中的某些值(这就是我使用该$ approved_string所做的事情),例如qty5批准值> 0,然后将其设置为红色,否则将其设置为绿色.

I want to conditionally highlight some of the values in the table (which is what I'm doing with that $approved_string) whereby if e.g. the qty5 approved value > 0 then make it red, else make it green.

我了解如何重新格式化表格以正确使用CSS,但是我不知道是否在像$ approved_string这样的表格回显之前预先计算值,或者是否可以/应该放置我的echo语句中的if语句?

I understand how to reformat the table to properly use CSS, but what I don't know is whether or not to pre-calculate the values before echoing the table like with that $approved_string OR if I can/should be placing an if statement within my echo statement?

推荐答案

重复的任务->创建函数:

Repeated tasks -> make a function:

function highlight_record_value($record, $qty_index) {
    $value = $record['qty'.$qty_index.' approved'][0];
    if ($qty_index == 5) {
        if ($value > 0)
            $color = 'red';
        else
            $color = 'green';

        return sprintf('<span style="color: %s;">%s</span>', $color, $value);
    }

    //anything else you want

    return $value;
}

echo '
<tr>
    <td><strong>Qty Approved</strong></td>
    <td><strong>' . highlight_record_value($record, 1) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 2) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 3) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 4) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 5) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 6) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 7) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 8) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 9) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 10) . '</strong></td>
    <td><strong>' . highlight_record_value($record, 11) . '</strong></td>
</tr>

';

这篇关于有条件地格式化HTML表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:31