2016-11-23 120 views
0
的數組數據

我有一個表單,用戶可以輸入部門名稱,然後是多個選擇框,其中多個員工職位/職位正在此部門工作。PHP/MySQLi - 顯示來自表

enter image description here

我現在正在尋找一種方式,讓用戶編輯自己提交的部門和:然後將記錄通過foreach()循環,這是給我這個結果保存到我的SQL表因此想要在一個字段中再次顯示存儲的記錄,這留下了將數組數據返回到選擇字段的問題...

我試圖用另一個foreach填充<select>字段,但是到目前爲止沒有運氣。我不知道,如果問題僅僅是糟糕的代碼,或者如果選擇二插件在這裏給我的問題,以及...

PHP(UPDATE2)

<?php 
    // Start MySQLi connection 
    $db = new mysqli($dbhost,$dbuser,$dbpass,$dbname); 

    if($db->connect_errno > 0){ 
     die('Unable to connect to database [' . $db->connect_error . ']'); 
    } 

    // Build basic query, Admins can see all records 
    $sql = ("SELECT * FROM qci_departments GROUP BY Department ORDER BY Department"); 

    // run the query or show an error message 
    if(!$result = $db->query($sql)){ 
     echo('There was an error selecting data from the table [' . $db->error . ']'); 
    } 

    while($row = mysqli_fetch_array($result)){ 
     $dept_id = $row['ID']; 
     $dept_name = $row['Department']; 
     $dept_positions = $row['Positions']; 
     echo " 
      <tr> 
       <td> 
        <input type=\"text\" class=\"form-control\" id=\"editDeptName\" name=\"editDeptName\" value=\"$dept_id\"> 
       </td> 
       <td>$dept_name</td> 
       <td> 
        <select id=\"editDeptPositions\" name=\"editDeptPositions\" class=\"form-control\" multiple> 
         <option value='".$dept_positions."'>".$dept_positions."</option>"; 
         //$position_query = ("SELECT distinct Positions FROM qci_departments"); 
         $position_query = ("SELECT distinct Positions FROM qci_departments where Department = '".$row['Department']."'"); 
         if(!$result_positions = $db->query($position_query)){ 
          echo('There was an error selecting data from the table [' . $db->error . ']'); 
         } else { 
          while($row_positions = mysqli_fetch_assoc($result_positions)){ 
           echo "<option value='".$row_positions['Positions']."'>".$row_positions['Positions']."</option>"; 
          } 
         } 
        echo "</select> 
       </td> 
      </tr>"; 

任何人都可以點我在正確的方向,請? 謝謝 A2K

編輯:更新的代碼,以反映海報下方

+0

似乎確定?檢查你有'echo'

";print_r($dept_positions);echo"
「;' –

+0

您期望的值你在一段時間內在這種情況下foreach是無用的。 –

+0

@RafaelShkembi確實如此,應該是 'echo「」;' –

回答

1

建議改變我給你兩個解決方案

第一:

while($row = mysqli_fetch_array($result)){ 
    $dept_id = $row['ID']; 
    $dept_name = $row['Department']; 
    $dept_positions = $row['Positions']; 
    echo " 
     <tr> 
      <td> 
       <input type=\"text\" class=\"form-control\" id=\"editDeptName\" name=\"editDeptName\" value=\"$dept_id\"> 
      </td> 
      <td>$dept_name</td> 
      <td> 
       <select id=\"editDeptPositions\" name=\"editDeptPositions\" class=\"form-control select2\"> 
        <option value='".$dept_positions."'>".$dept_positions."</option>"; 
        $position_query = ("SELECT distinct Positions FROM qci_departments"); 
        if(!$result_positions = $db->query($position_query)){ 
         echo('There was an error selecting data from the table [' . $db->error . ']'); 
        } else { 
         while($row_positions = mysqli_fetch_assoc($result_positions)){ 
          echo "<option value='".$row_positions['Positions']."'>".$row_positions['Positions']."</option>"; 
         } 
        } 
       echo "</select> 
      </td> 
     </tr>"; 
} 

撥打第二查詢在此期間獲得職位並填充選擇選項。

二://我喜歡這一個

$positions_arr = array(); 
$position_query = ("SELECT distinct Positions FROM qci_departments"); 
if(!$result_positions = $db->query($position_query)){ 
    echo('There was an error selecting data from the table [' . $db->error . ']'); 
} else { 
    while($row_positions = mysqli_fetch_assoc($result_positions)){ 
     $positions_arr[] = $row_positions['Positions']; 
    } 
} 
while($row = mysqli_fetch_array($result)){ 
    $dept_id = $row['ID']; 
    $dept_name = $row['Department']; 
    $dept_positions = $row['Positions']; 
    echo " 
     <tr> 
      <td> 
       <input type=\"text\" class=\"form-control\" id=\"editDeptName\" name=\"editDeptName\" value=\"$dept_id\"> 
      </td> 
      <td>$dept_name</td> 
      <td> 
       <select id=\"editDeptPositions\" name=\"editDeptPositions\" class=\"form-control select2\"> 
        <option value='".$dept_positions."'>".$dept_positions."</option>"; 
        foreach($positions_arr as $value){ 
         echo "<option value='".$value."'>".$value."</option>"; 
        } 
       echo "</select> 
      </td> 
     </tr>"; 
} 

使所有位置的陣列,後來做而裏面的foreach來填充選擇選項

+0

謝謝,但這兩種解決方案都不適合我。我有一個帶有多個元素的'select'字段,如果我遵循第二個選項,則選擇字段保持空白。如果我使用選項1,它只填充有關部門的第一個位置,但錯過其他部分。我更新了我的代碼以反映更改。謝謝! – Armitage2k

+0

在這種情況下,您可以使用第一個,但您需要修改查詢。讓我更新我的代碼 –

+1

將第一個解決方案中的查詢更改爲this-> $ position_query =(「SELECT distinct positions FROM qci_departments where Department ='」。$ row ['Department']。「'」);更新了 –