在表中显示检查或x布尔值 [英] display check or x boolean in table

查看:82
本文介绍了在表中显示检查或x布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在mysql数据库中获取到html

I have atable in mysql database i'm fetching it to a html

    print "<table>\n"; 
    $result = $con->query($query); //return only the first row (we only need field names)
    $row = $result->fetch(PDO::FETCH_ASSOC); 
    print "<tr>\n"; 
    foreach ($row as $field => $value){ 
         print "<th>$field</th>\n";
    } // end foreach 
    print "</tr>\n";  //second query gets the data 
    $data = $con->query($query); 
    $data->setFetchMode(PDO::FETCH_ASSOC); 
    foreach($data as $row){ 
         print "  <tr>\n"; 
         foreach ($row as $name=>$value){ 
               print "<td>$value</td>\n";
         } // end field loop 
         print "</tr>\n"; } // end record loop 
         print "</table>\n"; 
    } 
    catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage(); 
    } // end try

我有2个具有布尔值的列,我希望如果列6 ='0',则显示x color:red;否则显示&#10004; color:green;

i have 2 columns that have a boolean value, i want that if column 6 = '0' then display x color:red; else display &#10004; color:green;

最终密码

foreach ($row as $name=>$value){
 if (($name == "paid" || $name == "added") && $value == "0"){

           print "<td><span style='color: red;'>X</span></td>\n";
       }
       elseif (($name == "paid" || $name == "added") && $value == "1"){
           print "<td><span style='color: lime;'>&#10004;</span></td>\n";
       }

   else { 
       print "<td>$value</td>\n";
   }
 } // end field loop

推荐答案

尝试:

     foreach ($row as $name=>$value){
       if ($name == "Column 6"){
           if ($value == "0") {
               print "<td><span style='color: red;">X</span></td>\n";
           }
           else {
               print "<td>&#10004;</td>\n";
           }
       }
       else { 
           print "<td>$value</td>\n";
       }
     } // end field loop 

除非您要使用一些复杂的CSS,否则没有很好的方法使复选框着色.但是,您可以尝试使用CSS之前和之后的复选框,如下所示:

There's no good way to colorize checkboxes, unless you want to use some complex css. However you could try and utilize before and after CSS for checkboxes as shown here: CSS ''background-color" attribute not working on checkbox inside <div>

这篇关于在表中显示检查或x布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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