[SOLVED] Hide div based on database value

I know there is a simple solution to this but I cannot figure it out.

Basically, if the $variable = 2 then I do not want the div to show. I tried the code below but I am getting a syntax error.

<div <?php if ($dbValue == 2) echo style='display: none'; ?>>div content</div>

Try this.

<div<?php if ($dbValue == 2) echo " style='display: none';"; ?>>div content</div>

You didnā€™t have your echo around quotes.

You should really just add a class like ā€œhideā€ instead and have .hide{display:none;} instead.

1 Like

Worked like a charm, I know it was something small I was missing. And I took your advice and made a class for it.

Thank You very much

You may want to convert inline styles to a simple CSS class to reference that instead. Itā€™ll make your life easier later on:

.display-none {
      display:none;
}

Use like:

<div<?php if ($dbValue == 2) echo " class='display-none';"; ?>>div content</div>

Just a thought. I use to code using inline until I couldnā€™t anymore. Referencing CSS and their classes will keep things more consistent and easier to maintain IMO.

Good luck.

I already suggested that.

And the OP has taken that suggestion.

My fault. Sorry about that.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.