display php code in html class attribute

html class css
html class vs id
html class list
div id html
html multiple classes
html id
w3schools
class attribute css

i am not able to concatinate php in HTML please check the code where i want concatinate.

1  $data['seats'] .= "<div class=\"col-xs-2\">
2      <div class=\"seat-body\">
3      </div>
4  </div> ";

I want to add if else condition on above line number 2 in class attribute.

Here something more readable:

 $class = 'black';
 if($condition == 'something'){
  $class = 'white';
 }

 $data['seats'] .= '<div class="col-xs-2">
                     <div class="'.$class.'">
                     </div>
                    </div>';

or with a switch:

 switch($condition){
  case 'something':     $class = 'white'; break;
  case 'somethingelse': $class = 'red';   break;
  default:              $class = 'black';
 }

 $data['seats'] .= '<div class="col-xs-2">
                     <div class="'.$class.'">
                     </div>
                    </div>';

HTML Classes, of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. The HTML class attribute is used to define equal styles for elements with the same class name. x[i].style.display = "none"; } } </​script>. Try it Yourself ». Don't worry if you don't understand the code in the example above. The main idea of generating HTML is to keep HTML as is. Just divide your script into 2 parts: prepare data part and display data part. A latter one should contain mostly HTML with some PHP control structures. Of course, there should be as less PHP code as possible. Move all unnecessary PHP code into first part.

I quite like sprintf with a ternary for things like this:

<?php
$html =
sprintf(
'<div class="foo %s">
    <div class="bar">
    </div>
</div>',
$condition ? 'baz' : 'bat'
);

HTML class Attribute, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

Using java script would be ideal for changing classes. But if you want a PHP solution this is how you would do it.

UPDATED

I removed the "if" in your ternary operator. I also prefer single quotes when concatenating html together. So I switched your code to single quotes.

There were several other valid solutions on this page. Please take the time to understand all of the other solutions. It will help you down the road.

$data['seats'] .=
'<div class="col-xs-2">
  <div class="' . (in_array($seat, $bookArray)?('seat ladies'):('seat occupied ChooseSeat Harshad6')) . '" data-item="">
    <div class="' . (($id === 6) ? 'bla2' : '') . '">' .
      $seat .
      '<span class="seat-handle-left"></span>
      <span class="seat-handle-right"></span>
      <span class="seat-bottom"></span>
    </div>
  </div>
</div>';

HTML id, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Duplicate of: What's the best way to store class variables in PHP? For some time I've been having this discussion with a co-worker on how should you store attributes within a PHP class.

As I understand your question you want to add an additional class to your <div> based on an if statement:

<?php

$class = "";

if ( condition ) {
  $class = " yourNewClass";
}

$data['seats'] .= "<div class=\"col-xs-2\">
  <div class=\"seat-body{$class}\">
  </div>
</div> ";

$class is set to empty first, then set to your desired class if the condition is satisfied.

HTML div tag, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  If you are posting this form directly to a PHP script, you cannot access the data attributes. If you want to be able to do this you'd need to listen for the form submission in Javascript, then on submit grab the data you need and post it to the PHP script to handle it. Here is some example code (untested!), using jQuery.

 $data['seats'] .= "<div class=\"col-xs-2\">
                <div class='".(in_array($seat, $bookArray)?("seat ladies"):("seat occupied ChooseSeat Harshad6"))."' data-item=\"\">
                <div class='".(if($id === 6) ? 'bla2' : '')."'> 
                    $seat
                    <span class=\"seat-handle-left\"></span> 
                    <span class=\"seat-handle-right\"></span>
                    <span class=\"seat-bottom\"></span>
                </div>
                </div>
            </div> ";

W3.CSS Display, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Class in html: The class is an attribute which specifies one or more class names for an HTML element. The class attribute can be used on any HTML element. The class name can be used by CSS and JavaScript to perform certain tasks for elements with the specified class name.

CSS display property, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Using The class Attribute in JavaScript. The class name can also be used by JavaScript to perform certain tasks for elements with the specified class name. JavaScript can access elements with a specified class name by using the getElementsByClassName() method:

HTML Attributes, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Definition and Usage. The display property specifies the display behavior (the type of rendering box) of an element. In HTML, the default display property value is taken from the HTML specifications or from the browser/user default style sheet. The default value in XML is inline, including SVG elements.

HTML Global class Attribute, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  The class attribute specifies one or more classnames for an element. The class attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript (via the HTML DOM) to make changes to HTML elements with a specified class.

Comments
  • if(condition is true){ $data['seat'] = "<div>true html</div>"; } else { $data['seat'] = "<div>Another html</div"; }
  • no it is showing error can you please write and show
  • @Joseph_J has explained the exact thing in his answer. have a look there. If you're still confused, comment again and I'll write a detailed answer
  • it is working but there is some issue. please read below code <div class='\"seat-body\".$class."'> how to concatinate this?
  • Thank you @Bernhard
  • in your case it is class=\"seat-body '.$class.'\"
  • i like this idea
  • No need to \" when you '
  • @kerbholz Your completely right! I literally copied his code in. I edited it though.
  • where i have wrote if on line number 3