My code is like this below

HTML Code :

<select multiple="multiple" size="5" name="religion[]" id="religion" class="txtareamultifield" ondblclick="moveOptions(document.MatriForm.religion,document.MatriForm.RIGHTRELIGION,1);fnAnyChk(document.MatriForm.religion,document.MatriForm.RIGHTRELIGION);fnGothraChk();fnModifySubCasteChk(this.value);otherreligion();" style="width:355px; height:90px; font-size:13px;">

                                            <option value="" selected>Any</option>
              <option value="Hindu">Hindu</option>
                        <option value="Muslim-Shia">Muslim - Shia</option>
                        <option value="Muslim-Sunni">Muslim - Sunni</option>
                        <option value="Muslim-Others">Muslim - Others</option>
                        <option value="Christian-Catholic">Christian - Catholic</option>
                        <option value="Christian-Orthodox">Christian - Orthodox</option>
                        <option value="Christian-Protestant">Christian - Protestant</option>
                        <option value="Christian-Others
                        ">Christian - Others</option>
                        <option value="Sikh">Sikh</option>

PHP Code : 


$religion=$_POST['religion'];

 if(isset($religion)){

    //$rel=explode(':',$_POST["religion_".$i]);
    //$rel=implode(',',$religion);
    $serializedoptions1 = serialize($religion);
    $serializedoptions123 = unserialize($serializedoptions1);
    //print_r($serializedoptions123); 
    foreach($religion as $key=>$val){
    echo $val;
    }

}

$qry1="insert into preference(id,email,toage,fromage,maritalstatus,toheight,fromheight,phc,religion,star,caste, mangalik,mothertongue,qualification,occupation,income,country,state,city,citizenship,food, smoking,drinking,partner_desc)
VALUES('','" . $_SESSION['user'] . "','$toage','$fromage','$maritalstatus','$toheight',     '$fromheight','$phc','".implode(',',$serializedoptions123)."','$serializedoptions2','$serializedoptions','$mangalik','$serializedoptions3',          '$serializedoptions8','$serializedoptions9','$income','$serializedoptions4','$serializedoptions5','$serializedoptions6','$serializedoptions7','$food','$smoking','$drinking','$description')";

mysql_query($qry1) or die(mysql_error());
Member Avatar for diafol

Why would anybody have more than one religion?

It can't be stored or what's the problem ? Echo your $qry1 and copy / paste into your MySQL via CLI or PhpMyAdmin and see what happen.

Member Avatar for diafol

It can be done easily if you perform bitwise operations. If you provide a religions table... and there is a guarantee that this table will never have more than 32 records (for 32-bit systems).

id label bitval
1 Hindu 1
2 Muslim - Shia 2
3 Muslim - Sunni 4
4 Muslim - Others 8
5 Christian-Catholic 16
6 Christian-Orthodox 32
7 Christian-Protestant 64
8 Christian-Others 128
9 Sikh 256

You can provide the dropdown thus...(from php/mysql)

<option value="0" selected>None</option>
<option value="1">Hindu</option>
<option value="2">Muslim - Shia</option>
<option value="4">Muslim - Sunni</option>
<option value="8">Muslim - Others</option>
<option value="16">Christian - Catholic</option>
<option value="32">Christian - Orthodox</option>
<option value="64">Christian - Protestant</option>
<option value="128">Christian - Others</option>
<option value="256">Sikh</option>

However, multiselect listboxes are quite difficult, especially for small screen/mobile devices and a series of checkboxes may be easier. The format would be similar:

<label><input type="checkbox" name="religion[]" value="1" />  Hindu</label>
<label><input type="checkbox" name="religion[]" value="2" /> Muslim - Shia</label>
<label><input type="checkbox" name="religion[]" value="4" /> Muslim - Sunni</label>
<label><input type="checkbox" name="religion[]" value="8" /> Muslim - Others</label>
<label><input type="checkbox" name="religion[]" value="16" /> Christian - Catholic</label>
<label><input type="checkbox" name="religion[]" value="32" /> Christian - Orthodox</label>
<label><input type="checkbox" name="religion[]" value="64" /> Christian - Protestant</label>
<label><input type="checkbox" name="religion[]" value="128" /> Christian - Others</label>
<label><input type="checkbox" name="religion[]" value="256" /> Sikh</label>

SO when you submit you can just do this...

$religion = (isset($_POST['religion'])) ? array_sum($_POST['religion']) : 0;

That will give you an integer, for example...

A value of 25 = 16 + 8 + 1 = Catholic + Muslim other + Hindu

Your religion field for the user in the users table would be an integer field and you'd store this value.

In order to display all religions for the specific user...

SELECT * FROM religions WHERE bitval & $religion

An example of this is shown here: http://demos.diafol.org/bitwise-checkboxes.php where it shows 'interests' instead of 'religions', but its the same method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.