work_it_work Posted September 20, 2009 Share Posted September 20, 2009 i'm stuck with this array here is the case: i have php form in steps, at some step i have a checkbox table with 40 checkboxes generated by a loop. each checkbox has its value, which is a number and they work perfect if the user check some boxes and go to next step, the values are stored in php, then at the finish step they are stored together in db. this also works. if the user check some boxes, go to next step and then returns to the step with checkboxes the checkboxes checked before are not checked what i want to do is to have them checked in this situation here i store the checkboxes values if they are checked $o = implode(",",$_POST['opt']); if ($o[0] == ",") $o= substr($o, 1); here are the values stored manually for testing $ccc = array(189, 184, 193); here is the echo row echo "<td width=25%><input type=checkbox name=opt[] value=".$or['object_id'].""?><? if(in_array($or['object_id'], $ccc)) { echo ' checked';}?><?echo " /> ".$or['value']."</td>\n"; how do i place the checkboxes value from $o to $ccc array ? please help i'm stuck! Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 someone?! Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 tried this solution but don't work $ccc = array($o); if i echo $ccc[0] i get all the values : 1,2,3,4,5,6 if i echo $ccc[1] or any other $ccc[999] i get nothing... Please help!!!! Quote Link to comment Share on other sites More sharing options...
knsito Posted September 20, 2009 Share Posted September 20, 2009 I think you want this function array_in_array($needles, $haystack) { foreach ($needles as $needle) { if ( in_array($needle, $haystack) ) { return true; } } return false; } array_in_array($ccc,$or['object_id']); http://us.php.net/manual/en/function.in-array.php Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 array_in_array($ccc,$or['object_id']); that won't work ever! first comes the value then searches for it! Quote Link to comment Share on other sites More sharing options...
knsito Posted September 20, 2009 Share Posted September 20, 2009 that won't work ever! first comes the value then searches for it! Edited my post above. From php.net bool in_array ( mixed $needle , array $haystack [, bool $strict ] ) The search item comes first ($needle) and then the array you are looking through ($haystack) example straight from the manual $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } Output: "Got Irix" Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 the manual works perfect but the my case it's not the manual as far as i can see... the values that need to be in an array are passed by file like this $_POST['opt'] then i implode them $o = implode(",",$_POST['opt']); if ($o[0] == ",") $o= substr($o, 1); and i have something like 1,2,3,4,5,6 etc if i create and array like this $ccc = array($o); WON't WORK! then the echo row which outputs checkboxes must check to see if the values from checkboxes exsist in the array and if they exists it check the checkboxes that has the values = with values from array Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 any idea how to pass the content of $o = implode(",",$_POST['opt']); if ($o[0] == ",") $o= substr($o, 1); to an array ??!? Quote Link to comment Share on other sites More sharing options...
knsito Posted September 20, 2009 Share Posted September 20, 2009 OK sorry I guess Im reading your question wrong. $_POST['opt'] is an array right? Why are you imploding() and then trying to re-create the array? I am still alittle confused but you want something like this?: <?php $checkboxes = array('1','2','11','22','111','222'); //your checkboxes $ccc = array('222','11','2'); //values you want checked foreach($checkboxes as $key => $val){ ?> <input type=checkbox name=opt[] value="<?php echo $val ;?>" <?php //if this is checked if(in_array($val, $ccc)){ echo ' checked'; } ?> /> <?php echo $val."<br />"; //end checkbox } //end for each ?> Quote Link to comment Share on other sites More sharing options...
knsito Posted September 20, 2009 Share Posted September 20, 2009 use explode() which is basically the opposite of what implode() does any idea how to pass the content of $o = implode(",",$_POST['opt']); if ($o[0] == ",") $o= substr($o, 1); to an array ??!? Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 it's a step form with 6 pages, at page 2 the user check some checkboxes, then he move to next page or pages, but ig he wants to go back to page 2, the checkboxes checked before must be checked... so use it's not forced to check them again... i'm imploding the checked checkboxes values to substract the first character which is always a comma like this ",189,193,198,200" and i need it to be like this "189,193,198,200" so i solved the problem bi imploding and substract the first character Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 " use explode() which is basically the opposite of what implode() does " it doesn't work.... tried already Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 no one can help?! Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 20, 2009 Share Posted September 20, 2009 you need JavaScript mate sorry. unless you can use a session to hold the current info then do a manual cheek... Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 20, 2009 Author Share Posted September 20, 2009 impossible, there must be a solution to pass the values from $_POST['opt'] to an array and the array to recognize them individual array[0] array[1] .... etc that's all i need ... Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 21, 2009 Author Share Posted September 21, 2009 solved, it was a simple foreach function Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 21, 2009 Share Posted September 21, 2009 Can you show others then, and show us examples what you learnt so we can learn as well please. cheers. Quote Link to comment Share on other sites More sharing options...
work_it_work Posted September 21, 2009 Author Share Posted September 21, 2009 yeah $o = implode(",", $_POST['opt']); if ($o[0] == ",") $o= substr($o, 1); $rr = array($o); foreach ($rr as $v){ $r = explode(",",$v); and for the checkboxes <td width=25%><input type=checkbox name=opt[] value=".$or['object_id'].""?><? if(in_array($or['object_id'], $r)) { echo ' checked';}?><?echo " /> ".$or['value']."</td>\n" Quote Link to comment 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.