0

I wrote a little script where an item should be bold whenever a certain value is set to 0. But I have the feeling I'm repeating myself... Is there a way to avoid this repeat?

$output .= '<td>';
if ('0' == $value['treated']) {
    $output .= '<b>';
}
$output .= $value['from'];
$output .= substr($value['message'], 20);
if ('0' == $value['treated']) {
     $output .= '</b>';
}
$output .= '</td>';
1
  • Maybe you should checkout DOMDocument. Commented Jun 7, 2012 at 10:29

5 Answers 5

5

You could set a class instead?

$output .= '<td' . ('0' == $value['treated'] ? ' class="bold"' : '') . '>';
$output .= $value['from'];
$output .= substr($value['message'], 20);
$output .= '</td>';

All you would then need to do is add

.bold { font-weight: bold; }

to your css file

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

Comments

1
$output = $value['from'] . substr($value['message'], 20);

if ('0' == $value['treated']) {
    $output = "<b>$output</b>";
}

$output = "<td>$output</td>";

4 Comments

Won't I get issues when using $output in both cases? Won't it be overwritten?
Yes, it will be overwritten. But it's not an issue. IMO, consecutively wrapping your output in the necessary tags is a lot easier than cobbling together tags in a sequential way as you are doing.
True that! And why should I use your method and not the class-method? Are there certain advantages in this method comparing to the other?
Classes will work fine as well. It depends on whether you want a <b> tag (probably better a <strong> tag nowadays), which is semantic, or a class, which is just style.
0

I don't know if this is what you are looking for, but you could buffer the contents:

$output .= "<td>";
$buffer = $value['from'];
$buffer = substr($value['message'], 20);
$output .= ('0' == $value['treated'])
    ? "<b>" . $buffer . "</b>"
    : $buffer;

Edit: I see deceze was first :)

Comments

0

This is a simple way you can acheive

$output .= '<td>';
        $bs = '';
        $be = '';
        if ('0' == $value['treated']) {
            $bs = '<b>';
            $be = '</b>';
        }
        $output .= $bs;
        $output .= $value['from'];
        $output .= substr($value['message'], 20);
        $output .= $be;
    $output .= '</td>';   

or

$output .= '<td';
    if ('0' == $value['treated']) {
        $output .=' style="font-weight:bold;"';
    }
    $output .= '>';
    $output .= $value['from'];
    $output .= substr($value['message'], 20);
$output .= '</td>';

Comments

0
$dom = new DOMDocument;
$dom->loadHTML('<html/>');
$body = $dom->documentElement->appendChild($dom->createElement('body'));
for ( $i = 0; $i < 100; ++$i ) {
    $div = $body->appendChild($dom->createElement('div'));
    if ( rand() % 2 ) {
        $div->setAttribute('class', 'highlighted');
        $div->nodeValue = 'bold';
    } else {
        $div->nodeValue = 'normal';
    }
}
die($dom->saveHTML());

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.