简体   繁体   中英

PHP - Get and compare row value from a different column for the same row

I'm attempting to edit a custom function and struggling to get the row value for a particular custom column ('rank_td') to compare against its other columns (rank_lw and rank_lm), all inside a HTML table.

Tried a fair few variations and can't get it going.

Any ideas?

function custom_value($cellValue, $dataColumnHeader, $rank_td_value) {

if($dataColumnHeader == "rank_lw" || $dataColumnHeader == 'rank_lm'){

    $row['rank_td']->$cellValue = $rank_td_value;

    if($rank_td_value == $cellValue){
                $styleColor = 'color:blue;';
             }else if($rank_td_value < $cellValue){ 
                $styleColor = 'color:green;';
             }else{
                $styleColor = 'color:red;';
             } 
        return $class_name.'<span style="'.$styleColor.'">'.$cellValue.'</span>';
}

return $cellValue; }

样本数据

You are not calling the global variable $row which exists outside the scope of this function - hence my comment that it doesn't look like it belongs here. If you want to be able to access a variable from outside a function you need to either pass that variable in, or declare it using the global keyword. Here is a very basic example of this:

$row['some_value'] = "value1";

function scopeTest($var1) {
    $row['some_value'] = $var1;//local variable $row created
}
function scopeTestTwo($var1) {
    global $row;//variable outside the function
    $row['some_value'] = $var1;
}

scopeTest("jam");
print_r($row);//Array ( [some_value] => value1 )
scopeTestTwo("jam");
print_r($row);//Array ( [some_value] => jam )

in your code you also have this

return $class_name.'<span....

but $classname is not defined so either it is redundant and you should remove it (because this will cause a php notice and anyway redundant code is, well, redundant) or it should be defined somewhere which means something has been missed out

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