PHP Echo 与选择列表语法错误


PHP Echo with a select list syntax error

有人可以帮我吗...我需要在回显中使用数据库中的选项回显选项列表,但我收到语法错误

有谁知道如何做到这一点,如果是这样,如果可能的话,要编辑的代码在下面?

回声开始

echo('
    <option value="0">'?><?php if($fDetail['arrange']=="opening") echo "selected="selected"; ?>'opening</option>
    <option value="1">'?><?php if($fDetail['arrange']=="closing") echo "selected="selected"; ?>'closing</option>

我已经按照埃利亚斯的建议做了,但是现在echo('很多表格细节......然后我来到一个下拉菜单,我需要从数据库条目中预填充,打开或关闭等等,只是为了示例而放 2 个,但有 5 个......

<select name="fDetails['.$mCount.'][arrange]" id="itemArrange'.$mCount.'" onKeyPress="return disableEnterKey(event)" >
<option value="0">'?> <?php if($fDetail['arrange']=="1") {echo 'selected="selected"' Opening</option>
<option value="1">'?> <?php if($fDetail['arrange']=="2") {echo 'selected="selected"' Closing</option>
</select>

第 2 阶段

我现在有以下代码,没有语法错误,但是,输出问题...为了方便起见,这里提供了回显开头的完整代码

            echo('
    <tr>
    <th colspan="3"><label>Item '.$mCount.'</label></th>
    </tr>
    <tr>
    <td colspan="1"><label for="fDetailTitle'.$mCount.'">Item Title:</label></td> 
    <td colspan="2"><input type="text" size="25" name="fDetails['.$mCount.'][title]" value="'.$fDetail['title'].'" id="fDetailTitle'.$mCount.'" onKeyPress="return disableEnterKey(event)" />
    </td>
    </tr>
    <tr>
    <td colspan="1"><label for="fDetailArtist'.$mCount.'">Item location:</label></td>
    <td colspan="2"><input type="text" size="25" name="fDetails['.$mCount.'][location]" value="'.$fDetail['location'].'" id="fDetailArtist'.$mCount.'" onKeyPress="return disableEnterKey(event)" />
    </td>
    </tr>
    <tr>
    <td colspan="1"><label for="fDetailitemStatus'.$mCount.'">item Status:</label></td>
    <td colspan="2">
    <select name="fDetails['.$mCount.'][arrange]" id="itemArrange'.$mCount.'" onKeyPress="return disableEnterKey(event)" >
<option value="0". $fDetail['arrange']=="Entry" ? 'selected="selected"':'' .'>Entry</option>
<option value="1". $fDetail['arrange']=="Exit" ? 'selected="selected"':'' .'>Exit</option> 
 </select>
</td>
    </tr>
    <tr>
    <td colspan="1"><label for="itemNotes'.$mCount.'">item Notes:</label></td>
    <td colspan="2"><input type="text" size="25" name="fDetails['.$mCount.'][notes]" value="'.$fDetail['notes'].'" id="itemNotes'.$mCount.'" onKeyPress="return disableEnterKey(event)" /></td>
    </tr>
    <tr>'
);

循环时,我在屏幕顶部重复>关闭>关闭>关闭>关闭>关闭>关闭,并且所有字段都变为itemNotes,其余的都消失了,我假设缺少/etra需要'或" ?

有点

不清楚你的代码实际上是什么样子的,但如果它看起来像这样:

echo ('<option value="0">'?><?php if (<condition>) echo 'string';?>

你处于一个麻烦的世界。 echo 不是一个函数,所以不要使用括号(虽然可以使用括号,它只是代码气味,恕我直言)。
您推送到echo构造的字符串就是这样:一个字符串,一个表达式,不能包含单独的语句(如if分支)。无论哪种方式,您的语法都是无效的。当然,允许使用三元,但即使你让它工作,还有很多问题需要解决。

生成的 HTML 将无效,或者至少:不是您想要的,因为您在回显 selected 属性之前关闭了 <option> 标记:

echo '<option value="0">'

因此,生成的标记将是:

<option value="0"> selected="selected"opening</option>

保持简单,为什么不简单地写:

?>//close php
<option value="0" <?php if (<condition>) echo 'selected="selected"';?>>value</option>

只需发布原始 html,无需echo它。
您可以使用 PHP 的短 echo 标签(与短标签不同!)和三元组进一步缩短:

<option value="0" <?= <condition> ? 'selected="selected"' : '' ?>>value</option>

语法突出显示会告诉您:

echo "selected="selected";

是一个问题,应该是

echo "selected='"selected'"";
//or
echo 'selected="selected"';
//or
echo "selected='selected'";

我对第二个有效示例有轻微(也许是个人)偏好。部分原因是双引号分隔的字符串实际上是解析的(用于变量替换),而单引号字符串不是:

$var = 'Something';
echo "I want to echo $var";//output: I want to echo something
echo 'I want to echo $var';//output: I want to echo $var

在以下情况下,您不能在回显中使用三元:

echo('
    <option value="0" '. $fDetail['arrange']=="opening" ? 'selected="selected"':'' .'>opening</option>
    <option value="1" ' . $fDetail['arrange']=="closing" ? 'selected="selected"':'' .'>closing</option>
');