Skip to main content
edited title; edited title
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

This PHP variable changes thecode that chooses between multiple HTML class properly. How can I do this without creating endless if/else statements?banners

Source Link

This PHP variable changes the HTML class properly. How can I do this without creating endless if/else statements?

In the snippet below, I'm creating a banner for my website. If I change the variable in the html file, I want to call a different banner. It does work as is.

<?php $pencilbanner='hol'; include($_SERVER['DOCUMENT_ROOT'] . '/includes/banner.html'); ?>

In my banner.html file, I'm holding all the banners. Each one with its own variable name.

<style type="text/css">
  <?php include($_SERVER['DOCUMENT_ROOT'] . '/dist/css/components/banner.css');?>
</style>

<div class="banner <?php if($pencilbanner == 'hol') echo 'holiday-banner' ; else echo 'standard-banner'?>">
  <?php if($pencilbanner == 'std'){ ?>
    <!-- Standard -->
    <h4>This is the standard banner</h4>
  <?php } ?>

  <?php if($pencilbanner == 'hol'){ ?>
    <!-- Holiday -->
    <h4>This is the holiday banner</h4>
  <?php } ?>

  <?php if($pencilbanner == 'lim'){ ?>
    <!-- Limited -->
    <h4>This is the limited time only banner</h4>
  <?php } ?>
</div>

The issue I'm having is in this line:

<div class="banner <?php if($pencilbanner == 'hol') echo 'holiday-banner' ; else echo 'standard-banner'?>">

At worst, this can end up being an endless chain of if/else statements, based on the banner I call. Ultimately, we intend on having multiple banners, and that can lead to if else > else if > else if > else if > else if > else, and I don't want to get there.

What is the most direct approach to this without creating a long if/else statement?