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: [Dave] looping drop-down list

By eduran582 - January 26, 2009 - edited: January 26, 2009

Hi Dave,

Your code works great!

One thing though and I'm not sure if it makes any difference but if you look at the source when the form is generated on the web page, it shows the loop going through the whole 99 cycles displaying an "Notice: Undefined index: ..." error in the code after the last actual date (list). As this is not displayed on the form I'm not so sure it matters as there would certainly never be "99" dates and I would replace the 99 with the actual maximum number but is there a way to check for an empty variable between the <option><?php echo $registrationRecord["list$num"] ?><br/><?option> and the <?php endforeach ?> lines and if the variable does not exist or is blank to exit the loop?

ADDED (in case you already read the above): Disregard the error...it was because there were no additional fields to check (sorry). However, if it's possible to check for an empty field to stop the looping, that would/may save on some server resources.

Thanks again for the great support!

Eric

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