我现在遇到了一些问题,我的代码有些奇怪的行为,无法找到原因。
我有一张分配给qeue的分机表(电话)。我通过SQL查询获取数据,然后在屏幕上显示它。 其中一个显示的数据是优先级,我希望通过选择下拉列表中的新选项来更改jquery + ajax。
所以,我管理了这段代码:
echo '<table cellpadding="5" cellspacing="0" width="30%" id="deletion" bgcolor="#f6f6f6" style="border:1px solid #cccccc;">
<tr>
<th><strong>Extension</strong></th>
<th><strong>Priority</strong></th>
<th> </th>
</tr>';
while ($datos_extension = mysql_fetch_array($result)){
echo '<tr id="'.$extension_data['id'].'"><td>'.$extension_data['extension'].'</td><td>'.show_priority($datos_extension['priority']).'</td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr>';
}
echo "</table>";
现在,功能:
function show_priority ($priority) {
$max_priority = 3;
$cont = 1;
echo '<select name="priority" class="priority">';
while ($cont <= $max_priority) {
echo '<option';
if ($cont == $priority)
echo " selected";
echo ' value="'.$cont.'">'.$cont.'</option>';
$cont ++;
}
echo "</select>";
}
但是它显示了表格中的下拉菜单......这是HTML输出:
<tr>
<th><strong>Extension</strong></th>
<th><strong>Priority</strong></th>
<th> </th>
</tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="2"><td>1002</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="3"><td>1003</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="4"><td>1004</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="5"><td>1005</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="6"><td>1006</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="7"><td>1007</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="8"><td>1008</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option selected value="1">1</option><option value="2">2</option><option value="3">3</option></select><tr id="9"><td>1009</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr><select name="priority" class="priority"><option value="1">1</option><option selected value="2">2</option><option value="3">3</option></select><tr id="10"><td>9003</td><td></td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr></table>
有人知道问题出在哪里吗?
答案 0 :(得分:1)
我认为这是因为嵌套的echo语句。所以在PHP缓存第一个echo之前它会显示第二个,因为它在里面。我认为该解决方案是在show_priority
函数内部构建字符串并返回它,如:
function show_priority ($priority) {
$max_priority = 3;
$cont = 1;
$return = '<select name="priority" class="priority">';
while ($cont <= $max_priority) {
$return .= '<option';
if ($cont == $priority)
$return .= " selected=selected"; //this should be done like this in proper HTML
$return .= ' value="'.$cont.'">'.$cont.'</option>';
$cont ++;
}
$return .= "</select>";
return $return;
}
我没有测试过,但我相信它会起作用。
答案 1 :(得分:0)
这是因为echo
的所有代码在输出最终刺痛之前都会被执行。该函数在父echo
填充表行之前回显列表。您需要更改此功能才能将其插入其他字符串中:
function show_priority ($priority) {
$max_priority = 3;
$cont = 1;
$output = '';
$output .= '<select name="priority" class="priority">';
while ($cont <= $max_priority) {
$output .= '<option';
if ($cont == $priority)
$output .= " selected";
$output .= ' value="'.$cont.'">'.$cont.'</option>';
$cont ++;
}
$output .= "</select>";
return $output;
}
但毕竟最好将列表作为数组或对象返回,然后在生成所有内容后单独填充它;将数据库逻辑与设计分开。
如果由于某种原因您无法改变功能,您可以使用输出缓冲:
while ($datos_extension = mysql_fetch_array($result)){
ob_start();
show_priority($datos_extension['priority']);
echo '<tr id="'.$extension_data['id'].'"><td>'.$extension_data['extension'].'</td><td>'.ob_get_clean().'</td><td><a href="#" class="delete negative button"><span class="cross icon"></span>Delete</a></td></tr>';
}