close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

form multi select list insert records

Thread begun 6/06/2016 12:34 pm by Christopher West | Last modified 2/20/2019 10:52 am by Ray Borduin | 7037 views | 52 replies |

Christopher WestCommunity Expert

form multi select list insert records

Hi Ray, as discussed recently on a premiere ticket I wanted to look at using some code I found to allow client to do a multi select from a drop down and then save each into records.

So I need to somehow loop through each item that's selected in the multi select list on the form and perform an insert.

Also inside the multi select list I have grouped the sub-categories (child) into their corresponding parent category (at the moment its only displaying the ID value rather than the name, so I need to tweak the recordset that can do this (since the same table is storing both parent and child).

So I wonder upon form submit, what would the process be to do a multi insert for each value in the multi select list? I wanted to try myself before we use up the remaining support ticket I purchase.

I am providing the code and screenshot as reference.



php:
<select style="width: 260px;" multiple="" class="chosen-select">

                                <?php
while(!$rsCategory->atEnd()) { //dyn select
?> 
<?php
  
if ("".($rsCategory->getColumnVal("CategoryParentID"))  ."" != "0") {  // WebAssist Show If
?><optgroup label="<?php echo($rsCategory->getColumnVal("CategoryParentID")); ?>">
<?php
  
// ("".($rsCategory->getColumnVal("CategoryParentID"))  ."" != "0")
?>
<option value="<?php echo($rsCategory->getColumnVal("CategoryID")); ?>"><?php echo($rsCategory->getColumnVal("CategoryName")); ?></option>
</optgroup>
<?php
  $rsCategory
->moveNext();
//dyn select
$rsCategory->moveFirst();
                                
?>
                              </select>



Chris

Sign in to reply to this post

Ray BorduinWebAssist

Usually you would name your select list with brackets like:

<select name="selectedCats[]"

That way when submitted it would create an array from the values in the $_POST['selectedCats']. Then you could loop through that array doing your inserts.

Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

Hi, so an array is created by add [] then how would I implement the loop for the insert record?

Sign in to reply to this post

Ray BorduinWebAssist

You could use a FOR loop to loop on the array and wrap it around the insert code.

Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

could you provide some sample code please?

Chris

Sign in to reply to this post

Ray BorduinWebAssist

php:
<?php

for ($x=0$x<sizeof($_POST['selectedCats']); $x++;) {
?>
<?php
// insert to database code here with binding set to: 
<?php echo($_POST['selectedCats'][$x]); ?>
?>
<?php
}
?>
Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

Hi, Its almost working. it does insert the number of items I have selected. however, what its doing is inserted the same ID for all entries. After my test if I select all categories from the list, the ID which is being written to the table is the very first ID from those that I select.

For example is my database table consider of:
CategoryID
6
7
8
9
10

and if I select all the above in my multi-select list then it will create 5 records in the catalog table BUT all with ID 6 being stored.

Here is the code for the insert and the code for the html form:

Chris

php:
<?php

for ($x=0$x<sizeof($_POST['selectedCats']); $x++) {
?>
<?php
if (($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_SERVER["HTTP_REFERER"]) && strpos(urldecode($_SERVER["HTTP_REFERER"]), urldecode($_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"])) > 0) && isset($_POST)) {
  
$InsertQuery = new WA_MySQLi_Query($ecartdb);
  
$InsertQuery->Action "insert";
  
$InsertQuery->Table "`catalog`";
  
$InsertQuery->bindColumn("CatalogCategoryID""i""".((isset($_POST["selectedCats"]))?implode(",",$_POST["selectedCats"]):"")  ."""WA_DEFAULT");
  
$InsertQuery->bindColumn("CatalogProductID""i""".$_SESSION['ProductID']  ."""WA_DEFAULT");
  
$InsertQuery->saveInSession("CatalogID");
  
$InsertQuery->execute();
  
$InsertGoTo "product-manage.php";
  if (
function_exists("rel2abs")) $InsertGoTo $InsertGoTo?rel2abs($InsertGoTo,dirname(__FILE__)):"";
  
$InsertQuery->redirect($InsertGoTo);
}
?>
<?php
}
?>




php:
<select name="selectedCats[]" style="width: 260px;" multiple="" class="chosen-select">

  <?php
  
while(!$rsCategory->atEnd()) { //dyn select
  
?> 
    <?php
    
if ("".($rsCategory->getColumnVal("CategoryParentID"))  ."" != "0") {  // WebAssist Show If
    
?>
          <optgroup label="<?php echo($rsCategory->getColumnVal("CategoryID")); ?>">
    <?php
    
// ("".($rsCategory->getColumnVal("CategoryParentID"))  ."" != "0")
    
?>
              <option value="<?php echo($rsCategory->getColumnVal("CategoryID")); ?>"><?php echo($rsCategory->getColumnVal("CategoryName")); ?></option>
          </optgroup>
    <?php
  $rsCategory
->moveNext();
  } 
//dyn select
  
$rsCategory->moveFirst();
  
?>
</select>
Sign in to reply to this post

Ray BorduinWebAssist

This line:
$InsertQuery->bindColumn("CatalogCategoryID", "i", "".((isset($_POST["selectedCats"]))?implode(",",$_POST["selectedCats"]):"") ."", "WA_DEFAULT");

Should be:
$InsertQuery->bindColumn("CatalogCategoryID", "i", "".($_POST["selectedCats"][$x]) ."", "WA_DEFAULT");

Sign in to reply to this post
Did this help? Tips are appreciated...

Christopher WestCommunity Expert

Thanks Ray that seems to do the trick. With regard to the Update page. I assume that's going to be more involved. because I am going to need to read in some how all the values that are stored into the catalog table so they show as selected on my update page. And then on form submit I need to perform a delete server behavior. Do u have any guidelines for this? Chris

Sign in to reply to this post

Ray BorduinWebAssist

You could just add the insert just like it is on the insert page, and then add a delete server behavior before it that deletes all of the old ones based on the CatalogProductID.... You wouldn't actually do an update, just delete them all and re-insert them all.

Sign in to reply to this post
Did this help? Tips are appreciated...
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...