NKL

PHP Checkbox iteration

NKL
Jul 5th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. <?php
  2.     // Following example is part of a response at the following url.
  3.     // http://www.hamsterpaj.net/diskussionsforum/gaa_till_post.php?post_id=4265149
  4.    
  5.     // Items to iterate through (sql-key => post-value)
  6.     $postItems = array(
  7.         'newsadmin',
  8.         'forumpolis',
  9.         'moderator',
  10.         'memberadmin',
  11.         'superadmin'
  12.     );
  13.  
  14.     // Check if each individual item is set and assign an integer for state
  15.     // User altered information will be disregarded and changed to either a 0 or 1
  16.     // depending on the key being assigned or not, so no filtering necessary
  17.     foreach ($postItems as $postItem){
  18.         $_POST[$postItem] = (!isset($_POST[$postItem])) ? 0 : 1;
  19.     }
  20.    
  21.     // Cast userID to an integer only, non-numerical information will return 0
  22.     $_POST['userID'] = (int)$_POST['userID'];
  23.    
  24.     // Construct query
  25.     $sql = 'UPDATE user_groups SET news='.$_POST['newsadmin'].', forum='.$_POST['forumpolis'].', moderator='.$_POST['moderator'].', user='.$_POST['memberadmin'].', super='.$_POST['superadmin'].' WHERE userID='.$_POST['userID'];
  26.  
  27.     // Dump variable
  28.     var_dump($sql);
  29. ?>
Advertisement
Add Comment
Please, Sign In to add comment