• 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

    • Just for anyone reading, AdGuard (the free, standalone MV3 extension) is quite good now, a direct competitor to uBlock Origin Lite and much more built-out than it.
    • Microsoft Edge 149.0.4022.62 by Razvan Serea Microsoft Edge is a super fast and secure web browser from Microsoft. It works on almost any device, including PCs, iPhones and Androids. It keeps you safe online, protects your privacy, and lets you browse the web quickly. You can even use it on all your devices and keep your browsing history and favorites synced up. Built on the same technology as Chrome, Microsoft Edge has additional built-in features like Startup boost and Sleeping tabs, which boost your browsing experience with world class performance and speed that are optimized to work best with Windows. Microsoft Edge security and privacy features such as Microsoft Defender SmartScreen, Password Monitor, InPrivate search, and Kids Mode help keep you and your loved ones protected and secure online. Microsoft Edge has features to keep both you and your family protected. Enable content filters and access activity reports with your Microsoft Family Safety account and experience a kid-friendly web with Kids Mode. The new Microsoft Edge is now compatible with your favorite extensions, so it’s easy to personalize your browsing experience. Download: Microsoft Edge (64-bit) | 193.0 MB (Freeware) Download: Microsoft Edge (32-bit) | 170.0 MB Download: Microsoft Edge (ARM64) | 188.0 MB View: Microsoft Edge Website | Release History Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yeah, when I saw that, I wanted to find the nearest nose. You can't find a good nose these days when you need one.
    • Anthropic launches Claude Fable 5, a state-of-the-art AI model that beats OpenAI's GPT-5.5 by Pradeep Viswanathan Back in April, Anthropic announced Claude Mythos Preview, a frontier model with state-of-the-art coding capabilities. Due to the cybersecurity implications that would occur due to the availability of such a powerful model, Anthropic made it available to only a select set of companies around the world. The company's plan was to prepare appropriate guardrails before releasing such a powerful model to everyone. Now, after nearly two months, Anthropic announced Claude Fable 5, its most capable AI model yet for general users. The company also announced Claude Mythos 5, the same underlying model as Fable 5, but with safeguards lifted, making it more suitable for selected cybersecurity and biology use cases. Claude Fable 5 sits a tier above its Opus models and it beats most other generally available models across areas including software engineering, knowledge work, vision, scientific research, and long-running autonomous tasks. To prevent model misuse, when Claude Fable 5 detects certain requests related to cybersecurity, biology, chemistry, or model distillation, the request will be routed to the Claude Opus 4.8 model. Anthropic claims that these safeguards trigger in less than 5% of sessions on average. However, for large organizations working on critical software, Claude Mythos 5 can be availed through Project Glasswing. Later, Anthropic has plans to expand access through a broader trusted access program. As you can notice in the benchmarks above, Fable 5 and Mythos 5 are state-of-the-art on most key AI benchmarks and they are well ahead of OpenAI's frontier model, GPT-5.5. For example, Fable 5 is the new state-of-the-art model for vision tasks. Also, Mythos 5 has the strongest cybersecurity capabilities of any model in the world. Claude Fable 5 and Claude Mythos 5 are priced at $10 per million input tokens and $50 per million output tokens, which is less than half the price of Claude Mythos Preview. Another big change is that Anthropic is making a change to the way they handle business customer data for both Fable 5 and Mythos 5 models. The company will now require 30-day retention for all traffic on both first- and third-party surfaces. Anthropic promises that it won't use the data to train Claude models, instead it will use it against complex and novel attacks. Claude Fable 5 is available today on the Claude API and consumption-based Enterprise plans. It is also included at no extra cost for Pro, Max, Team, and seat-based Enterprise customers from today through June 22. After that, users on those plans will need usage credits to continue using Fable 5, unless Anthropic extends the included access window based on capacity. Developers can access Fable 5 through the Claude API using the claude-fable-5 model name.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      530
    2. 2
      PsYcHoKiLLa
      234
    3. 3
      +Edouard
      128
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!