0

I am trying to echo out multiple table rows on my sql query and provide them with alternating colors to improve the aesthetic value of my site. I am fairly new to php and I am extremely fussy over the presentation of my code and therefore I would like to include the output html into my PHP block to improve readability.

I have browsed some past threads but I am still rather unclear of how the formatting of a string works in PHP, the code below shows my attempt of formatting the output:

echo '<tr class=" . 'if( $class_style %2 == 0 ){ echo "row_dark"; } else echo "row_light"' . ">';

What am I doing wrong here?

Regards Alex.

4 Answers 4

1

You can't put an if structure in an echo.
Use that :

echo '<tr class="'. ($class_style %2 == 0) ? 'row_dark' : 'row_light' . '">';

It's a ternary operation.

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

1 Comment

Thank-you for your response, greatly appreciated - this did the trick!
0

You should use like this right syntax:

echo '<tr class="'.($class_style %2 == 0 ? "row_dark" : "row_light").'">';

Comments

0

It should be

echo '<tr class=" '. instead of echo '<tr class=" .'

Comments

0
<?php echo '<tr class="'.(($class_style %2 == 0)?'row_dark':'row_light').'">';?>

or

<?='<tr class="'.(($class_style %2 == 0)?'row_dark':'row_light').'">';?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.