问题

我有一个完整的脚本,显示存储销售结果.在结果中,我有多个参数,我需要更改颜色.以下适用于一个参数,但如何让它在多个标准下执行?说我想更改TMP,但是包和gp智能?

 <td width="75px" id="profit">'.$data["tmppercent"].'</td>
 

 <td width="75px" id="profit">'.$data["bundlepercent"].'</td>
 

完整的脚本是:

  $count = $db->exec($sql);
                $sql = "Select * from BevWhiteboard ";
                $result = $db->query($sql);
                echo "<table class='fixed_header' width='1080'>";
                echo "<thead background='black'>";
                Echo "<th id='man' width='100px'>Employee Name</th>";
                Echo "<th class='model' width='100px'>GP</th>";
                Echo "<th class='desc' width='75px'>Activations</th>";
                Echo "<th class='ingram' width='75px'>Upgrades</th>";
                Echo "<th class='verizon' width='75px'>Smartphones</th>";
                Echo "<th class='mp' width='75px'>GP / Smart</th>";
                Echo "<th class='dpmax' width='75px'>Acc. GP / Smart</th>";
                Echo "<th class='percent' width='75px'>Pullthrough %</th>";
                Echo "<th class='netprofit' width='75px'>TMP %</th>";
                Echo "<th class='netprofit' width='75px'>Bundle %</th>";
                Echo "<th class='netprofit' width='75px'>HUM</th>";
                echo "</tr></thead>";
                echo "<tbody id='imwireless' align='center' border='1'>";

                foreach ($result as $data) {
                    if($data["tmppercent"]<"75"){
                        echo '

                       <tr >
                       <td id="man" width="100px">'.$data["Employee_Name1"].'</td>
                       <td id="model" width="100px">'.$data["GP"].'</td>
                       <td width="75px">'.$data["activations"].'</td>
                       <td width="75px">'.$data["upgrades"].'</td>
                       <td width="75px">'.$data["smartphone"].'</td>
                       <td width="75px">'.$data["gp_smart2"].'</td>
                       <td width="75px">'.$data["accessorygp"].'</td>
                       <td width="75px">'.$data["pullthroughpercent"].'</td>
                       <td width="75px" id="tmp"><font color="red">'.$data["tmppercent"].'</td>
                       <td width="75px" id="tmp">'.$data["bundlepercent"].'</td>
                       <td width="75px" id="profit">'.$data["HUM_Devices"].'</td>
                       ';
                    }  
                    echo '

                       <tr >
                       <td id="man" width="100px">'.$data["Employee_Name1"].'</td>
                       <td id="model" width="100px">'.$data["GP"].'</td>
                       <td width="75px">'.$data["activations"].'</td>
                       <td width="75px">'.$data["upgrades"].'</td>
                       <td width="75px">'.$data["smartphone"].'</td>
                       <td width="75px">'.$data["gp_smart2"].'</td>
                       <td width="75px">'.$data["accessorygp"].'</td>
                       <td width="75px">'.$data["pullthroughpercent"].'</td>
                       <td width="75px" id="profit">'.$data["tmppercent"].'</td>
                       <td width="75px" id="profit">'.$data["bundlepercent"].'</td>
                       <td width="75px" id="profit">'.$data["HUM_Devices"].'</td>
                       ';     
                }
                echo "</tr></tbody></table>";
 

因此,如果TMP低于75%,它会返回true.这很好用.如果TMP低于75,如果TMP红色,如果HUM = 0转这些红色等,我怎么能得到它.

enter image description here

