i.one i.one - 1 month ago 5
PHP Question

syntax error, unexpected 'if' (T_IF),how to resolve

i create statement in class,but syntax error and unexpected 'if'
this is my code



$row->actions = '
<div class="btn-group">
<ul class="dropdown-menu" role="menu">
'.generate_button('activity', 'edit', '<li class='". if('.$row->status==1).'{echo 'hidden'}"'>
<a href="'.site_url('activity/edit_activity/'.$row->activity_id).'"> <i class="fa fa-pencil"></i> Edit </a> </li>').'
'.generate_button('activity', 'view', '<li>
<a href="'.site_url('activity/view/'.$row->activity_id).'"> <i class="fa fa-file"></i> View </a> </li>').'
</ul></div>';





so,how to resolve it.

Answer

The if construct doesn't work in concatenation expressions, as it doesn't return a value. You can use the ternary operator instead:

generate_button('activity', 'edit',
  '<li class="' . ($row->status==1 ? 'hidden' : '') . '">...');

Another issue is wrong order of quotes (or unescaped quotes, if you like):

'<li class='". if('.$row->status==1).'{echo 'hidden'}"'>...'

The fixed version:

'<li class="' . ($row->status==1 ? 'hidden' : '') . '">...'

Finally, consider using a template engine such as Smarty instead of mixing HTML with PHP.

Comments