简体   繁体   English

在 select 选项标签中使用三元运算符

[英]Using ternary operator inside select option tag

Currently I have a select box that is filling in its data from the controller class.目前我有一个 select 框,它正在从 controller class 中填写其数据。 As of now all the values are filling in properly as intended, but there are some null values for firstname for which I'm trying to add a ternary operator to replace it with email.截至目前,所有值都按预期正确填写,但有一些 null 值作为firstname ,我正在尝试添加一个三元运算符以将其替换为 email。

This doesn't seem to work in my controller class as I'm placing the parentheses wrong.这似乎在我的 controller class 中不起作用,因为我把括号放错了。

Here's my current code:这是我当前的代码:

public function getContacts($id){
    $option = "<option value='0'>Select</option>";

    $modelList = $this->listings_model->get_contact(array('contact_type'=>3),'firstname,lastname,email,refno');  
    foreach($modelList as $m){
        $option .= "<option value='".$m['firstname']." ".$m['lastname']." - ".$m['refno']."' id='".$m['id']."'>".$m['firstname']." ".$m['lastname']." - ".$m['refno']."</option>";
    }

    echo json_encode($option);
} 

I've tried the following but I'm getting a syntax error:我尝试了以下方法,但出现语法错误:

$option .= "<option value='"($m['firstname'].$m['lastname'] ? $m['firstname']." ".$m['lastname']." - ".$m['refno'] : $m['email'])"' id='".$m['id']."'>".$m['firstname']." ".$m['lastname']." - ".$m['refno']."</option>";

What I want to achieve is this:我想要实现的是:

value="<?php echo ($ow['firstname'].$ow['lastname']?$ow['firstname'].' '.$ow['lastname']:($ow['email']?$ow['email']:$ow['mobile'])).' - '.$ow['refno']; ?>"

$m['firstname'].$m['lastname'] is the evil. $m['firstname'].$m['lastname']是邪恶的。 You should be checking for null value for only $m['firstname'] instead of the both.您应该只检查$m['firstname']而不是两者的null值。

Also, it should be:此外,它应该是:

$option .= "<option value='".($m['firstname'] !== null ? $m['firstname']." ".$m['lastname']." - ".$m['refno'] : $m['email'])."' id='".$m['id']."'>".($m['firstname'] !== null ? $m['firstname']." ".$m['lastname']." - ".$m['refno'] : $m['email'])."</option>";

We have these things called variable that help code become readable.我们有这些称为变量的东西,可以帮助代码变得可读。 Also, parentheses can help identify groups of variables with operations between them in a ternary operator.此外,括号可以帮助识别变量组,它们之间在三元运算符中进行运算。 You have several misplaced ' and " in your code, both in PHP and in HTML.在 PHP 和 HTML 中,您的代码中有几个错位'"

From what I understand from your requirements the following code does this: if firstname and lastname are not empty it will use <firstname> <lastname> - <ref> for the value and text of the option else it will use <email> if email is not empty else <mobile>根据我对您的要求的了解,以下代码执行此操作:如果 firstname 和 lastname 不为空,它将使用<firstname> <lastname> - <ref>作为选项的值和文本,否则它将使用<email>如果 email不为空,否则<mobile>

$name = ( $m['firstname'] && $m['lastname'] ) ? ( $m['firstname'] . ' ' . $m['lastname'] ) : ( '' );
$value = $name ? ($name . ' - ' . $m['refno']) : ($m['email'] ? $m['email'] : $m['mobile']);
$id = $m['id'];
$option .= "<option value=\"$value\" id=\"$id\">$value</option>";

By the way, you cannot json_encode an HTML string.顺便说一句,您不能对 HTML 字符串进行 json_encode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM