简体   繁体   中英

Define CSS property value based on PHP variable

I want to define the font-weight for my tr element based on a PHP variable. What am I doing wrong in this code?

<?
    $vartest = 1;
?>

<table>
  <tr style="font-weight: <? ($vartest === 1) ? echo bold : echo normal ?>">
    <td>aaaaaaa</td>
    <td>bbbbbbb</td>                                        
  </tr>
</table>

Try this :

<?php

    $vartest = 1;
?>

<table>
  <tr style="font-weight: <?php echo ($vartest === 1) ? 'bold' : 'normal' ?>">
    <td>aaaaaaa</td>
    <td>bbbbbbb</td>                                        
  </tr>
</table>

The ternary expression is wrong. Try with -

<tr style="font-weight: <? echo ($vartest === 1) ? 'bold' : 'normal'; ?>">

Try the below

<table>
<?php 
$result=($vartest === 1)?"<tr style='font-weight:bold'>":"<tr style='font-weight:normal'>";
    echo $result; ?>
        <td>aaaaaaa</td>
        <td>bbbbbbb</td>                                        
      </tr>
    </table>

Let me know if it is helpful

在语法中为String添加""

<tr style="font-weight: <? echo ($vartest === 1) ?  "bold" :  "normal" ?>">

Maybe you can do it like this, using css to make it pre-defined:

<?php
$Class = "bold";
?>
<html>
<head>
    <style>
        tr.bold{
            font-weight: bold;
        }

        tr.normal{
            font-weight: normal;
        }
    </style>
</head>

<body>
    <table>
        <tr class="<?php echo $Class; ?>">
            <td>abc</td>
            <td>def</td>
        </tr>
    </table>
</body>

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