close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

PHP MYSQL Form - Multiple Select List

Thread began 12/30/2009 11:51 am by karen.marley367377 | Last modified 5/03/2011 1:03 pm by neonanonite424251 | 31266 views | 29 replies |

karen.marley367377

PHP MYSQL Form - Multiple Select List

Hi

I have a list in an insert form which I have set to multiple select. When more than one item in the list is selected, the results shows only the last item selected.

<select name="PropertyType" size="6" multiple="multiple" id="PropertyType">
<?php
do {
?>
<option value="<?php echo $row_rspropertyTypes['PropertyType']?>"><?php echo $row_rspropertyTypes['PropertyType']?></option>
<?php
} while ($row_rspropertyTypes = mysql_fetch_assoc($rspropertyTypes));
$rows = mysql_num_rows($rspropertyTypes);
if($rows > 0) {
mysql_data_seek($rspropertyTypes, 0);
$row_rspropertyTypes = mysql_fetch_assoc($rspropertyTypes);
}
?>
</select>
Any ideas?

Sign in to reply to this post

Eric Mittman

You should add in square brackets after the name of the element so that it passes an array of values rather than just the last value like this:

php:
<select name="PropertyType[]" size="6" multiple="multiple" id="PropertyType">



This is a nifty trick in php that you can use to get back an array of values rather than a single value.

Sign in to reply to this post

karen.marley367377

PropertyType[]

I have changed the list name to include the square brackets as recommended. If I change the insert form to include the form field PropertyType[], then the results shows blank. Have tried leaving the insert form to include PropertyType (without the brackets) and the result shows "Array".

Any suggestions?
((isset($_POST["PropertyType"]))?$_POST["PropertyType"]:"")

Sign in to reply to this post

Eric Mittman

Once you get an array of values rather than just a single value you will need to make use of php array functions to manipulate the data. If you would like to just print out the values from the array in a comma separated list you could do so using the implode function like this:

php:
implode(", ", $_POST["PropertyType"])



You can do many things with arrays in php, you should take some time to review the implode function and some others for manipulating the arrays to get an idea for the things you can do.

php.net/implode

Sign in to reply to this post

karen.marley367377

Implode

Thanks Eric your solution works.

I have added this solution to the update page. How can I craft the multiple select field in the update pageso it highlights the items as inserted with the Insert Page (currently, if user does not update this field, it returns blank in the results page).

Many thanks

Sign in to reply to this post

Eric Mittman

Getting the list to show the selections will be a little trickier. You will need to start by creating a new array of the values stored in that column. You can use the explode function like this to do that:

php:
$your_array_var = explode(", ", $row_YourRS['PropertyType']);



Once you do this you will then need to loop over this array for each entry in the list and compare the value of the option with values in this array. If the values match then you will need to add the selected="selected" attribute to that option.

This is a little custom and will require hand coding the loop and comparison.

Sign in to reply to this post

karen.marley367377

Holding the Value of a Multiple Select list on the Update form

Hi Eric,
Do you have any examples or suggestions on how I can do this - been searching the web to no avail!


Originally Said By: Eric Mittman
  Getting the list to show the selections will be a little trickier. You will need to start by creating a new array of the values stored in that column. You can use the explode function like this to do that:

php:
$your_array_var = explode(", ", $row_YourRS['PropertyType']);


Once you do this you will then need to loop over this array for each entry in the list and compare the value of the option with values in this array. If the values match then you will need to add the selected="selected" attribute to that option.

This is a little custom and will require hand coding the loop and comparison.  
Sign in to reply to this post

Eric Mittman

You would have to start with the value string that comes from the db and turn it into an array like this:

php:
$the_selections = explode(", ", $row_YourRS['the values']);



Once you have an array of values you can add in some code per option in the select list to check if the value of that option is in the list. Here is an example of what that code would look like:

php:
<option <?php echo ((in_array("1"$the_selections))?"selected='selected'":""); ?>value="1">one</option>



What I have listed as 1 in this example should be replaced with your recordset values for these options.

If you use this in_array() function it allows you to make the comparison without looping and making an if statement. I'm including a small two page example with the code so that you have a reference for this.

Attached Files
update_multilist.zip
Sign in to reply to this post

karen.marley367377

Holding Values for a Multiple List on an Update form

Hi Eric,

Apologies I cannot get this to work as am unsure how to do this - the error message I get with the multiple select form is :

Warning in_array [function.in-array]: Wrong datatype for second argument on line 234 >Assembly and Leisure

When an option or options from the list - the results page returns a value of Array. Then if the update form is accessed again, and the multiple select list is not changed by the user. The word Array disappears from the results page (i.e. and from the database)


I know it is to do with where I have put the code you kindly provided but am stuck.

This is the list option:
<select name="PropertyType[]" size="6" multiple="multiple" id="PropertyType[]" title="<?php echo $row_WADAproperties['PropertyType']; ?>">
<?php
do {
?><option value="<?php echo $row_rspropertytypes['PropertyType']?>"<?php if (!(in_array($row_rspropertytypes['PropertyType'], $row_WADAproperties['PropertyType']))) {"selected='selected'";} ?>><?php echo $row_rspropertytypes['PropertyType']?></option>
<?php
} while ($row_rspropertytypes = mysql_fetch_assoc($rspropertytypes));
$rows = mysql_num_rows($rspropertytypes);
if($rows > 0) {
mysql_data_seek($rspropertytypes, 0);
$row_rspropertytypes = mysql_fetch_assoc($rspropertytypes);
}
?>
</select>
And this is the explode code is on the last line of the following:
$WA_table = "properties";
$WA_redirectURL = "properties_Results.php";
$WA_keepQueryString = false;
$WA_indexField = "MainPropertiesID";
$WA_fieldNamesStr = "SLNNumberID|FirstName|LastName|Company|PropertyType|Assembly|HealthCare|Office|SaleORLease|RegionName|Address1|Address2|Address3|City|County|PostCode|Country|ThumbPath|Thumb|PicturePath|Picture|AgentLogo|PropertyDetails|Size|Sep|SizeMax|SizeType|shortDescription|longdescription|CURR|PriceDetails|RentalPeriod|Price|Status";
$WA_fieldValuesStr = "".((isset($_POST["SLNNumberID"]))?$_POST["SLNNumberID"]:"") ."" . "|" . "".$row_WADAproperties['FirstName'] ."" . "|" . "".$row_WADAproperties['LastName'] ."" . "|" . "".$row_WADAproperties['Company'] ."" . "|" . "".((isset($_POST["PropertyType"]))?explode(", ", $_POST["PropertyType"]):"")

Please Help! X

Sign in to reply to this post

Eric Mittman

In order for the page to work like this you must ensure that you have the array of values posting to a page to begin with. Do you have this part worked out? This is the first thing we were working on.

Once you have the list posting an array of values you would then store the imploded array into your db.

On the update page you will have a recordset that will select the record based on the id. In the field in the recordset that holds the values you will need to explode the values into an array, just like you imploded the array to get the string of values but backwards. Once you get the string of values into an array you can then use the in_array function to compare values to values in this array to see if they are present.

The select list that you have should be populated by a different recordset. The record that is being updated should come from it's own recordset. So the select list on the page should have a loop for one recordset to populate the available options. Then you will be using the in array function with the variable that you set based on the values that were in the other recordset to compare against. So you will be comparing a value from the recordset that is populating the selections with the array of values you exploded earlier.

If you continue to have trouble with this please post back and include both pages in question so that I can get a feel for how you have them working now and what needs to be done to get them working correctly.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...