• 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.
  • Posts

    • Still 3x what it should cost. So, it seems the trick is to increase price by 6x so that a reduction in price back to 4x looks like a steal. "You savvy shoppers win again!" I'm glad I'm not in a desperate spot to actually even need this overpriced crap. Hopefully, it comes back down by the time for when (or if) I ever do.
    • Although AI is great and has it's use cases they likely have massively overhyped it and it has not delivered as per their expectations. I fully expect them to start saying the same things again when it does get to a certain level of intelligence!
    • Microsoft wants to end printer driver headaches with Windows Ready Print by Usama Jawad A few days ago, Microsoft released Windows 11 Experimental build 26300.8553, bringing a ton of enhancements such as Start menu customization, search improvements, Taskbar polish, and other minor UI tweaks. Another relatively major enhancement snuck deep within the change log was related to upgrades to the Windows printing experience. Now, Microsoft has shared more details about these benefits. For starters, Microsoft has renamed its Modern Print Platform to Windows Ready Print. The company believes that this name highlights its shift in strategy, which now focuses on modernizing, securing, and streamlining the printing experience for Windows devices. Some of the upgrades present in Windows Ready Print have already been seeded to customers and partners. This includes ending support for third-party printer drivers via Windows Update and transitioning towards the Internet Printing Protocol (IPP) and the native Windows IPP printer driver. In line with these changes, new printer installations will default to Windows Ready Print on eligible devices starting from July 2026. However, Microsoft recognizes that not all environments will be able to migrate to this platform immediately, so it will allow users to choose between installing the printer via Windows Ready Print or the traditional OEM process. Users will be able to toggle this configuration through Settings > Bluetooth & Devices > Printers & Scanners > Printer preferences. This control applies only to new printer installations, and its functionality can also be modified via Group Policy as follows: Launch Group Policy Editor Navigate to Local Computer Policy -> Administrative Templates -> Printers Find and select 'Configure Windows Ready Print driver ranking' -> double click to open it Select 'Enabled' (if you wish to enable Windows Ready Print driver selection) or 'Disabled' (if you wish to explicitly disable Windows Ready Print driver selection). Select Apply Select OK Similarly, if you set up Windows protected print mode through the same setting in Windows 11, it will also default to using Windows Ready Print exclusively. Microsoft hopes that these improvements will help eradicate dependency on OEM-specific driver installation processes and simplify printer installations. We'll likely find out more about other tangible benefits in the coming months.
    • Hey what's about the proton vpn firefox extension ? It's not working today
  • Recent Achievements

    • One Year In
      Primer1st earned a badge
      One Year In
    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      513
    2. 2
      PsYcHoKiLLa
      229
    3. 3
      Edouard
      138
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!