0

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>.

4
  • 1
    Please post a dump of your object. var_dump($my_exams); Commented Dec 29, 2013 at 1:02
  • What exactly is the output of this code? Commented Dec 29, 2013 at 1:03
  • @Machavity Why do you need that? His question is just about the PHP syntax to add the class to the output, he already knows how to access the array contents. Commented Dec 29, 2013 at 1:05
  • 1
    $exam->name is not an array Commented Dec 29, 2013 at 1:16

3 Answers 3

3

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

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

1 Comment

Just the "if" statement is not correct, it should be without "!" to keep the logic. Staying in PHP section is a good idea.
1
<tr class="<? echo $exam->name ?>"><td><? echo $exam->name ?></td></tr>

Comments

-1

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.

1 Comment

Adding sprintf and the pointless if()/continue condition makes this way more complicated than it needs to be.

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.