2

I'm posting the below arrays, where each checkbox/checkbox tag are linked with a key (i.e. each category check box is linked with a "worldwide" checkbox):

<input type="checkbox" name="catpref[1]" value=10 > Politics
<input type="checkbox" name="worldwide[1]" value=1 > Worldwide

<input type="checkbox" name="catpref[2]" value=20 > Sports
<input type="checkbox" name="worldwide[2]" value=1 > Worldwide

I received them in the controller and trying to store the values.

 $catprefs = $request['catpref'];
 $worldwide = $request['worldwide'];

foreach($catprefs as $key => $value and $worldwide as $key => $value2){

      $save_catp = new Cpref();
      $save_catp->user_id = $user_id;
      $save_catp->qatype = $value;
      $save_catp->wwide= $value2;
      $save_catp->type = 1;
      $save_catp->save();

What would be the syntax for the above foreach loop ?

P.S : Please note that the arrays linked together via key could be different in size because a category could be selected while "worldwide" could be left unselected (i.e. array_combine() requires equal size arrays).

0

3 Answers 3

1

you can check if the worldwide key is set then add it's value other wise set to default value.

foreach($catprefs as $key => $value){

  $save_catp = new Cpref();
  $save_catp->user_id = $user_id;
  $save_catp->qatype = $value;
  $save_catp->wwide= (array_key_exists($key, $worldwide) ? $worldwide[$key] : 'default value');
  $save_catp->type = 1;
  $save_catp->save();
}
Sign up to request clarification or add additional context in comments.

Comments

0

One of possible solutions could be "chunking" those inputs. You can use multidimensional arrays for form inputs.

Try to rename them like this:

<input type="checkbox" name="set[1][catpref]" value=10> Politics
<input type="checkbox" name="set[1][worldwide]" value=1> Worldwide

<input type="checkbox" name="set[2][catpref]" value=20> Sports
<input type="checkbox" name="set[2][worldwide]" value=1> Worldwide

Then your foreach loop will look something like this:

foreach ($request['set'] as ['catpref' => $catpref, 'worldwide' => $worldwide]) {
    var_dump($catpref, $worldwide);
}

2 Comments

I received error : Invalid argument supplied for foreach()
Don't blindly copy-paste this code. Did you change checkboxes name attributes? What's inside $request variable? Please show me var_dump($request) just before loop.
0

Try the below code.

$catprefs = $request['catpref'];
$worldwide = $request['worldwide'];

foreach($catprefs as $key => $value){
   $save_catp = new Cpref();
   $save_catp->user_id = $user_id;
   $save_catp->qatype = $value;
   $save_catp->wwide= isset($worldwide[$key]) ? $worldwide[$key] : 1;
   $save_catp->type = 1;
   $save_catp->save();
}

3 Comments

I received error :Undefined offset: 3 at this line : $save_catp->wwide= $worldwide[$key];
I tried and it didn't give an error, but now all categories are having the wwide set to 1 even the ones that have the worldwide checkbox not selected.
$save_catp->wwide= isset($worldwide[$key]) ? $worldwide[$key] : 1; Change '1' with your default value and it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.