looping drop-down list

8 posts by 2 authors in: Forums > CMS Builder
Last Post: January 30, 2009   (RSS)

Hi Dave,

I'm starting to get the hang of CMS Builder and as I'm not a php programmer, need your help. I have several classes (as in instruction) that have dates to select from in a registration drop-down form; some only have a few and others have several. In the section editor in the registration I created several "List" fields (list1, list2...listn) for the user to add for each class.

Is there a way to loop those in the form, that contains a drop-down list field, with each loop detecting if there is a date to list (non-empty field)? Something similar to:

for x = 1 to 99
<option><?php echo $registrationRecord["list'x'"] ?><br/></option>
<?php if (!$registrationRecord["list'x'"]): ?>
<?php endif ?>

The end result will be the loop will only list a date (list) if the listx field is not empty.

Keep up the great work and the latest build is EXCELLENT!

Regards,

Eric

Re: [eduran582] looping drop-down list

By Dave - January 26, 2009

Hi Eric,

Not sure if this is exactly what you need but it will get us started. Try this:

<?php foreach (range(1,99) as $num): ?>
<?php if (!$registrationRecord["list$num"]) { continue; } ?>
<option><?php echo $registrationRecord["list$num"] ?><br/></option>
<?php endforeach ?>


"continue" means "skip to the next item in the foreach loop".

Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [eduran582] looping drop-down list

By Dave - January 27, 2009

Hi Eric,

You can use @ in PHP to suppress warnings or errors. So we can add that to your code like this:

<?php foreach (range(1,99) as $num): ?>
<?php if (!@$registrationRecord["list$num"]) { continue; } ?>
<option><?php echo $registrationRecord["list$num"] ?><br/></option>
<?php endforeach ?>


Also, continue; skips the number and goes to the next item in the foreach loop but you can use break; to exit the foreach loop if you want to stop after the first undefined list. If you know you'll never have gaps in your list values this should be fine.

<?php if (!@$registrationRecord["list$num"]) { break; } ?>

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] looping drop-down list

Thanks for the quick reply, Dave!

I really appreciate your help in this and I appoligize for my ignorance (example: my lame description asking to check for an empty value AFTER it has already been displayed. [blush]) With your help, this addition should do the trick!

And again, thanks for the great support. I'll let you know how it turns out. [;)]

Eric

Re: [eduran582] looping drop-down list

By Dave - January 27, 2009

No problem at all, glad to help! :)
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] looping drop-down list

Dave,

Just a quick note to let you know that your help with the coding worked great! Now the user can add dates to each of the respective fields (list1, list2,...listx) and only those will show up in the drop-down field in the registration form for each respective class.

Well done! Keep up the great work and thanks for the GREAT support! [:)]

Eric

Re: [eduran582] looping drop-down list

By Dave - January 30, 2009

Great, glad to hear it's working! Thanks for posting back to let us know! :)
Dave Edis - Senior Developer
interactivetools.com