2013-12-18 75 views
1

我有這張帶有複選框的表,我的想法是能夠刪除複選框已被選中的行。隱藏表中的一列

在Charaf jra的幫助下,我能夠POST該行的uniqID,所以我可以DELETE它在我的delete.php頁面上使用mysql查詢。

我現在的問題是爲了通過uniqID我不得不把它添加到桌子上,這看起來不錯。我的意思是,我不想讓桌上的ID號碼顯示出來。我一直在閱讀如何隱藏它,但我讀過的所有解釋都不適用於我的案例。

這裏是我的代碼更新:

if ($arch = $pdo->prepare("SELECT name, age, uniqID FROM table WHERE id = ?")) { 
    $arch ->execute(array($id)); 
    $data = $arch->fetchAll(); 
    echo '<div class="coolTable" ><form method="post" action="delete.php"><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>'; 
    foreach ($data as $row){ 
    echo '<tr>'; 
     foreach ($row as $col){ 
     $col=nl2br($col); 
     echo '<td>'.$col.'</td>'; 
     } 
    echo '<td><input type="checkbox" name="checkbox[]" value="'.$col.'" id="checkbox"></td>'; //this captures the column ID so I can pass it through the `POST` 
    echo '</tr>'; 
    } 
    echo '</table><input type="submit" value="Delete Selected"/></form></div>'; 
} 

這完美的作品。唯一的大問題是我不想顯示uniqID。任何人都可以告訴我如何從桌子上隱藏它,仍然可以通過POST

+1

會隱藏字段的工作:'」/>'? – newfurniturey

+1

記住它是一個'foreach'循環我正在處理,所以如果我添加這個代碼,我會隱藏每一個$ col – azirion

+0

在這種情況下,你到底想要達到什麼目的?你是否只想隱藏一些* ID,所有這些,其中之一?如果答案不是「全部」,那麼您需要添加一個「if」語句或兩個語句(待處理的內容)。 – newfurniturey

回答

1
if ($arch = $pdo->prepare("SELECT name, age, uniqID FROM table WHERE id = ?")) { 
    $arch ->execute(array($id)); 
    $data = $arch->fetchAll(); 
    echo '<div class="coolTable" ><form method="post" action="delete.php"><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>'; 
    foreach ($data as $row){ 
    echo '<tr>'; 
     echo '<td>'.nl2br($row['name']).'</td>'; 
     echo '<td>'.nl2br($row['age']).'</td>'; 
     echo '<td><input type="checkbox" name="checkbox[]" value="'.nl2br($row['uniqID']).'" id="checkbox"></td>'; //this captures the column ID so I can pass it through the `POST` 
    echo '</tr>'; 
    } 
    echo '</table><input type="submit" value="Delete Selected"/></form></div>'; 
} 

PHP數組可以用作字典(如果您已經看過python或ruby)。 PDO返回一組key-value對,其中鍵是列的名稱和值,即列的值。所以,你可以通過

  1. 迭代(的foreach)
  2. 迭代(指數 - 直接訪問,如果你知道你的元素的數組中的位置)訪問這些值
  3. 鍵(直接進入不知道的位置你的元素在陣列中)
1

假設你想循環每一行中的列,而不是像alkis那樣手動回顯它們,得到結果作爲關聯數組,然後取消設置$ row ['uniqID'] ;

if ($arch = $pdo->prepare("SELECT name, age, uniqID FROM table WHERE id = ?")) { 
    $arch ->execute(array($id)); 
    $data = $arch->fetch(PDO::FETCH_ASSOC); 
    echo '<div class="coolTable" ><form method="post" action="delete.php"><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>'; 
    foreach ($data as $row){ 
     if (isset($row['uniqID'])) { 
      unset($row['uniqID']); 
     } 
     echo '<tr>'; 
     foreach ($row as $col){ 
      $col = nl2br($col); 
      echo '<td>'.$col.'</td>'; 
     } 
     echo '<td><input type="checkbox" name="checkbox[]" value="'.$col.'" id="checkbox"></td>'; //this captures the column ID so I can pass it through the `POST` 
     echo '</tr>'; 
    } 
    echo '</table><input type="submit" value="Delete Selected"/></form></div>'; 
}