Jump to content

[SOLVED] using explode() to fill in checkboxes


juselliott

Recommended Posts

Hi

 

I have a field stored in a table that contains regions in the UK separated by commas. There could be any number of pieces held in this field from 1-12, based upon the users specified operating region (i.e. they could operate in multiple regions).

 

I have then created a form with 12 check boxes, 1 for each possible region they could have specified. I need to automatically tick the relevant box for each region they have specified (i.e. each piece of data held in the field).

 

Is this doable?

 

I have tried experimenting with explode() but cannot figure out to this information to specific whether a check box should be ticked by default.

 

Thanks

 

 

yes this is doable and for checked checkbox you simple needs to write something extra as follow

 

if(userregion == fromdbregion)

{

  <INPUT TYPE=CHECKBOX NAME="region"  checked >regionname<BR>

}

else

{

  <INPUT TYPE=CHECKBOX NAME="region">regionname<BR>

}

 

modify this as per you requirement.

 

Regards

<?php
$allregion = array('r1','r2','r3',....,'r12'); //store ur region like this.

$region = 'r1,r2,r3'; //get the values from the db

$region_explode = explode(",",$region);//make it array using explode

for($i=0;$i<12;$i++)
{
    if(in_array($allregion[$i],$region_explode))
        echo "<input type='checkbox' name='region' value='".$allregion[$i]."' checked='checked'>";
    else
        echo "<input type='checkbox' name='region' value='".$allregion[$i]."'>";
}
?>

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.