Jump to content

Recommended Posts

I have a field with multiple check values. In the database.. if all 4 are checked, it displays similar to..

value1, value2, value3, value4

If two is checked.. its

value1, value2

or whatever. You get the drift.

 

We use an explode, to seperate it all. Now because the code isn't a mind reader.. the explode, will give up a maximum of 4 values in the array. The code is below.

$proin = explode(', ', $profile->interested_in); echo '
<input type="checkbox" name="intin[]" value="men"'; 
if($proin[0] == 'men') { echo 'checked'; } echo '/> Men
<input type="checkbox" name="intin[]" value="women"'; 
if($proin[1] == 'women') { echo 'checked'; } echo ' /> Women
<input type="checkbox" name="intin[]" value="none"'; 
if($proin[2] == 'none') { echo 'checked'; } echo ' /> None
<input type="checkbox" name="intin[]" value="other"'; 
if($proin[3] == 'other') { echo 'checked'; } echo ' /> Other

Now, whats the problem? The problem is.. if a person only checked in "none" The checkbox wont display checked. As it only checks as if there are 4 values in the database.

 

How can we solve this? Well.. i know if this way, here is one.

echo '<input type="checkbox" name="intin[]" value="men"'; 
if($proin[0] == 'men' || $proin[1] == 'men' || $proin[2] == 'men' || $proin[3] == 'men') { echo 'checked'; } echo '/> Men';

This is alright, if there is maybe only 2 or 3 fields. However.. in my case there is 4, and 4 rows of that code, wil llook very messy. I also have another lot of checkboxes. There is 9 of them. This will end up very messy.

 

I was wondering, is there an easier way to do this? Cheers!

 

Im not entirely sure if im understanding but... would the following work?

 

$fields = (0 => 'men', 1=> 'women', 2=> 'none', 3=> 'other');
$proin = explode(', ', $profile->interested_in); 
foreach($fields as $num => $name){
	if(in_array($name,$proin)){$checked = 'checked';}else{$checked = '';}
	echo '<input type="checkbox" name="intin[]" value="'.$name.'" '.$checked.' /> '.ucfirst($name); 
}

Good lord. There was a few syntax errors. $fields should of been an array. It works though!

I imagine you may not of be able to come up with something similar for this too could you?

 

echo '<option value="" '; if($profile->orien_status == '') { echo 'selected'; } echo '>Select Orientation:</option>
<option '; if($profile->orien_status == 'straight') { echo 'selected'; } echo '>Straight</option>
<option '; if($profile->orien_status == 'bisexual') { echo 'selected'; } echo '>Bisexual</option>
<option '; if($profile->orien_status == 'lesbian/gay') { echo 'selected'; } echo '>Lesbian/Gay</option>
<option '; if($profile->orien_status == 'asexual') { echo 'selected'; } echo '>Asexual</option>
<option '; if($profile->orien_status == 'pansexual') { echo 'selected'; } echo '>Pansexual</option>

Only one is aloud.

Cheers

Woops yeah, sorry i quickly rushed it together!

 

You can do what you're after using the same logic I used in the previous example, but change the field array to straight, bisexual, etc, etc

 

and change the echo row as required as well

Because it only has 1 value in the database. It can't be an array.. So I came up with this.. which doesn't work.

$fields = array(0 => 'straight', 1 => 'bisexual', 2 => 'lesbian/gay', 3 => 'asexual', 4 => 'pansexual');
foreach($fields as $num => $name){
if(in_array($name,$profile->orien_status)){$selected = 'selected';}else{$selected = '';}
echo '<option value="'.$name.'" '.$selected.' /> '.ucfirst($name).' </option>'; 
}

All the fields come up, but none of them are selected. I'm assuming it's because $profile->orien_status isn't an array.

How can I fix it?

Almost

$fields = array(0 => 'straight', 1 => 'bisexual', 2 => 'lesbian/gay', 3 => 'asexual', 4 => 'pansexual');
foreach($fields as $num => $name){
if(in_array($name,$profile->orien_status)){$selected = 'selected';}else{$selected = '';}
echo '<option value="'.$name.'" '.$selected.' /> '.ucfirst($name).' </option>'; 
}

should be

$fields = array(0 => 'straight', 1 => 'bisexual', 2 => 'lesbian/gay', 3 => 'asexual', 4 => 'pansexual');
foreach($fields as $num => $name){
if($name==$profile->orien_status){$selected = 'selected';}else{$selected = '';}
echo '<option value="'.$name.'" '.$selected.' /> '.ucfirst($name).' </option>'; 
}

 

Probably a bit more wordy, but cleaner.

 

function GenerateListOption ($value, $text, $IsSelected) {
     $selected = $IsSelected ? 'selected="selected"' : '';
     return sprintf('<option value="%s" %s>%s</option>', $value, $selected, $text);
}

