Re: Handling Large Check Box Data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 17, 2006 1:21 pm, Rahul S. Johari wrote:
> I¹m a little confused as to what¹s the best way to handle this.
> I have a form which, apart from lots of other fields, has a set of 25
> ­ 30
> Check Boxes, each of which asks the user for some kind of information
> which
> the user can check or leave unchecked.
> The information each Check Box collects will also appear in the
> ³Listing²
> for users to view once the user has completed & submitted the form.
> Furthermore, there is an Advanced Search also available to users which
> will
> also provide the same 25 - 30 check boxes which define the search
> criteria.
> I¹m not sure what¹s the best, most efficient way to do this.
>
> The tedious way to do this is to make 30 fields in the mySQL database,
> and
> if the check box is checked, the data goes into the corresponding
> field...
> And similarly listing the data from the field if field is
> non-empty.... And
> similarly including each field in the Search options.
>
> I want suggestions for a better/faster way to do this. I did think
> about
> creating a single field and storing the data from each ?checked¹ check
> box
> as comma separated values in the single field. I¹m not sure how to do
> that
> and if that¹s the best way.... But even if I can, I¹m not sure how to
> get
> the data to display separately out of that field in the Listings view
> and
> more importantly how to include that data in the Search options.

You could combine up to 32 bits into a 32-bit integer, and store the
choices as an 'int'

Something like:
<?php
  $choices = array('box1', 'box2', 'whatever', 'moreboxes');
  $data = 0;
  foreach($choices as $choice){
    if (isset($_POST['checkbox'][$choice]){
      $data = 2 * $data + 1;
    }
    else{
      $data = 2 * $data + 0;
    }
  }
?>

For nicer syntax, you could use those >> and << operators for
bit-shifting instead of that goofy "2 *" I used, because I was too
lazy to RTFM to lookup the syntax for << and >>

But since you are right on the edge of needing over 32 bits, this is
probably not such a good idea...

You could also just use a string with the same basic framework:

$data = '';
foreach (...){
  if (isset(...)){
    $data .= '1';
  }
  else{
    $data .= '0';
  }
}

This is not real flexible when you add more choices, however.

Probably the CORRECT way to do it is to store the values in another
table, with an id field back to the current table.

This will provide the most flexibility in the long run, and probably
be more maintainable and have clearer code.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux