• 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

    • HomeBank 5.10.1 by Razvan Serea HomeBank is a free software (as in "free speech" and also as in "free beer") that will assist you to manage your personal accounting. It is designed to easy to use and be able to analyse your personal finance and budget in detail using powerful filtering tools and beautiful charts. If you are looking for a completely free and easy application to manage your personal accounting, budget, finance then HomeBank should be the software of choice. HomeBank also benefits of more than 19 years of user experience and feedback, and is translated by its users in around 56 languages. Highlights: Cross platform, supports GNU/Linux, Microsoft Windows, Mac OS X Import easily from Intuit Quicken, Microsoft Money or other software Import bank account statements (OFX, QIF, CSV, QFX) Duplicate transaction detection Automatic cheque numbering Various account types : Bank, Cash, Asset, Credit card, Liability Scheduled transaction Category split Internal transfer Month/Annual budget Dynamic powerful reports with charts Automatic category/payee assignment Vehicule cost HomeBank 5.10.1 changelog: change: the input field helper icon + fixed some spacing inconsistency change: transaction, added some missing input tooltips and reworked existing change: category, payee and tag window add input now have a tooltip and button change: split window, refactored the layout change: split window, add display of memo and date wish : #2106800 budget report option to exclude transfers from unbudgeted line bugfix: prevent deletion of non pending transaction when rejecting bugfix: transaction warning for no rate faultly showing in transfer bugfix: report missing space for filter tooltip icon bugfix: budget report missing filter tooltip bugfix: manage account closed icon was hidding budget icon bugfix: #2154771 view transcations requires hitting Escape or X twice to close dialog bugfix: #2154337 transfer to/from closed account with different currency don't show the amount bugfix: #2154234 scheduled transaction recurring pattern daily value limited to 100 bugfix: #2149897 view split for closed accounts bugfix: #2148561 global time chart do not shows current period by default bugfix: #2148456 the main screen Total Chart is no longer showing an overall total bugfix: #2147497 editing a transaction resets scroll position bugfix: #2147377 balance mixup with transaction same day sort by amount bugfix: #2147052 quarter are wrong when fiscal year is jan 1 bugfix: #2147048 all events for the month are late but today is only the 1st bugfix: #2144993 impossible to search for transactions by value for values >999,99 bugfix: #2144698 adding new Category/Payee/Tags requires hitting -Enter- bugfix: #2144419 QIF Account name detection fail on import bugfix: #2142349 can't delete account groups bugfix: #2139409 account maximum limit is not fully used (example credit card) bugfix: #2133783 transfers shouldn't add to dashboard top spending reports Download: HomeBank 5.10.1 | 20.5 MB (Open Source) Download: 3rd party packages (macOSX. Ubuntu...etc) View: HomeBank Website | Support | Features | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Same, price was right for my Home, laptop, phone. Works great!
    • Brave and Firefox. I’ve been using them as my primary browsers for a while now, perfect combo
    • They want Ring 0 access. Should be a hard no. A middle ground needs to be found.
  • 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
      524
    2. 2
      PsYcHoKiLLa
      232
    3. 3
      Edouard
      135
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!