Jump to content

[SOLVED] please help with aray


work_it_work

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/174893-solved-please-help-with-aray/
Share on other sites

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

 

 

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"

 

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

 

 

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

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

 

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"

 

 

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.