-1

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.

i.one
  • 35
  • 1
  • 3
  • 9

1 Answers1

0

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.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • I would argue that using PHP within HTML is okay as long as it does not do any complicated processing or modify model data. I don't think you *need* a template engine to separate the responsibilities of your code. It does make it easier though. I personally quite like Twig. – Ben Guest Oct 21 '16 at 07:53
  • @BenGuest, I personally don't like Vincent van Gogh's paintings, although they are considered masterpieces. The PHP code mixed with HTML and CSS may look like a masterpiece to somebody, but not to me. I just won't argue. – Ruslan Osmanov Oct 21 '16 at 08:04
  • I understand it's a matter of opinion. I was just saying it's not bad practice to mix the two. – Ben Guest Oct 21 '16 at 08:11