publicly populate a list field

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 23, 2009   (RSS)

By Shore - November 20, 2009

Hello,

How can I publicly populate a cms builder multiple-select list field?

I have a list field called 'activity' that is grabbing it's values from another table 'activities' where $num is the value and $title is the label.

At the top of my page that has the html form, the mysql UPDATE statement for the activity field is as follows:

-----------------------

activity = '".mysql_real_escape_string( $_REQUEST['activity'] )."',

-----------------------

then in my html form

-------------------------
<select multiple="multiple" name="activity" id="activity">
<?php foreach ($activitiesRecords as $recordActivity): ?>
<option value="<?php echo $recordActivity['num'] ?>"><?php echo $recordActivity['title'] ?></option><?php endforeach; ?>
</select>
-------------------------

On the html form, a user can select multiple activities, but when the record is added to cms builder only the last selected activity becomes checked in the cms builder record.

Any ideas?

Thanks

Re: [Shore] publicly populate a list field

By Chris - November 20, 2009

Hi Shore,

I'm having trouble figuring out exactly what it is you're trying to accomplish. Can you please attach the complete PHP source code for the page (or pages) you're working on so I can take a closer look?
All the best,
Chris

Re: [Shore] publicly populate a list field

By Chris - November 23, 2009

Hi Shore,

Multi-value fields are stored in CMS Builder's databases as a tab-separated list of values, with extra tabs on both ends to facilitate easy searching.

When submitting a form with a <select multiple> with an "array name" (eg. activity[]), PHP should be giving you an array of values. Since you said above that "$num is the value", you'll want to make sure that your <option values> are record "nums":

<select multiple name="activity[]" id="activity" style="margin-left: 280px;"><?php foreach ($activitiesRecords as $recordActivity): ?>
<option value="<?php echo $recordActivity['num'] ?>"><?php echo $recordActivity['title'] ?></option><?php endforeach; ?>
</select>

To convert the array of values to a tab-padded, tab-separated list, you can use the following code:

activity = '\t".mysql_real_escape_string( join("\t", $_REQUEST['activity'] ) )."\t',

I hope this helps! Please let me know if you have any questions.
All the best,
Chris