简体   繁体   中英

how to change attribute of table cell based on contents when table rows created by while loop with PHP

I am a beginner with PHP and only know the basics. I want to do my first more advanced thing, but been have not been able to figure out how to do it.

I fetch data from mysql database and then create an html table.

while($row = $queryresult->fetch_assoc()) {
echo "<tr><td>".$row['col1']."</td><td>".$row['col2']."</td></tr>";}

I want to add a css class to a cell based on its contents. So if col1 on line one is "foo" then I want to have <td class="foo"> and if col1 is "bar" on line 2 of the table then I want <td class="bar">

I tried

if ($row['col1'] == "foo") {
        $classvar = " class=foo";
    } elseif ($row['col1'] == "bar") {
        $classvar = " class=bar";
    }
echo "<tr><td".$classvar.">";

But that sets all rows to the same thing based on the first line iterated. So if the first line has col1="foo" then $classvar = " class=foo" on all lines. How could I have each table row evaluated separately?

Place the test inside the while loop.

Its also a good idea to first initialize $classvar to nothing for the cases where $row['col1'] is neither foo or bar

Also a nice way of keeping the string concatenation easy to read is to use the {} around the {$array['key']} then you dont have to keep concatenating with the dot all the time

And as someone else mentioned a class name must be inside either "" or ''

while($row = $queryresult->fetch_assoc()) {
    $classvar = '';
    if ($row['col1'] == "foo") {
        $classvar = 'class="foo"';
    } 
    if ($row['col1'] == "bar") {
        $classvar = 'class="bar"' ;
    }
    echo "<tr><td {$classvar}>{$row['col1']}</td><td>{$row['col2']}</td></tr>";
}

Change elseif to if into your condition.

while($row = $queryresult->fetch_assoc()) {

     if ($row['col1'] == "foo") {
         $classvar = " class=foo";
     } 
     if ($row['col1'] == "bar") {
         $classvar = " class=bar";
     }

        echo "<tr><td",$classvar,">",$row['col1'],"</td><td>",$row['col2'],"</td></tr>";
}

Replace

echo "<tr><td>",$row['col1'],"</td><td>",$row['col2'],"</td></tr>";

to

echo "<tr><td ".$classvar.">".$row['col1']."</td><td>".$row['col2']."</td></tr>";

I prefer the templating approach:

<?php while($row = $queryresult->fetch_assoc()) { ?>
  <tr>
    <td class="<?=($row['col1'] == "foo") ? 'foo' : 'bar'?>"><?=html($row['col1'])?></td>
    <td><?=html($row['col2'])?></td>
  </tr>
<?php } ?>

Include this function first:

function html($str){
  return htmlspecialchars($str, ENT_QUOTES);
}

As you can see above, I'm using a combination of <?='string'?> which is shorthand for <?php echo 'string' ?> .

And I also use the ternary operator: statement ? if_true : if_false statement ? if_true : if_false .

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