2013-12-18 72 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>'; 
}