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: [chris] publicly populate a list field

By Shore - November 20, 2009

Hi Chris,

Attached is my php.

In CMS Builder, organization.activity is a list field that pulls it's values from another table called activities using num as the value and title as the label.

Between lines 273-275

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


I'm attempting to post it's array value and insert it into the organization.activity list field.

Appreciate any help. Let me know if you have any questions.

Thanks

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