azirion
azirion

Reputation: 109

Hidding a column from a table

I have this table with checkboxes, my idea is to be able to delete the rows where the checkboxes have been checked.

With the help of Charaf jra, I was able to POST the uniqID of the row so I could DELETE it using mysql query on my delete.php page.

My problem now is in order to pass the uniqID I had to add it on the table, which doesnt look good. I mean, I dont want the ID number showing on the table. I have been reading on how to hide it, but none of the explanations I have read apply to my case.

Here is my code updated:

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>';
}

This works perfect. The only big problem is that I dont want the uniqID to be shown. Can anyone tell me how to hide it from the table and still be able to pass it through the POST?

Upvotes: 1

Views: 101

Answers (2)

Dave
Dave

Reputation: 3658

Assuming you want to loop the columns in each row, as opposed to manually echoing them as alkis suggests, get the results as an associative array and then unset $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>';
}

Upvotes: 1

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

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 arrays can be used as dictionaries (if you've seen python or ruby). PDO returns an array of pairs key-value, where your key is the name your column and the value, the value of your column. So you can access those values via

  1. iteration (foreach)
  2. iteration (index - direct access if you know the position of your element inside the array)
  3. key (direct access without knowing the position of your element inside the array)

Upvotes: 1

Related Questions