2011-03-17 37 views
0

我现在有一些问题,我的代码有一些奇怪的行为,无法找到原因。下拉列表奇怪地显示出表

我有一个分机表(电话)分配给一个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>&nbsp;</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>&nbsp;</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> 

是否有人知道它可能是什么问题?

回答

1

我认为这是因为嵌套的回声语句。所以在PHP缓存第一个回显之前,它会显示第二个回显,因为它在里面。我认为解决方案是构建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; 
} 

我没有测试过,但我相信它会工作。

+0

谢谢,它的工作原理,我不知道这种行为 – 2011-03-17 10:15:47

0

这是因为echo的所有代码在输出最终sting之前得到执行。该函数在父代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>'; 
    } 
+0

感谢的是,现在的工作! – 2011-03-17 10:15:16