-3

Im creating option with if, because if need that on load item would b selected by the current view. I dont need JS. I was trying to do this, but i get error because of if, i done everything right i think here it is:

echo ('<option value="?m=ticketsmith&ticket_company=' . $row['company_id'] . '&type='.$type.'"'. if ($show_uid == $row['company_id']) { echo ' selected="selected"';}'/>' . $row['company_id'].', ' . $row["company_name"]) ;

I hope you guys will help me. And here is error messege: Parse error: syntax error, unexpected 'if' (T_IF) in

TorresAlGrande
  • 213
  • 1
  • 14
  • Issue is that you are trying to use a `if(){}` inside a echo (and even echo inside a echo). Which you can't. – Epodax Jul 10 '15 at 08:18
  • you can make variable with all parts of your Option element, paste every part of string to that var and in the end just do echo $foobar; cause you cant do if statement in echo (you can eventually do _($foobar==true)? 'true' : 'false'_ – Buzka91 Jul 10 '15 at 08:19

2 Answers2

2

You can't use echo inside echo.

Try following:

$isSelected = '';

if ($show_uid == $row['company_id']) {
    $isSelected = ' selected="selected"';
}

echo ('<option value="?m=ticketsmith&ticket_company=' . $row['company_id'] . '&type='.$type.'"'. $isSelected '/>' . $row['company_id'].', ' . $row["company_name"]);

Hope this helps!!

Reena Parekh
  • 933
  • 1
  • 8
  • 24
1
echo ('<option value="?m=ticketsmith&ticket_company=' . $row['company_id'] . '&type='.$type.'"'.  ($show_uid == $row['company_id'] ? ' selected="selected"' : '' ) . '/>' . $row['company_id'].', ' . $row["company_name"]) ;

The change was this

if ($show_uid == $row['company_id']) { echo ' selected="selected"';}

to this

($show_uid == $row['company_id'] ? ' selected="selected"' : '' )
jmattheis
  • 10,494
  • 11
  • 46
  • 58
CT14.IT
  • 1,635
  • 1
  • 15
  • 31