0

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" />&nbsp;&nbsp;<img src="{$ibforums->vars['img_url']}/icon15.gif"  align='middle' alt='' />
<input type="radio" class="radiobutton" name="iconid" value="16" />&nbsp;&nbsp;<img src="{$ibforums->vars['img_url']}/icon16.gif"  align='middle' alt='' />
<input type="radio" class="radiobutton" name="iconid" value="17" />&nbsp;&nbsp;<img src="{$ibforums->vars['img_url']}/icon17.gif"  align='middle' alt='' /><br />
<input type="radio" class="radiobutton" name="iconid" value="SPRING" />&nbsp;&nbsp;<span class='text-icon SPRING'>SPRING</span>
<input type="radio" class="radiobutton" name="iconid" value="SUMMER" />&nbsp;&nbsp;<span class='text-icon SUMMER'>SUMMER</span>
<input type="radio" class="radiobutton" name="iconid" value="AUTUMN" />&nbsp;&nbsp;<span class='text-icon AUTUMN'>AUTUMN</span>
<input type="radio" class="radiobutton" name="iconid" value="WINTER" />&nbsp;&nbsp;<span class='text-icon WINTER'>WINTER</span><br />
<input type="radio" class="radiobutton" name="iconid" value="0" checked="checked" />&nbsp;&nbsp;[ Use None ]

The column used in MySQL table (it's icon_id) is set as varchar(64).

I apologize for the long title...

deathlock
  • 2,756
  • 5
  • 27
  • 48

2 Answers2

1

Your initial IF condition contain a =, that is not a comparison operator but should be ==

if ( $topic['topic_icon'] == $topic['icon_id']  ) {

Then you seem to be missing the echo command:

 if (is_numeric($topic['icon_id'])) {
            echo "<img src='".$ibforums->vars['img_url']."/icon{$topic['icon_id']}.gif' alt='{$topic['icon_id']}' />";
            } else {
            echo "<span class='text-icon {$topic['icon_id']}'>{$topic['icon_id']}</span>";
            }
} else {
        echo  "";
         }

You does not specify how the contents of $topic array is set. Possible typo is the radio buttons name iconid but $topic['icon_id'] (with _)

jtheman
  • 7,421
  • 3
  • 28
  • 39
  • Doesn't seem to have any difference, it still displays as text even with the `echo` command. And having `==` instead of `=` makes it shows nothing at all.. – deathlock Oct 07 '12 at 20:12
  • Well you can't use just = anyway. Makes the IF condition fail! What is in your $topic array then? – jtheman Oct 07 '12 at 20:14
  • I'm not sure... I'm only modifying a PHP script and everything else inside the function seem to use only `=`. Here is the function: http://pastebin.com/fYk35FEJ I included the original code which I'm trying to modify there. It's working with the original (but it can only display image), while it's not working with my modification. – deathlock Oct 07 '12 at 20:20
  • Your modified script is wrong. Please paste the original. Also, what are you trying to do? Why should `topic_icon` equal `icon_id`? Sounds like a stupid `if-statement` to me. – OptimusCrime Oct 07 '12 at 20:24
  • In what context is render_entry() used? Also the IF condition have been wrong also before then... – jtheman Oct 07 '12 at 20:27
  • Ah, I've found the solution. It's mostly part of my stupidity. Thanks a lot for the answer, it hints me. :) – deathlock Oct 07 '12 at 21:05
0

OK, I know I shouldn't try to code when I'm sleepy... this is pure of my stupidity.

This is the original code:

$topic['topic_icon']     = $topic['icon_id']
                          ? "<img src='".$ibforums->vars['img_url']."/icon{$topic['icon_id']}.gif' alt='{$topic['icon_id']}' />&nbsp;&nbsp;"
                          : "";

I should've un-shortcut it first. Without the ternary operator shortcut, it should've been written as this:

if ( $topic['icon_id'] ) {
                $topic['topic_icon'] =  "<img src='".$ibforums->vars['img_url']."/icon{$topic['icon_id']}.gif' alt='{$topic['icon_id']}' />&nbsp;&nbsp;";
        } else {
                $topic['topic_icon'] = "";
        }

And then, to achieve what I need, it just needs a little additional IF:

if ( $topic['icon_id'] ) {
            if (is_numeric($topic['icon_id'])) {
                $topic['topic_icon'] =  "<img src='".$ibforums->vars['img_url']."/icon{$topic['icon_id']}.gif' alt='{$topic['icon_id']}' />&nbsp;&nbsp;";
            } else {
                $topic['topic_icon'] =  "<span class='text-icon {$topic['icon_id']}'>{$topic['icon_id']}</span>";
            }
        } else {
                $topic['topic_icon'] = "";
        }

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.

Community
  • 1
  • 1
deathlock
  • 2,756
  • 5
  • 27
  • 48