Jump to content

Getting multiple checked values from the database in a checkbox help


TeddyKiller

Recommended Posts

I see what you mean Nightslyr, though radio buttons wouldn't quite do the trick would it..

I mean.. radio buttons run off 1 check only, the rest remain unchecked. No matter which one is checked. That only limits someone to be interested in 1 option... which limits me for example?

 

Exactly right, which is why it's more logical to do it that way.  Given the choices:

 

Men - Women - Both - None

 

If someone is interested in, say, women, then logically it's impossible for them to choose none as well.  They can't have it both ways.  Radio buttons enforce this behavior, and save you from having to check for those kinds of logically impossible combinations.

 

It just looks like you're trying to over-design it, IMO.  Checkboxes aren't a one-size-fits-all tool.

Ken2k7, in the code you gave me, how does it display it as collums? I was wondering if it was possible to migrate it to display as rows?

<tr><td> checkbox one </td><td> checkbox two</td> so on.

If so, i'll appreciate it.

This one

echo '<tr>';
function GenerateInputOption ($name, $value, $text, $IsSelected) {
     $selected = $IsSelected ? 'checked="checked"' : '';
     return sprintf('<input type="checkbox" name="%s" value="%s" %s />%s', $name, $value, $selected, $text);
}

$eths = array(
     'Column1' => array('width' => 137, 'asian' => 'Asian', ...),
     'Column2' => array('width' => 124, 'east indian' => 'East Indian', ...)
);
foreach ($eths as $key => $value) {
     if (is_array($value)) {
          echo sprintf('<td width="%d">', $value['width']);
          foreach ($value as $eth => $option)
               if (strcmp($eth, "width") != 0) echo GenerateListOption('ethnicity[]', $eth, $option, false);
          echo '</td>';
     }
}
echo '</tr>';

Replace

          echo sprintf('<td width="%d">', $value['width']);
          foreach ($value as $eth => $option)
               if (strcmp($eth, "width") != 0) echo GenerateListOption('ethnicity[]', $eth, $option, false);
          echo '</td>';

 

With

          foreach ($value as $eth => $option) {
               echo sprintf('<td width="%d">', $value['width']);
               if (strcmp($eth, "width") != 0) GenerateListOption('ethnicity[]', $eth, $option, false);
               echo '</td>';
          }

I'd like it displayed like it is below. So it's like..

<tr><td> blah </td><td> blah </td><td> blah </td></tr>

<table id="ethnicity">
<tr>
<td>
    <input type="checkbox" name="asian" rel="ethnicity"/><label>Asian</label>
    </td><td>
    <input type="checkbox" name="east indian" rel="ethnicity"/><label>East Indian</label>
    </td><td>
    <input type="checkbox" name="native american" rel="ethnicity"/><label>Native American</label>
    </td>
</tr>

<tr>
<td>
    <input type="checkbox" name="black/african" rel="ethnicity"/><label>Black/African</label>
    </td><td>
    <input type="checkbox" name="hispanic/latino" rel="ethnicity"/><label>Hispanic/Latino</label>
    </td><td>
<input type="checkbox" name="pacific islander" rel="ethnicity"/><label>Pacific Islander</label>
    </td>
</tr>

<tr>
<td>
    <input type="checkbox" name="caucasian/white" rel="ethnicity"/><label>Caucasian/White</label>
    </td><td>
<input type="checkbox" name="middle eastern" rel="ethnicity"/><label>Middle Eastern</label>
    </td><td>
    <input type="checkbox" name="other" rel="ethnicity"/><label>Other</label>
    </td>
</tr>
</table>

<?php
function GenerateColumnCheckboxTable ($num_cols, $values, $table_id, $input_attrs) {
$contents = sprintf('<table id="%s"><tr>', $table_id);
$count = 1;

$input_params = '';
foreach ($input_attrs as $key => $val) $input_params .= sprintf('%s=%s', $key, $val);

foreach ($values as $key => $value) {
	if ($count == $num_cols) {
		$contents .= '</tr><tr>';
		$count = 1;
	}

	$contents .= sprintf('<td><input type="checkbox" name="%s" %s /><label>%s</label></td>', $key, $input_params, $value);
	$count++;
}

for (; $count <= $num_cols; $count++) $contents .= '<td> </td>';
$contents .= '</tr></table>';
return $contents;
}

$ethnics = array(
'asian'	=> 'Asian',
'east indian'	=> 'East Indian',
'native american'	=> 'Native American',
'black/african'	=> 'Black/African',
.
.
.
);

echo GenerateColumnCheckboxTable (3, $ethnics, 'ethnicity', array('rel' => 'ethnicity'));

Thanks I'll give it a go. Another thing I thought of..

You know the echo's, when it displays the checkboxes. Couldn't we put that into an array.

eg:

$array[] = 'checkbox here';

Then I'd put out my table.. and put $echo array[0] where I want the to be displayed. I know still it's not the best design.. although it'd work out properly wouldn't it? (Even with access to the database. For example.. accessing it via deanlearners method)

That works thanks and also.. my method of putting it in the array works too! It's incredibly very messy. However.. the job works!

            $fields = array(0 => 'asian', 1 => 'black/african', 2 => 'caucasian/white', 3 => 'east indian', 4 => 'hispanic/latino', 
						5 => 'middle eastern', 6 => 'native american', 7 => 'pacific islander', 8 => 'other');
		$proin = explode(', ', $profile->ethnicity);
		$array = array();
		foreach($fields as $num => $name){
			if(in_array($name,$proin)){$checked = 'checked';}else{$checked = '';}
			$array[] = '<input type="checkbox" name="ethnicity[]" value="'.$name.'" '.$checked.' /> '.ucwords($name); 
		}                            
    	echo '<tr><td height="34">Ethnicity:</td><td>
	<table>
        <tr>
		<td width="137">' . $array[0] . '<br />' . $array[1] . '<br />' . $array[2] . '</td>
        	<td width="124">' . $array[3] . '<br />' . $array[4] . '<br />' . $array[5] . '</td>
        	<td width="128">' . $array[6] . ' <br />' . $array[7] . '<br />' . $array[8] . '</td>
	</tr>
        </table>
	</td></tr>

I am jumping with joy. The simplest solution huh. :)

Yup, until you start adding more entries in that array. ;)

See your point. Though I don't think I'll be having 100 ethnicities. That'll be too much. I'm sweet with what I have.

Thanks everyone :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.