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