douwen5985 2015-08-10 06:27
浏览 27
已采纳

标记ifs的问题

Hello stackoverflow people, i need some information about what im doing. Is it possible to make 2 ifs in one tag? for example if(a > b) then a=a; else if (c>d) then a=c; else a=d; I'm trying to do something like this. Here it is the code:

echo '<select class="text" name="task_type">';
foreach ($task_types as $key => $value) {
    echo '<option value="'.$key.'"'.($key == $obj->task_type ? 'selected="selected"' : $key == '-1' ? 'selected="selected"' : '').'>'.$value.'</option>';
}
echo '</select>';

In the option tag you can see these ifs. But problem is that, when it eching. It selects -1 key, and the $obj->task_type key. But it should only pick this $obj->task_type.

  • 写回答

1条回答 默认 最新

  • duandu2980 2015-08-10 06:31
    关注

    There is a problem with placing of brackets. The correct syntax is -

    (expr1) ? (expr2) : (expr3)

     echo '<select class="text" name="task_type">';
     foreach ($task_types as $key => $value) {
        echo '<option value="'.$key.'"'.($key == $obj->task_type )?      'selected="selected"' : ($key == '-1') ? 'selected="selected"' : '' .'>'.$value.'</option>';
    }
    echo '</select>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