Halfpint
Halfpint

Reputation: 4079

PHP echo formatting of html

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.

Upvotes: 0

Views: 547

Answers (4)

Bora
Bora

Reputation: 10717

You should use like this right syntax:

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

Upvotes: 0

user2678106
user2678106

Reputation:

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

or

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

Upvotes: 0

netvision73
netvision73

Reputation: 4921

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.

Upvotes: 1

gifnoc-gkp
gifnoc-gkp

Reputation: 1516

It should be

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

Upvotes: 0

Related Questions