1

I am performing some server side validations and I want to style the rows depending on the PHP variable. For example, if PHP variable is 2 the row should be styled to green else red.

I was thinking to add a php function in the html but it doesn't work. What is the best practice to accomplish that?

<tr class="<?php add_red();?>">
 <td>
  <input class="w3-radio" name="reflexive" type="radio" id="reflexiveYES" value="yes">
 </td>
</tr>

function add_red(){
    //inside validation.php
    if($a == 2){
        return "w3-green"; //class for changing the background
    }
    return "w3-red";
}
5
  • 2
    You're returning a value but you're not echoing it Commented Feb 22, 2018 at 10:30
  • 3
    Don't need a function for this - assuming w3-green and w3-red are CSS classes... <input class="w3-radio <?= $a == 2 ? "w3-green" : "w3-red"; ?>" name="reflexive" type="radio" id="reflexiveYES" value="yes" /> Commented Feb 22, 2018 at 10:31
  • But what if she has 20000 rows and decides to change it? Function is pretty useful here. Commented Feb 22, 2018 at 10:32
  • @MichałSkrzypek - if there are 20000 rows and they're not being generated dynamically in a foreach() loop there's probably a bigger problem overall ;) Commented Feb 22, 2018 at 10:34
  • Problem is that the validation.php file is a different file than the index.php (which the html is located) Commented Feb 22, 2018 at 10:38

5 Answers 5

2

You don't need a function to do this, try this:

<tr class="<?php echo ($a == 2) ? 'w3-green' : 'w3-red'; ?>">
 <td>
  <input class="w3-radio" name="reflexive" type="radio" id="reflexiveYES" value="yes">
 </td>
</tr>
Sign up to request clarification or add additional context in comments.

3 Comments

the variable $a is in a different file.
@Eternal: ok, how about your function?
@Eternal: you can add that variable into global array!
2

Your function returns a value you specified, but you need a command to actually print it:

<tr class="<?php echo add_red();?>">
 <td><input class="w3-radio" name="reflexive" type="radio" id="reflexiveYES" value="yes">
</td>
</tr>

1 Comment

Hey, I answered that earlier 😁
1

You forgot to output the value. Just put echo before add_red call

Comments

0

You actually need to print that value, something like this:

<tr class="<?php echo add_red();?>">
    <td>
        <input class="w3-radio" name="reflexive" type="radio" id="reflexiveYES" value="yes">
    </td>
</tr>

<?php
function add_red(){
    //inside validation.php
    if($a == 2){
    return "w3-green"; //class for chaning the background
    } else {
    return "w3-red";
    }
}
?>

Ensure that you define $a inside your function or you'll get an error

2 Comments

what if the variable is in a different file ?
You'll need to include that file with require, or include
0

First of all pass a parameter in your function. The function structure should like the following:

function add_red( $a ){
    //inside validation.php
    if($a == 2){
        echo"w3-green"; //class for making the background red
    } else {
        echo"w3-reed"
    }
}

Then call the function in html

<tr class = "<?php add_red(2); ?>">
    <td>
        <input class="w3-radio" name="reflexive" type="radio" id="reflexiveYES" value="yes">
    </td>
</tr>

5 Comments

I wouldn't print right in the function. who knows where else you gonna run it. And it's not testable.
If you wouldn't print right the function then use the following. This is tested function add_red( $a ){ //inside validation.php if($a == 2){ return "w3-green"; //class for making the background red } else { return "w3-reed"; } }
Okey, could you please show me the unit test example for this function?
Yeah that's better :)
Nope, it is not the variable the problem. I use a standard return into the function without a variable but it still doesn't work. Only if I use the code in the same file it works. It doesn't recognize that the function exists in that file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.