• 0

PHP / CSS question


Question

I have a script that calculates a number based on user inputs. I am trying to highlight the resulting cell based on the submitted value.

$vartotal is the result when the form is submited <20 is suppose to be highlighed green, >30 Orange and > 34 red. howerver the only one that is working is >34 - Red.

I was hoping someone will see what I am doing wrong.

TIA


$varIndicatorClass = "";
if($vartotal < 20){
$varIndicatorClass = 'class="highlight"';
}
$varIndicatorClass = "";
if($vartotal > 30){
$varIndicatorClass = 'class="highlight2"';
}
$varIndicatorClass = "";
if($vartotal > 34){
$varIndicatorClass = 'class="highlight1"';
}
[/CODE]

The cell that is highlighted

[CODE]<td '.$varIndicatorClass.'>'.$vartotal.'</td>[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1156174-php-css-question/
Share on other sites

15 answers to this question

Recommended Posts

  • 0

You only need to initialize varIndicatorClass once:

   $varIndicatorClass = "";

   if($vartotal &lt; 20){
	$varIndicatorClass = 'class="highlight"';
	}
   else if($vartotal &gt; 30){
	$varIndicatorClass = 'class="highlight2"';
	}
   else if($vartotal &gt; 34){
	$varIndicatorClass = 'class="highlight1"';
	}

edit: you should also use an else statement for situations like this.

  • Like 1
  • 0

I screwed up, because it was late when I replied and I didn't read the code properly.

In the code I pasted above highlight2 will be chosen when the vartotal is above 30 (which includes > 34).

Removing the else statements would work, checking in reverse order would work, or getting more specific with the conditions would work:

Reverse order:

   $varIndicatorClass = "";

   if($vartotal &gt; 34){
        $varIndicatorClass = 'class="highlight1"';
        }
   else if($vartotal &gt; 30){
        $varIndicatorClass = 'class="highlight2"';
        }
   else if($vartotal &lt; 20){
        $varIndicatorClass = 'class="highlight"';
        }

More specific:

   $varIndicatorClass = "";

   if($vartotal &lt; 20){
        $varIndicatorClass = 'class="highlight"';
        }
   else if($vartotal &gt; 30 &amp;&amp; $vartotal &lt;= 34){
        $varIndicatorClass = 'class="highlight2"';
        }
   else if($vartotal &gt; 34){
        $varIndicatorClass = 'class="highlight1"';
        }

  • 0

I screwed up, because it was late when I replied and I didn't read the code properly.

In the code I pasted above highlight2 will be chosen when the vartotal is above 30 (which includes > 34).

Removing the else statements would work, checking in reverse order would work, or getting more specific with the conditions would work:

Reverse order:

   $varIndicatorClass = "";

   if($vartotal &gt; 34){
		$varIndicatorClass = 'class="highlight1"';
		}
   else if($vartotal &gt; 30){
		$varIndicatorClass = 'class="highlight2"';
		}
   else if($vartotal &lt; 20){
		$varIndicatorClass = 'class="highlight"';
		}

More specific:

   $varIndicatorClass = "";

   if($vartotal &lt; 20){
		$varIndicatorClass = 'class="highlight"';
		}
   else if($vartotal &gt; 30 &amp;&amp; $vartotal &lt;= 34){
		$varIndicatorClass = 'class="highlight2"';
		}
   else if($vartotal &gt; 34){
		$varIndicatorClass = 'class="highlight1"';
		}

you can also nest the code :p

Like so:


$varIndicatorClass = "";
if($vartotal > 30){
$varIndicatorClass = 'class="highlight"';
if($vartotal > 34){
$varIndicatorClass = 'class="highlight2"';
}
} else if($vartotal < 20){
$varIndicatorClass = 'class="highlight1"';
}
[/CODE]

  • 0

you can also nest the code :p

Like so:


$varIndicatorClass = "";
if($vartotal < 20){
$varIndicatorClass = 'class="highlight"';
if($vartotal > 30){
$varIndicatorClass = 'class="highlight2"';
if($vartotal > 34){
$varIndicatorClass = 'class="highlight1"';
}
}
}
[/CODE]

That won't work though, it will only go into that block if it's < 20, then you test if it's > 30/34 within that block.

  • 0

That won't work though, it will only go into that block if it's < 20, then you test if it's > 30/34 within that block.

Oops my bad I didn't see the < instead of > :p

fixed it

small asthetic thing, why are you doing $var='class="value"' when you can do $var="value" and put the "class=" bit in the html portion itself?

I wonder about that myself too but maybe he wants to use something else then class on certain values :/

  • 0

small asthetic thing, why are you doing $var='class="value"' when you can do $var="value" and put the "class=" bit in the html portion itself?

Cells that don't meet those conditions don't have a class.

It's not how I would do it, but it keeps the HTML generation part tidier.

  • 0

Do you guys all hate switch statements, or something? :laugh:

If you get to if, elseif, elseif, the syntax gets much cleaner if you just move to switch. Plus, if you ever have to stack classes (i.e, > 30 has classes for >30 and > 20), a switch makes it much easier.

  • Like 1
  • 0

Do you guys all hate switch statements, or something? :laugh:

If you get to if, elseif, elseif, the syntax gets much cleaner if you just move to switch. Plus, if you ever have to stack classes (i.e, > 30 has classes for >30 and > 20), a switch makes it much easier.

I'm always thinking like if and else if and else in my mind when writing stuff xD

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.