I’ve been trying to customize a portion of PHP script for this.
I want it to display image if the input value is in number (1, 3, 20, 99, or etc). Else, I want it to display text if the input value is in text (lorem, randomtext, or etc). And I want it to display nothing if the value is 0.
But I keep getting it wrong. It always displays text, no matter what the input value is.
Here is the PHP code:
if ( $topic['topic_icon'] = $topic['icon_id'] ) {
if (is_numeric($topic['icon_id'])) {
"<img src='".$ibforums->vars['img_url']."/icon{$topic['icon_id']}.gif' alt='{$topic['icon_id']}' />";
} else {
"<span class='text-icon {$topic['icon_id']}'>{$topic['icon_id']}</span>";
}
} else {
"";
}
And here is a portion of the HTML where user can input the value (it’s with radio button, the <form /> tag is not included since it’s a part of huge chunk of code):
<input type="radio" class="radiobutton" name="iconid" value="15" /> <img src="{$ibforums->vars['img_url']}/icon15.gif" align='middle' alt='' />
<input type="radio" class="radiobutton" name="iconid" value="16" /> <img src="{$ibforums->vars['img_url']}/icon16.gif" align='middle' alt='' />
<input type="radio" class="radiobutton" name="iconid" value="17" /> <img src="{$ibforums->vars['img_url']}/icon17.gif" align='middle' alt='' /><br />
<input type="radio" class="radiobutton" name="iconid" value="SPRING" /> <span class='text-icon SPRING'>SPRING</span>
<input type="radio" class="radiobutton" name="iconid" value="SUMMER" /> <span class='text-icon SUMMER'>SUMMER</span>
<input type="radio" class="radiobutton" name="iconid" value="AUTUMN" /> <span class='text-icon AUTUMN'>AUTUMN</span>
<input type="radio" class="radiobutton" name="iconid" value="WINTER" /> <span class='text-icon WINTER'>WINTER</span><br />
<input type="radio" class="radiobutton" name="iconid" value="0" checked="checked" /> [ Use None ]
The column used in MySQL table (it’s icon_id) is set as varchar(64).
I apologize for the long title…
OK, I know I shouldn’t try to code when I’m sleepy… this is pure of my stupidity.
This is the original code:
I should’ve un-shortcut it first. Without the ternary operator shortcut, it should’ve been written as this:
And then, to achieve what I need, it just needs a little additional
IF:Ta-da, works like a charm. Not sure though if this is the best practice (I feel it could’ve been done better). But it still works anyway.
Thank you for @jtheman as his answer hints me what I have done it wrong and especially thanks @JohnT ( https://stackoverflow.com/a/1080257 ) to make me realize where I did wrong.