0

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

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;

Final Code

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
YesItsMe
  • 1,709
  • 16
  • 32

1 Answers1

0

Try:

     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 

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>

Community
  • 1
  • 1
David P
  • 2,027
  • 3
  • 15
  • 27
  • it's not a form, its a table output, take a look http://jsfiddle.net/t1w9vg88/ paid and added instead of 0 or 1 i want x or `✔`. – YesItsMe Mar 02 '15 at 17:13
  • So you aren't displaying the HTML table? Or are you concerned that the checkbox can be toggled? If the latter, just put something on the onclick of the checkbox to set it to always be checked. ie - onclick="this.checked=true;" – David P Mar 03 '15 at 14:47
  • jack fill out a form the info gets passed into DB, john process it, the val. of paid is 0/1 since it's boolean, now john views the table he should see paid ✔ or x instead of 1/0 (if i would know how to select the 4 column i would do {foreach row $(column.3 inner.html(if==1).html="✔" else(html="x")} or make 2 classes .check .ex toggle class like that i could play with css too, – YesItsMe Mar 04 '15 at 00:33