user3143024
user3143024

Reputation: 1

Setting a CSS element class in PHP

I'm trying to modify a bit of PHP code to get it to assign a unique CSS class to the elements it creates as it cycles through its loop. Theoretically, I'm just trying to take a "name" that's echoed to the screen and assign that as a class to a element that's created next... Here's the intitial relevant code loop:

<?php foreach($my_exams as $exam):
  if(!$exam->is_taken) continue;?>
  <tr><td><?php echo $exam->name;?></td></tr>
<?php endforeach;?>

Simplistcally, I'm trying to get the string that's echoed by $exam->name to be assigned to the class of that <tr> element. Something like

<tr class="<?php echo $exam->name;"><td><?php echo $exam->name;?></td></tr>

Although I'm sure I'm handling the quotes or syntax improperly (at least, anyway, it doesn't end up assigning the class to the <tr>.

Upvotes: 0

Views: 97

Answers (3)

Giacomo1968
Giacomo1968

Reputation: 26056

Others have answered this pretty much the same way I am about to, but I want to add this to explain the issue. And why this is not a “stupid” question, but more of a bizarre byproduct of the way that some CMS system mix HTML & PHP within their templates. In short: They format the template as nice HTML to make it seem clean & easy for non-coders, but in doing so their mixing of inline-PHP makes PHP coding seem more difficult than it is. Meaning this code:

<?php foreach($my_exams as $exam):
  if(!$exam->is_taken) continue;?>
  <tr><td><?php echo $exam->name;?></td></tr>
<?php endforeach;?>

Can easily be this:

<?php
foreach($my_exams as $exam) {
  if ($exam->is_taken) {
    echo '<tr><td>'
       . $exam->name
       . '</td></tr>'
       ;
  }
}
?>

Which is now easier to parse from a programming standpoint, so you can now do this:

<?php
foreach($my_exams as $exam) {
  if ($exam->is_taken) {
    echo sprintf('<tr%s><td>', ' class="' . $exam->name . '"')
       . $exam->name
       . '</td></tr>'
       ;
  }
}
?>

What I did there is use sprintf to place ' class="' . $exam->name . '"' into the ''. The %s means that is a string that should be placed there, and the string is what comes after the comma in the sprintf statement. I find this much easier to code, test & debug. But in general, the key to making PHP coding easier is to just use straight PHP when any logic needs to be placed in the context of HTML.

Upvotes: -1

Jessica
Jessica

Reputation: 7005

It will help if you stop going in and out of PHP so much, it will probably be easier to read this way:

<?php 
foreach($my_exams as $exam){
    if($exam->is_taken){
        echo '<tr class="'.$exam->name.'"><td>'.$exam->name.'</td></tr>';
    }
}

If you want to do double quotes, you need to escape them when you want to echo them, but then you can use a variable without concatenating a bunch of strings. (Once you are using objects/arrays it helps to surround each variable with {})

echo "<tr class=\"{$exam->name}\"><td>{$exam->name}</td></tr>";

Reference: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double

Upvotes: 3

zajd
zajd

Reputation: 761

<tr class="<? echo $exam->name ?>"><td><? echo $exam->name ?></td></tr>

Upvotes: 1

Related Questions