$oriens = array('' => 'Sexual Orientation',
                         'straight' => 'Straight',
                          ...
);
foreach ($oriens as $key => $value) {
     echo GenerateListOption($key, $value, $profile->orien_status === $key);
}

Ahh. Yeah. I didn't think the foreach was a look.  Though I did think of that, but because I didn't think it was a loop (Blonde moment) i thought it wouldn't of worked.

 

Ken2k7, that also works. It's cleaner too. Both brilliant respones! Good thing with yours Ken2k7, you can use it in other places. :) Good job!

 

Thanks!

I've tried doing the same for the checkboxes too. None of them come up as checked.

function CheckboxMenuDisplay($value, $text, $IsChecked) {
     $checked = $IsChecked ? 'checked' : '';
     return sprintf('<input type="checkbox" name="intin[]" value="%s" %s />%s', $value, $checked, $text);
}

$proin = explode(', ', $profile->interested_in); 
$intins = array('men' => 'Men', 'women' => 'Women', 'none' => 'None', 'other' => 'Other');
foreach ($intins as $key => $value) {
     	echo CheckboxMenuDisplay($key, $value, $proin === $key);
}

Whats wrong

Logically, wouldn't it make more sense to have these options as a set of radio buttons?  I mean, this is for some sort of social networking site, correct?  Someone can be interested in men, women, both, or none.  With a set of radio buttons, you wouldn't have to do as much work to manipulate the data as you wouldn't have those "The user clicked both 'none' and 'men'" cases.

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?

deanlearner, heres the one with 9 options. It uses <br/> and <td> 's etc. I can't figure it out in a method like you chose.

echo '<tr><td width="137">
<input type="checkbox" name="ethnicity[]" value="asian" /> Asian
<br />
<input type="checkbox" name="ethnicity[]" value="black/african" /> Black/African
<br />
<input type="checkbox" name="ethnicity[]" value="causcasian/white" /> Caucasian/White
</td>
<td width="124">
<input type="checkbox" name="ethnicity[]" value="east indian" /> East Indian
<br />
<input type="checkbox" name="ethnicity[]" value="hispanic/latino" /> Hispanic/Latino
<br />
<input type="checkbox" name="ethnicity[]" value="middle eastern" /> Middle Eastern
</td>
<td width="128">
<input type="checkbox" name="ethnicity[]" value="native american" /> Native American
<br />
<input type="checkbox" name="ethnicity[]" value="pacific islander" /> Pacific Islander
<br />
<input type="checkbox" name="ethnicity[]" value="other" /> Other
</td></tr>';

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 ($oriens 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>';

Thanks for the reply.

$oriens isn't defined in that example. Should it be $eths?

foreach ($oriens 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($key, $value, $profile->orien_status === $key);
          echo '</td>';
     }
}

GenerateListOption

Should of been..

GenerateInputOption

 

It also doesn't access the database in any way at all. There for nothing will be checked upon load.

$profile->ethnicity holds the values of the ethnicities. These are similar to..

value1, value2, value3

So you'd do an implode to seperate them. Though.. I'm not sure how that'd go in? Also, after every checkbox, should be a <br /> except the last one.

eg:

<checkbox> <br /> <checkbox> <br /> <checkbox>

It becomes unaligned otherwise. Although, instead of using <br/>.. would there be a way to do 2 extra tr's and manage to fit it in?

It's probably not the best set-up.

 

1. You shouldn't use CSV in the database. Makes searching very difficult.

2. Table is bad unless you're display some statistics. There are better ways to display something other than TD + breaks.

So if I'm not using CSV. How can I do it. Would I have like.. ethnicity1, ethnicity2.. I'm confused.

Then when I display them.. it'll just implode it for the display.

Yet they'll be like 9 collums for ethnicities. It's rather stupid.

 

This is frustrating me. Can people have more than 1 ethnicity, in real life, If they can't.. can I have checkboxes acting like radiobuttons? Would this make the search easier as there is only 1 value in the database..

 

Really annoyed ¬,¬

Well, someone can be 50% Asian and 50% Hispanic. The decision is up to you.

 

I will explain the CSV issue though. In your database, you can have this:

 

Table ethnicity

---------------------------

eth_id  |  ethnicity

---------------------------

  1      |    Asian

  2      |    Hispanic

              .

              .

              .

Table user_eth

-----------------------------------

id  |  user_id  |  ethnicity 

-----------------------------------

1  |      4      |      1

2  |      4      |      2

              .

              .

              .

I understand, and table Ethnicity, holds all the ethnicities. To insert into user_eth, I could do a foreach.

So foreach ethnicities checked.. insert a row into database based in this ethnicity.

 

Lets say I did that, how would I then populate the checkboxes.

 

Another site, uses similar methods. Although like I said before, using td's instead. There checkboxes are displayed like below.

<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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.