Jump to content

Trying to use a loop to clean up some long code


Jalz

Recommended Posts

Hi Guys,

 

Currently I have some code which I wrote long hand which works, however I would like to code it more elgantly using a loop. The only difference between the lines underneath is the variable name, each variable as a different number attached to it.

 

if($order == 0) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs0));

if($order == 1) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs1));

if($order == 2) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs2));

if($order == 3) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs3));

if($order == 4) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs4));

if($order == 5) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs5));

if($order == 6) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs6));

if($order == 7) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs7));

if($order == 8) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs8));

if($order == 9) $event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs9));

 

 

So the loop that I managed to come up with is as follows as I wont need the if statement from my original code:

//recid is a array variable I've set up

 

foreach($recid as $order => $recid1) {

 

$dietReqs = '$dietReqs'.$order;

$event_edit->setField('DietaryRequirements',fmsCheckboxCombine($dietReqs));

 

}

 

However this doesn't seem to work. It doesn't show up an error message, but nether does it write the contents of variable $dietReqs0..9 in my database field where as my long hand does.I've tried putting echo just before the variable name, that does produce an error. Any ideas what I could be doing wrong? I could keep the long code as it works, but would love to simplify it ideally.

 

Many Thanks to all that help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.