close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

merge arrays to one string

Thread began 11/05/2009 11:59 am by Steve | Last modified 11/05/2009 4:18 pm by Jason Byrnes | 928 views | 3 replies |

Steve

merge arrays to one string

Hello,
I have a few repeating regions with checkbox values that I would like to append as one comma separated string.

Individual groups can be posted using the implode function from checkbox arrays []

But trying to merge these to one string is posing a problem.

Here is my code:

$a1 = ((isset($_POST["1pcCB"]))?implode(", ", $_POST["1pcCB"]):""));
$a2 = ((isset($_POST["desksetCB"]))?implode(", ", $_POST["desksetCB"]):"");
$a3 = ((isset($_POST["trimlineCB"]))?implode(", ", $_POST["trimlineCB"]):"");
$a4 = ((isset($_POST["bedRailCB"]))?implode(", ", $_POST["bedRailCB"]):"");
$a5 = ((isset($_POST["specialtyCB"]))?implode(", ", $_POST["specialtyCB"]):"");
$requestedPhones = array_merge($a1, $a2, $a3, $a4, $a5);



Is this not an array: ((isset($_POST["1pcCB"]))?implode(", ", $_POST["1pcCB"]):"")) ?

I'm looking to post $requestedPhones as the final string to be inserted. The individual elements seem to work, such as ((isset($_POST["1pcCB"]))?implode(", ", $_POST["1pcCB"]):"")) properly inserts a comma separated string to the target DB field.
And the array_merge() function works in testing if I use a test array such as :


$my_array1 = array ( 'a', 'b', 'c' );
$my_array2 = array ( 'd', 'e', 'f' );
// append one array to the other
$my_array = array_merge($my_array1, $my_array2);

// print the resulting array to see if it worked
print_r($my_array);

I believe the problem is here:
((isset($_POST["1pcCB"]))?implode(", ", $_POST["1pcCB"]):""))
and that this is not a pure array or some extra syntax is conflicting with the setting of this value to the new var ($a1)...

Anyone see the problem and can offer the solution?

Sign in to reply to this post

Jason ByrnesWebAssist

The implode function is used to convert an array to a string.


if you have a group of checkboxes with the same name ending in "[]". the square brackets crates an array.


Your code using the implode function is converting that array to comma separated string.

No extra functions are needed to convert the form output into an array in this case.


Instead of using:
((isset($_POST["1pcCB"]))?implode(", ", $_POST["1pcCB"]):""));


just use:
((isset($_POST["1pcCB"]))?$_POST["1pcCB"]:""));

Sign in to reply to this post

Steve

Thanks


So since the array already exists I need to array_merge the available arrays:

$a1 =  ((isset($_POST["1pcCB"]))?$_POST["1pcCB"]:"");
$a2 = ((isset($_POST["desksetCB"]))?$_POST["desksetCB"]:"");

$PrerequestedPhones = array_merge($a1, $a2);



Then, implode that value to comma delimit and have ready for the Insert

$requestedPhones = ((isset($_POST["PrerequestedPhones"]))?implode(", ", $_POST["PrerequestedPhones"]):"");



I then set the value to be inserted via DA as "$requestedPhones"

Makes sense, but on test I did not find the values inserted. No errors reported , just no values inserted.

It's not that I need set the value to a hidden field is it? Seems that value should be present as the var: $requestedPhones

Sign in to reply to this post

Jason ByrnesWebAssist

Your code here:

$a1 =  ((isset($_POST["1pcCB"]))?$_POST["1pcCB"]:"");
$a2 = ((isset($_POST["desksetCB"]))?$_POST["desksetCB"]:"");

$PrerequestedPhones = array_merge($a1, $a2);



is creating an application variable named $PrerequestedPhones not a POST variable.

In this code:

$requestedPhones = ((isset($_POST["PrerequestedPhones"]))?implode(", ", $_POST["PrerequestedPhones"]):"");




you are referencing a POST variaberl that does not exist. It should be:

$requestedPhones = ((isset($PrerequestedPhones))?implode(", ", $PrerequestedPhones):"");
Sign in to reply to this post

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...