Pull down (multi value) - Get options from database

9 posts by 3 authors in: Forums > CMS Builder
Last Post: December 17, 2010   (RSS)

By ryanGT - November 22, 2010

Hi,

Im having some troubles with extracting the multi-value results from on of my fields in the CMSB.

Basically I am trying to create a for each loop that for every value in the multi-value pull down it publishes the relevant youtube video.

I already have the embed code working for a static video, but need to make it dynamic so the user can add multiple videos to the system.

If you know a better way of doing this, or can help me out that would be grand!

thanks
Ryan

Re: [ryanGT] Pull down (multi value) - Get options from database

By Jason - November 22, 2010

Hi Ryan,

In CMS Builder, multi value drop downs are stored as a string separated by tab characters("\t"). The best way to work with these values is to turn them into an array.

In this example, we'll assume we're using a multi value field called "category".

$category = $record['category'];
$category = trim($category,"\t");
$category = explode("\t",$category);


What we did here was take the string value from the record and assign it to $category. Next, we stripped away the first and last \t from the string. We then used explode to turn all the values separated by a \t be elements in an array. So at the end of this code the variable $category is an array with each element of the array being a selected value. You can now use a foreach loop to go over each element of the array.

Hope this helps. Let me know if you run into any problems.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Pull down (multi value) - Get options from database

By ryanGT - November 24, 2010

Hi,

Thanks for that, but I'm still having some troubles.

Would you be able to provide me with an example page? I'm fairly new to PHP programming, I have searched through other posts to see if I can solve the issue myself, but to no avail.

Kind regards

Ryan

Re: [ryanGT] Pull down (multi value) - Get options from database

By Jason - November 24, 2010

Hi Ryan,

It may be difficult to give a good example since you're trying to accomplish something specific. If you could attach the .php file you're working with, I can take a look at that.

Also, please let me know the name of the mult-value field you're working with and exactly what is being stored there.

Once I have this, I should be able to give you a more specific example of how to do this.

Thanks.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Pull down (multi value) - Get options from database

By ryanGT - November 26, 2010

Hi Jason,

I have attached the PHP file that I am working with.

The name of the multi-value is 'youtube_video_url'

This field then links to a table or section called 'cms_youtube_videos' and has two fields called 'title' and 'youtube_video_id'

I am not interested in displaying the title of the youtube videos, just the youtube video id. I was then going to create a for each loop and wrap the video id's into the youtube video embed code.

If there is anything else that you need, let me know

Kind regards

Ryan
Attachments:

gsu-draft.php 4K

Re: [ryanGT] Pull down (multi value) - Get options from database

By Jason - November 26, 2010

Hi Ryan,

Try something like this:

<?php
$youtube_videos = trim($gsu_draftRecord['youtube_video_url'],"\t");
$youtube_videos = explode("\t",$youtube_videos);
?>

<?php foreach($youtube_videos as $videoId):?>
**OUTPUT VIDEO HERE**
<?php endforeach ?>


In your foreach loop, the variable $videoId is the value field of your multi-select drop down.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Pull down (multi value) - Get options from database

By Rusty - December 17, 2010 - edited: December 17, 2010

So based upon what you've told me, if I want to submit a form with multiple values selected, hard coded, I would use your / CMSB /t switch.


Ah HA~!!

You mention how to strip the tabs out, with trim and explode.

What if I wanted to go the other direction?

I noticed another switch used on the errorsAndAlerts the \n switch, what's that one do?

So I'm thinking that this might work setting/putting Multiple Values INTO the Database from a form.


mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
state = '".mysql_escape("Ohio\tPennsylvania")."',


?? Just wondering.
Rusty

Re: [Rusty] Pull down (multi value) - Get options from database

By Jason - December 17, 2010

Hi Rusty,

You're close. You need to have a tab character at the beginning and end of the string as well:

mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
state = '".mysql_escape("\tOhio\tPennsylvania\t")."',


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/