正如您在我的图像中看到的,我使用了其他if,它正确地颜色了HUM,但删除了第一个if的位置.

 $count = $db->exec($sql);
                $sql = "Select * from BevWhiteboard ";
                $result = $db->query($sql);
                echo "<table class='fixed_header' width='1080'>";
                echo "<thead background='black'>";
                Echo "<th id='man' width='100px'>Employee Name</th>";
                Echo "<th class='model' width='100px'>GP</th>";
                Echo "<th class='desc' width='75px'>Activations</th>";
                Echo "<th class='ingram' width='75px'>Upgrades</th>";
                Echo "<th class='verizon' width='75px'>Smartphones</th>";
                Echo "<th class='mp' width='75px'>GP / Smart</th>";
                Echo "<th class='dpmax' width='75px'>Acc. GP / Smart</th>";
                Echo "<th class='percent' width='75px'>Pullthrough %</th>";
                Echo "<th class='netprofit' width='75px'>TMP %</th>";
                Echo "<th class='netprofit' width='75px'>Bundle %</th>";
                Echo "<th class='netprofit' width='75px'>HUM</th>";
                echo "</tr></thead>";
                echo "<tbody id='imwireless' align='center' border='1'>";

                foreach ($result as $data) {
                    if($data["tmppercent"] <"70"){
                        echo '

                       <tr >
                       <td id="man" width="100px">'.$data["Employee_Name1"].'</td>
                       <td id="model" width="100px">'.$data["GP"].'</td>
                       <td width="75px">'.$data["activations"].'</td>
                       <td width="75px">'.$data["upgrades"].'</td>
                       <td width="75px">'.$data["smartphone"].'</td>
                       <td width="75px">'.$data["gp_smart2"].'</td>
                       <td width="75px">'.$data["accessorygp"].'</td>
                       <td width="75px">'.$data["pullthroughpercent"].'</td>
                       <td width="75px" id="tmp"><font color="red">'.$data["tmppercent"].'%</td>
                       <td width="75px" id="tmp">'.$data["bundlepercent"].'</td>
                       <td width="75px" id="profit">'.$data["HUM_Devices"].'</td>
                       ';
                    }  

                    elseif($data["HUM_Devices"] < "1"){
                        echo '

                       <tr >
                       <td id="man" width="100px">'.$data["Employee_Name1"].'</td>
                       <td id="model" width="100px">'.$data["GP"].'</td>
                       <td width="75px">'.$data["activations"].'</td>
                       <td width="75px">'.$data["upgrades"].'</td>
                       <td width="75px">'.$data["smartphone"].'</td>
                       <td width="75px">'.$data["gp_smart2"].'</td>
                       <td width="75px">'.$data["accessorygp"].'</td>
                       <td width="75px">'.$data["pullthroughpercent"].'</td>
                       <td width="75px" id="tmp">'.$data["tmppercent"].'%</td>
                       <td width="75px" id="tmp">'.$data["bundlepercent"].'</td>
                       <td width="75px" id="profit"><font color="red">'.$data["HUM_Devices"].'</td>
                       ';
                    }  

                    else{
                    echo '

                       <tr >
                       <td id="man" width="100px">'.$data["Employee_Name1"].'</td>
                       <td id="model" width="100px">'.$data["GP"].'</td>
                       <td width="75px">'.$data["activations"].'</td>
                       <td width="75px">'.$data["upgrades"].'</td>
                       <td width="75px">'.$data["smartphone"].'</td>
                       <td width="75px">'.$data["gp_smart2"].'</td>
                       <td width="75px">'.$data["accessorygp"].'</td>
                       <td width="75px">'.$data["pullthroughpercent"].'</td>
                       <td width="75px" id="profit">'.$data["tmppercent"].'%</td>
                       <td width="75px" id="profit">'.$data["bundlepercent"].'</td>
                       <td width="75px" id="profit">'.$data["HUM_Devices"].'</td>
                       ';     
                    }
                }
                echo "</tr></tbody></table>";
 

  最佳答案

我将在echo之前为每个变量设置样式,然后您不需要每个变量的if.我还将使用 heredoc语法来更容易编写输出.例如:

 $tmpstyle = $data["tmppercent"] < "75" ? ' style="color: red;"' : '';
$bundlestyle = $data["tmppercent"] > "50" ? ' style="color: green;"' : '';
$gp_smartstyle = $data["gp_smart2"] < "30" ? ' style="color: blue;"' : '';
echo <<<EOD
    <tr >
    <td id="man" width="100px">{$data["Employee_Name1"]}</td>
    <td id="model" width="100px">{$data["GP"]}</td>
    <td width="75px">{$data["activations"]}</td>
    <td width="75px">{$data["upgrades"]}</td>
    <td width="75px">{$data["smartphone"]}</td>
    <td width="75px" $gp_smartstyle>{$data["gp_smart2"]}</td>
    <td width="75px">{$data["accessorygp"]}</td>
    <td width="75px">{$data["pullthroughpercent"]}</td>
    <td width="75px" id="profit" $tmpstyle>{$data["tmppercent"]}</td>
    <td width="75px" id="profit" $bundlestyle>{$data["bundlepercent"]}</td>
    <td width="75px" id="profit">{$data["HUM_Devices"]}</td>
EOD;
 

请注意,您有具有相同id值的多个元素,这不支持,您需要向它们添加索引以使它们独有.例如,添加一个计数器:

 $i = 0;
foreach ($result as $data) {
     ...
     echo <<EOD
<td id="man$i" width="100px">{$data["Employee_Name1"]}</td>
     ...
EOD;
     $i++;
}
 

  相同标签的其他问题

phpcssmysql
上一个问题: