gathos Posted January 28, 2009 Share Posted January 28, 2009 ok so what i want to do is have a page with 30 checkboxes on it, and save those to a database, however i don't want to have 30 different variables on the database. what i've done so far is to take teh checkboxes and add tehm to an array, and then turn it into one big string, then save the string. however if i go back to the page where the check boxes are, the checkboxes are empty (obviously). so then i split the strring back into an array when i load the page. i've tried something like this, foreach ($words as &$value) { if ($value == "square")){echo '<input name="keywords[]" type="checkbox" id="square" value="square" checked="checked"/> Square'; $square=1;} elseif ($value == "square"){echo '<input name="keywords[]" type="checkbox" id="square" value="square" /> Square'; $square=1;} }; but the output is outs out a checkbox on EVERY variable so instead of just 30 checkboxes i end up with 30 * however many words are in the string. so my question is, can you guys give me some suggestions on how to fill in the checkboxes when i open the page? I really hope i made sense, it's was hard for me to describe. thanks in advance. phil Quote Link to comment https://forums.phpfreaks.com/topic/142813-trying-to-fill-checkboxes-from-database/ Share on other sites More sharing options...
severndigital Posted January 28, 2009 Share Posted January 28, 2009 I'm not 100% sure, but it looks like it might be a syntax problem .. your if statment says if($value == 'square'){ do this }elseif($value == 'square'){ do this instead } make sense? if you have an array of which ones are checked shouldn't it look like something like this? foreach($words as $value){ if($value == checkedSignal) //don't know what you are using to tell weahter they are checked or not. echo '<input name="keywords[]" type="checkbox" id="square" value="square" checked="checked"/> Square'; }else{ echo '<input name="keywords[]" type="checkbox" id="square" value="square" />Square'; } } hope that helps .. Quote Link to comment https://forums.phpfreaks.com/topic/142813-trying-to-fill-checkboxes-from-database/#findComment-748612 Share on other sites More sharing options...
gathos Posted February 3, 2009 Author Share Posted February 3, 2009 alright i mad e it work but ... not well... my array looks like this: Array ( [1] => rectangle [2] => cabinet [3] => geometric [4] => bevel [5] => border ) > my code looks like this: <? //turn the string into a array $s = $keywords; $words = split("[ ]+", $s); ?> <? // remove the first empty entry in the array. $i=0; foreach ($words as &$transfer){ if ($i >= 1){$keytra[$i] = $transfer;}; $i++; }; ?> <? //set checkers to 0 $square = 0; $rectangle = 0; $circle = 0; $curvetop = 0; $octagon = 0; $oval = 0; $rake = 0; //check array, see if there is a value that equals any of the checkboxes of this set. foreach ($keytra as &$value) { if (($value == "square")&&($square !=1)){echo '<input name="keywords[]" type="checkbox" id="square" value="square" checked="checked"/> Square <br />'; $square=1;} elseif (($value != "square")&&($square !=1)){echo '<input name="keywords[]" type="checkbox" id="square" value="square" /> Square <br />'; $square=1;} }; foreach ($keytra as &$value) { if (($value == "rectangle")&&($rectangle !=1)){echo '<input name="keywords[]" type="checkbox" id="rectangle" value="rectangle" checked="checked"/> rectangle <br />'; $rectangle=1;} elseif (($value != "rectangle")&&($rectangle !=1)){echo '<input name="keywords[]" type="checkbox" id="rectangle" value="rectangle" /> rectangle <br />'; $rectangle=1;} }; foreach ($keytra as &$value) { if (($value == "circle")&&($circle !=1)){echo '<input name="keywords[]" type="checkbox" id="circle" value="circle" checked="checked"/> circle <br />'; $circle=1;} elseif (($value != "circle")&&($circle !=1)){echo '<input name="keywords[]" type="checkbox" id="circle" value="circle" /> circle <br />'; $circle=1;} }; foreach ($keytra as &$value) { if (($value == "curvetop")&&($curvetop !=1)){echo '<input name="keywords[]" type="checkbox" id="curvetop" value="curvetop" checked="checked"/> curvetop <br />'; $curvetop=1;} elseif (($value != "curvetop")&&($curvetop !=1)){echo '<input name="keywords[]" type="checkbox" id="curvetop" value="curvetop" /> curvetop <br />'; $curvetop=1;} }; foreach ($keytra as &$value) { if (($value == "octagon")&&($octagon !=1)){echo '<input name="keywords[]" type="checkbox" id="octagon" value="octagon" checked="checked"/> octagon <br />'; $octagon=1;} elseif (($value != "octagon")&&($octagon !=1)){echo '<input name="keywords[]" type="checkbox" id="octagon" value="octagon" /> octagon <br />'; $octagon=1;} }; foreach ($keytra as &$value) { if (($value == "oval")&&($oval !=1)){echo '<input name="keywords[]" type="checkbox" id="oval" value="oval" checked="checked"/> oval <br />'; $oval=1;} elseif (($value != "oval")&&($oval !=1)){echo '<input name="keywords[]" type="checkbox" id="oval" value="oval" /> oval <br />'; $oval=1;} }; ?> it works for this section of checkboxes however in the next set it no longer fills out the check boxes that should be listed. this is the code for the next section <? ///now check the next set. $cabinet = 0; $door = 0; $panel = 0; $transom = 0; $sidelite= 0; $ceiling= 0; $skylight= 0; $divider= 0; $sign= 0; foreach ($keytra as &$value) { if (($value == "cabinet")&&($cabinet !=1)){echo '<input name="keywords[]" type="checkbox" id="cabinet" value="cabinet" checked="checked"/> cabinet <br />'; $cabinet=1;} elseif (($value != "cabinet")&&($cabinet !=1)){echo '<input name="keywords[]" type="checkbox" id="cabinet" value="cabinet" /> cabinet <br />'; $cabinet=1;} }; foreach ($keytra as &$value) { if (($value == "door")&&($door !=1)){echo '<input name="keywords[]" type="checkbox" id="door" value="door" checked="checked"/> door <br />'; $door=1;} elseif (($value != "door")&&($door !=1)){echo '<input name="keywords[]" type="checkbox" id="door" value="door" /> door <br />'; $door=1;} }; foreach ($keytra as &$value) { if (($value == "panel")&&($panel !=1)){echo '<input name="keywords[]" type="checkbox" id="panel" value="panel" checked="checked"/> panel <br />'; $panel=1;} elseif (($value != "panel")&&($panel !=1)){echo '<input name="keywords[]" type="checkbox" id="panel" value="panel" /> panel <br />'; $panel=1;} }; foreach ($keytra as &$value) { if (($value == "transom")&&($transom !=1)){echo '<input name="keywords[]" type="checkbox" id="transom" value="transom" checked="checked"/> transom <br />'; $transom=1;} elseif (($value != "transom")&&($transom !=1)){echo '<input name="keywords[]" type="checkbox" id="transom" value="transom" /> transom <br />'; $transom=1;} }; foreach ($keytra as &$value) { if (($value == "sidelite")&&($sidelite !=1)){echo '<input name="keywords[]" type="checkbox" id="sidelite" value="sidelite" checked="checked"/> side lite <br />'; $sidelite=1;} elseif (($value != "sidelite")&&($sidelite !=1)){echo '<input name="keywords[]" type="checkbox" id="sidelite" value="sidelite" /> side lite <br />'; $sidelite=1;} }; foreach ($keytra as &$value) { if (($value == "ceiling")&&($ceiling !=1)){echo '<input name="keywords[]" type="checkbox" id="ceiling" value="ceiling" checked="checked"/> ceiling <br />'; $ceiling=1;} elseif (($value != "ceiling")&&($ceiling !=1)){echo '<input name="keywords[]" type="checkbox" id="ceiling" value="ceiling" /> ceiling <br />'; $ceiling=1;} }; foreach ($keytra as &$value) { if (($value == "skylight")&&($skylight !=1)){echo '<input name="keywords[]" type="checkbox" id="skylight" value="skylight" checked="checked"/> skylight <br />'; $skylight=1;} elseif (($value != "skylight")&&($skylight !=1)){echo '<input name="keywords[]" type="checkbox" id="skylight" value="skylight" /> skylight <br />'; $skylight=1;} }; foreach ($keytra as &$value) { if (($value == "divider")&&($divider !=1)){echo '<input name="keywords[]" type="checkbox" id="divider" value="divider" checked="checked"/> divider <br />'; $divider=1;} elseif (($value != "divider")&&($divider !=1)){echo '<input name="keywords[]" type="checkbox" id="divider" value="divider" /> divider <br />'; $divider=1;} }; foreach ($keytra as &$value) { if (($value == "sign")&&($sign !=1)){echo '<input name="keywords[]" type="checkbox" id="sign" value="sign" checked="checked"/> sign <br />'; $sign=1;} elseif (($value != "sign")&&($sign !=1)){echo '<input name="keywords[]" type="checkbox" id="sign" value="sign" /> sign <br />'; $sign=1;} }; ?> There should be a checkbox in the cabinet checkbox, but there isn't. i hope someone can see the error i made because i can't find it. thanks again, phil. Quote Link to comment https://forums.phpfreaks.com/topic/142813-trying-to-fill-checkboxes-from-database/#findComment-753950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.