0

I am using PHP to retrieve data stored in a MySQL table and then display it on a page using the following the code:

CSS

.date {
color:red;
font-weight:bold;
}

HTML / PHP

<span class="date">'.$date_available.'</span>

This works fine, but I need to assign a different class depending on the value within the $date_available variable. For example: if the variable contains the value SOLD, the span class should be as shown above. If the variable contains the value NOW, it should be:

HTML / PHP

<span class="date-two">'.$date_available.'</span>

CSS

.date-two {
color:green;
font-weight:bold;
}

Is this possible with PHP or will JavaScript need to be used?

5 Answers 5

1
<?php
$cls = $data_available == "SOLD" ? "date-two" : "date";
echo "<span class=\"{$cls}\">{$date_available}</span>";

edit: explanation. you check if your variable contains "SOLD", and based on that check save the name of the css-class to be used in a variable. then, you don't output a fixed class-string, but the content of the variable.

Sign up to request clarification or add additional context in comments.

2 Comments

agreed. it just comes natural to me in such cases without even thinking about it. and it gets the job done.
Yes i know what you mean. Its realy slimmer.
1

Depending on if you have one or more states you could go with either an if () {} else {} construct, use an array() for the job or a switch statement might also be usable if you have multiple states that require the same class to be output.

for instance using an array:

$classes = array(
    'sold' => 'date-two',
    '...' => '...' //example values
);

// imagining that $data_available = 'SOLD'
echo '<span class="'. ($classes[strtolower($data_available)] || 'date') .'">';

or using a switch statement

$cls = 'date';
switch (strtolower($data_available)) {
    case 'sold':
    case 'sold2': // switch statements allow multiple 'cases' to be grouped for the same output.
        $cls = 'date-two';
    break;

    case '...':
        $cls = '...';
    break;

    default:
        $cls = 'date';
    break;
}

for the if example I'd look at Tobias' answer

Another option is instead of using a different class, simply know what the database can output and create prefixed classes for that.

e.g. sold, bought and negotiating are some states (example)

What you could do here is simply

$cls = 'date-' . strtolower($data_available); // if $data_available is sold it will output date-sold

echo '<span class=". $cls .">';

then in your CSS you could do something like:

.date-sold {
    width: 420px;
    background: green;
}

.date-bought {
    width: 720px;
    background: white;
}

.date-negotiating {
    width: 30px;
    background: transparent;
}

this way you'll always be safe and if your database input is consistent which it should be then all your divs will get the right class with the right CSS applied without too much PHP hassle.

Good luck!

Comments

0

Yes its possible. You can use if to decide which class should be use for the span. You can easy add new classes with else if.

if($date_available == "SOLD") {
    echo '<span class="date">'.$date_available.'</span>';
} else {
   echo '<span class="date-two">'.$date_available.'</span>';
}

Comments

0

Change the class on the way in:

  <?  if ($date_available == 'whatever'){ ?>
       <span class="date"><? echo $date_available ?></span>
  <?  }else{ ?>
       <span class="date-two"><? echo $date_available ?></span>
  <?  } ?>

Comments

0
switch( $date_available ){
    case 'sold':$class='date-two';break;
    case 'available':$class='date';break;
    case 'low-stock':$class='date-three';break;
    /* etc */
}
echo "<span class='$class'>$date_available</span>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.