开发者

What Is The Syntax Error In The Provided Code:

$pollPart .= '
<tr>
<td valign="top" nowrap="nowrap">'.$question['question'].'</td>
<td valign="top" height="10" width="100%" style="padding: 0px 10px;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" width="'.$percentage.'%" '.if($percentage > 0){$pollPart .= 'style="background: url(\'/includes/poll/images/bar.jpg\') repeat-x;"';}.'><img src="/includes/poll/images/dot.gif" width="1" height="19" /></td>
<td valign="top" width="'.$percentage2.'%"></td>
</tr>
</table>
</td>
<td valign="top">'.$responses['total'].'</td>
</tr>
';

I am getting the Syntax on the following line:

<td valign="top" width="'.$percentage.'%" '.if($percentage > 0){$pollPart .= 'style="background: url(\'/includes开发者_如何学Go/poll/images/bar.jpg\') repeat-x;"';}.'><img src="/includes/poll/images/dot.gif" width="1" height="19" /></td>

Specifically this part:

if($percentage > 0){$pollPart .= 'style="background: url(\'/includes/poll/images/bar.jpg\') repeat-x;"';}

Any input on this would be greatly appreciated, Thank You!


You can't use if condition inside of string that is assigned into variable. You can try:

(($percentage > 0)?'style="background: url(\'/includes/poll/images/bar.jpg\')':'')


You can't do if-blocks inside of a concatenation of strings. If you want to do an inline ternary if/else, the syntax is a little different.

Instead of

... if($percentage > 0){$pollPart .= 'style="background: url(\'/includes/poll/images/bar.jpg\') repeat-x;"';} ...

You would structure it like CONDITION ? TRUE-PART : FALSE-PART:

... (($percentage > 0) ? 'style="background: url(\'/includes/poll/images/bar.jpg\') repeat-x;"' : '') ...

I use an empty string ('') since no false condition is needed, but something needs to be returned in order to complete the statement and to concatenate with the rest of the string.

Alternatively, you can end your string of concatenations, place the if block after it, then start again with $pollPart .=.


$pollPart .= '
<tr>
<td valign="top" nowrap="nowrap">'.$question['question'].'</td>
<td valign="top" height="10" width="100%" style="padding: 0px 10px;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" width="'.$percentage.'%"';

if($percentage > 0){
    $pollPart .= ' style="background: url(\'/includes/poll/images/bar.jpg\') repeat-x;"';
}

$pollPart .='><img src="/includes/poll/images/dot.gif" width="1" height="19" /></td>
<td valign="top" width="'.$percentage2.'%"></td>
</tr>
</table>
</td>
<td valign="top">'.$responses['total'].'</td>
</tr>'; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