ananaz Posted December 24, 2010 Share Posted December 24, 2010 Hello I want to be able to change the class depending on if a variable is 1 or 0. <html> <p class="note-general"> This is the general </p> <p class="note-warning"> And this is the warning </p> </html> I want the variable $adult (that can be 1 or 0) to set if the class should be a note-general or note-warning. Thank you for support Link to comment https://forums.phpfreaks.com/topic/222577-change-html-class-with-php-variable/ Share on other sites More sharing options...
dragon_sa Posted December 24, 2010 Share Posted December 24, 2010 <html> <?php if ($adult==0) { $class="note-general"; echo "<p class='$class'> This is the general </p>"; } if ($adult==1) { $class="note-warning"; echo "<p class='$class'> And this is the warning </p>"; } ?> </html> Link to comment https://forums.phpfreaks.com/topic/222577-change-html-class-with-php-variable/#findComment-1151096 Share on other sites More sharing options...
the182guy Posted December 24, 2010 Share Posted December 24, 2010 $adult = 1; // add code to set the $adult variable $className = $adult == 1 ? 'note-general' : 'note-warning'; <html> ... <p class="<?php echo $className; ?>">some message</p> </html> Link to comment https://forums.phpfreaks.com/topic/222577-change-html-class-with-php-variable/#findComment-1151105 Share on other sites More sharing options...
ananaz Posted December 24, 2010 Author Share Posted December 24, 2010 will this do? <p class="<?php if ($aresult1['adult']==0) { $class="note-general"; } else { $class="note-warning"; } echo "$class"; ?>"> Link to comment https://forums.phpfreaks.com/topic/222577-change-html-class-with-php-variable/#findComment-1151120 Share on other sites More sharing options...
BlueSkyIS Posted December 24, 2010 Share Posted December 24, 2010 unless you'll be using $class later, there is no need to define it. <?php if ($adult==0) { echo "<p class='note-general'> This is the general </p>"; } else if ($adult==1) { echo "<p class='note-warning'> And this is the warning </p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/222577-change-html-class-with-php-variable/#findComment-1151134 Share on other sites More sharing options...
BlueSkyIS Posted December 24, 2010 Share Posted December 24, 2010 but if you want to keep $class for later in the script: <?php if ($adult==0) { $class = 'note-general'; echo "<p class='$class'> This is the general </p>"; } else if ($adult==1) { $class = 'note-warning'; echo "<p class='$class'> And this is the warning </p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/222577-change-html-class-with-php-variable/#findComment-1151135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.