语法错误。出乎意料的'如果'

时间:2016-11-29 08:02:47

标签: php html drop-down-menu

我想在 $ row [“gender”] ==选择选项时选择选项中的一个性别。我认为这样做的唯一方法是通过if else语句,但在这种情况下,'if'会产生错误。我认为我的语法可能有问题,还是因为单引号和双引号?

$output .= '  
                <tr class="datas">  
                     <td class="id" data-id10="'.$row["id"].'" contenteditable>'.$row["id"].'</td>
                     <td class="firstname" data-id1="'.$row["id"].'" contenteditable>'.$row["firstname"].'</td>  
                     <td class="middlename" data-id2="'.$row["id"].'" contenteditable>'.$row["middlename"].'</td>  
                     <td class="lastname" data-id3="'.$row["id"].'" contenteditable>'.$row["lastname"].'</td> 
                     <td class="address" data-id4="'.$row["id"].'" contenteditable>'.$row["address"].'</td> 
                     <td class="gender" data-id5="'.$row["id"].'" contenteditable>

                     <select onchange="getval(this)">

                     'if ($row["gender"]=="Male") {
                         echo '<option value="Male" selected>Male</option>
                     <option value="Female">Female</option>';
                     } else if ($row["gender"]=="Female") {
                       echo '<option value="Male">Male</option>
                     <option value="Female" selected>Female</option>';
                     } .'

                     </select>

                     </td> 

                     <td class="contact" data-id6="'.$row["id"].'" contenteditable>'.$row["contact"].'</td> 
                     <td class="username" data-id7="'.$row["id"].'" contenteditable>'.$row["username"].'</td> 
                     <td class="password" data-id8="'.$row["id"].'" contenteditable>'.$row["password"].'</td> 

                     <td class="but"><button type="button" name="delete_btn" id="delete_btn" data-id9="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">DELETE</button>
                      <button type="button" name="delete_btn" id="update_btn" data-id12="'.$row["id"].'">UPDATE</button>
                      </td>
                </tr>  
           '; 

3 个答案:

答案 0 :(得分:1)

将代码拆分为多个语句:

$output .= '  
<tr class="datas">  
    <td class="id" data-id10="'.$row["id"].'" contenteditable>'.$row["id"].'</td>
    <td class="firstname" data-id1="'.$row["id"].'" contenteditable>'.$row["firstname"].'</td>  
    <td class="middlename" data-id2="'.$row["id"].'" contenteditable>'.$row["middlename"].'</td>  
    <td class="lastname" data-id3="'.$row["id"].'" contenteditable>'.$row["lastname"].'</td> 
    <td class="address" data-id4="'.$row["id"].'" contenteditable>'.$row["address"].'</td> 
    <td class="gender" data-id5="'.$row["id"].'" contenteditable>
    <select onchange="getval(this)">
     ';
if ($row["gender"]=="Male") {
    $output .= '<option value="Male" selected>Male</option>
    <option value="Female">Female</option>';
} else if ($row["gender"]=="Female") {
    $output .= '<option value="Male">Male</option>
    <option value="Female" selected>Female</option>';
}
$output .= '
        </select>
    </td> 

    <td class="contact" data-id6="'.$row["id"].'" contenteditable>'.$row["contact"].'</td> 
    <td class="username" data-id7="'.$row["id"].'" contenteditable>'.$row["username"].'</td> 
    <td class="password" data-id8="'.$row["id"].'" contenteditable>'.$row["password"].'</td> 

    <td class="but"><button type="button" name="delete_btn" id="delete_btn" data-id9="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">DELETE</button>
    <button type="button" name="delete_btn" id="update_btn" data-id12="'.$row["id"].'">UPDATE</button>
    </td>
</tr>  
'; 

答案 1 :(得分:1)

我建议像这样重写部分代码:

            autoreleasepool(invoking: { 
                let result = self.detect(image: image)
                // do other things
            })

Ternary Operator

答案 2 :(得分:1)

您无法在字符串中添加$output = ' ... <select onchange="getval(this)"> <option value="Male" ' . ( $row["gender"]=='Male' ? 'selected' : '' ) . '>Male</option> <option value="Female" ' . ( $row["gender"]=='Female' ? 'selected' : '' ) . '>Female</option> </select> ...'

if

您也可以使用它:

<?php
if ($row["gender"] == "Male") {
    $gender = '<option value="Male" selected>Male</option><option value="Female">Female</option>';
} else {
    $gender = '<option value="Male">Male</option><option value="Female" selected>Female</option>';
} 

$output .= '  
<tr class="datas">  
        <td class="id" data-id10="'.$row["id"].'" contenteditable>'.$row["id"].'</td>
        <td class="firstname" data-id1="'.$row["id"].'" contenteditable>'.$row["firstname"].'</td>  
        <td class="middlename" data-id2="'.$row["id"].'" contenteditable>'.$row["middlename"].'</td>  
        <td class="lastname" data-id3="'.$row["id"].'" contenteditable>'.$row["lastname"].'</td> 
        <td class="address" data-id4="'.$row["id"].'" contenteditable>'.$row["address"].'</td> 
        <td class="gender" data-id5="'.$row["id"].'" contenteditable>

        <select onchange="getval(this)">' . $gender . '</select>

        </td> 

        <td class="contact" data-id6="'.$row["id"].'" contenteditable>'.$row["contact"].'</td> 
        <td class="username" data-id7="'.$row["id"].'" contenteditable>'.$row["username"].'</td> 
        <td class="password" data-id8="'.$row["id"].'" contenteditable>'.$row["password"].'</td> 

        <td class="but"><button type="button" name="delete_btn" id="delete_btn" data-id9="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">DELETE</button>
        <button type="button" name="delete_btn" id="update_btn" data-id12="'.$row["id"].'">UPDATE</button>
        </td>';