user541597
user541597

Reputation: 4345

If statement to change Div id

I'm writing some php/html code. I have a css file that has several backgrounds. Is there any way to write an if statement to select a div id depending on a php variable?

Upvotes: 1

Views: 5616

Answers (5)

jondavidjohn
jondavidjohn

Reputation: 62392

could do it in-line in your html/php

<div id="<?= ($true_or_false) ? 'div_id_1' : 'div_id_2' ?>">div content</div>

Or... if you have more than 2 div id options just do an classic if statement

<?php

if($condition) {
    $div_id = 'something';
}
else if ($another_condition) {
    $div_id = 'something_else';
}
..etc

?>

<div id="<?php echo $div_id ?>">div content</div>

Upvotes: 1

fredley
fredley

Reputation: 33871

// file: style.php
<?php
header("Content-Type: text/css");
?>

#mydiv {
  background: url('<?php

switch($_GET['background'])
  {
  case 1:
    echo 'blah1.jpg';
  //etc...
  }

?>');
}

Upvotes: 1

karim79
karim79

Reputation: 342625

Is this what you mean?

<?php
    $divId = $someBoolean ? "peaches" : "pineapples";
?>
<div id="<?php echo $divId ?>">Hello</div>

Upvotes: 1

jhuebsch
jhuebsch

Reputation: 451

There are a few different ways you can do this. You can do it in php and assign a different class or id. Or you do it with javascript and change the class or id after the page loads.

Upvotes: 0

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

You can generate CSS via PHP, first sort everything out and then print with text/css header.

<?php
header("Content-Type: text/css");
?>
*{margin:0;padding:0;}

Anyway you need to clarify your question.

Upvotes: 4

Related Questions