close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Loop Insert from an array?

Thread began 8/30/2010 12:00 pm by Steve | Last modified 9/01/2010 1:16 pm by Jason Byrnes | 1480 views | 9 replies |

Steve

Loop Insert from an array?

Hello,
I've searched the forums and not seen anything that points out how to take a current single record insert and then modify it IF there are more than one record to insert.

Any pointers on what it would take to modify the Insert behavior to do a loop inserting all values from an array?

Using the latest version of DA, in a PHP environment with MySQL 5.1

code:

// WA Application Builder Insert
if ($_SERVER["REQUEST_METHOD"] == "POST") // Trigger
{
$WA_connection = $emailList;
$WA_table = "phplist_user_user";
$WA_sessionName = "phplist_user_user_id";
$WA_redirectURL = "";
$WA_keepQueryString = false;
$WA_indexField = "id";
$WA_fieldNamesStr = "email|confirmed|uniqid|htmlemail|subscribepage";
$WA_fieldValuesStr = "".((isset($_POST["fldEmail"]))?$_POST["fldEmail"]:"") ."" . "|" . "1" . "|" . md5(uniqid(mt_rand())) . "|" . "1" . "|" . "4";
$WA_columnTypesStr = "',none,''|none,none,NULL|',none,''|none,none,NULL|none,none,NULL";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_emailList;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>




Note, I'm also setting a random value to one of the fields and inserting php code as a value was not found as an option in the behavior dialog. No problem as I can remove the quotation marks making it no longer a string but it would be a nice enhancement to also use php functions as insert values.

I can also hand write the insert loop but the reason to use these extensions is the speed of implementation so learning how to loop an array in DA Insert would be of value.

Thanks,

Steve

Sign in to reply to this post

Jason ByrnesWebAssist

to create an array loop, you would use a for statement around the insert code:

for($i = 0; $i < sizeof($arrayName); $i++) {
<insert code here>
}

Sign in to reply to this post

Steve

Thanks Jason.

Sign in to reply to this post

Jason ByrnesWebAssist

you're welcome.

Sign in to reply to this post

Steve

Harder than I thought...

Hello,

I can't seem to get the array posted to the DA Insert Behaviour.

I can print_r the array and see the results present but in the js alert I set in the behaviour the array never shows up.

<?php require_once("../../Connections/emailList.php"); ?>
<?php require_once("../../WA_DataAssist/WA_AppBuilder_PHP.php"); ?>

<?php
// WA Application Builder Insert
if (isset($_POST["InsertMe"])) // Trigger
{


echo "<script language=\"JavaScript\">\n";
echo "alert('";
echo '<pre>';
print_r($matches[0]);
echo '</pre>';
echo "we made it this far');\n";
echo "</script>";

// loop to allow array of inserts
for($i = 0; $i < sizeof($matches[0]); $i++) {

{
$WA_connection = $emailList;
$WA_table = "phplist_user_user";
$WA_sessionName = "phplist_user_user_id";
$WA_redirectURL = "";
$WA_keepQueryString = false;
$WA_indexField = "id";
$WA_fieldNamesStr = "email|confirmed|uniqid|htmlemail|subscribepage";
$WA_fieldValuesStr = "$matches[0]" . "|" . "1" . "|" . md5(uniqid(mt_rand())) . "|" . "1" . "|" . "4";
$WA_columnTypesStr = "',none,''|none,none,NULL|',none,''|none,none,NULL|none,none,NULL";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_emailList;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
}
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Search, Find and Insert</title>
</head>

<body>


<form method="post">
Please enter full URL of the page to parse (including http://):<br />
<input type="text" name="url" size="65" value="<?php echo $the_url; ?>"/><br />
or enter text directly into textarea below:<br />
<textarea name="text" cols="50" rows="15"></textarea>
<br />
<input type="submit" value="Parse Emails" />
</form>


<p>


<form id="formUser" name="formUser" method="post" action="">
<?php
$the_url = isset($_REQUEST['url']) ? htmlspecialchars($_REQUEST['url']) : '';
?>


<?php
if (isset($_REQUEST['url']) && !empty($_REQUEST['url'])) {
// fetch data from specified url
$text = file_get_contents($_REQUEST['url']);
}
elseif (isset($_REQUEST['text']) && !empty($_REQUEST['text'])) {
// get text from text area
$text = $_REQUEST['text'];
}

// parse emails
if (!empty($text)) {
$res = preg_match_all("/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",$text,$matches);


if ($res) {
// loop through array only taking the first one of any duplicates with the array_unique function
foreach(array_unique($matches[0]) as $email) {
echo $email . "<br />";
}
}
else {
echo "No emails found.";
}
}
?>
</p>

<p>
<label for="fldEmail"></label>
Email: **
<input name="fldEmail" type="text" id="fldEmail" size="45" />
</p>
<p>&nbsp;</p>
<p>
<input type="submit" name="InsertMe" id="InsertMe" value="Submit" />
</p>
<p>&nbsp;</p>
<p>rev3</p>
</form>
<p>&nbsp;</p>
</body>
</html>



The page allows you to specify some page that has email addresses in it then using preg_match_all makes an array of the results. I now need to pass that in the insert loop to the DB.

I'm down to thinking I need pass the array in a $_POST from a hidden field but tests have not worked with that method so I am at a loss.

Any ideas?

Sign in to reply to this post

Jason ByrnesWebAssist

your loop should probably be:
for($i = 0; $i < sizeof($matches); $i++) {


the in the insert, reference the array index as $matches[$i]:

$WA_fieldValuesStr = "$matches[$i]" . "|" . "1" . "|" . md5(uniqid(mt_rand())) . "|" . "1" . "|" . "4";

Sign in to reply to this post

Steve

That did not work... I think the problem is just getting the $match array to pass to the DA Insert code.


$match is created within the form
the button is labeled id/name "InsertMe"
and the primary trigger looks for this as referenced by:

<?php 
// WA Application Builder Insert
if (isset($_POST["InsertMe"])) // Trigger
{



But nothing gets inserted nor does any value get shown in the js alert I am using to see if the array is present:


<?php 
// WA Application Builder Insert
if (isset($_POST["InsertMe"])) // Trigger
{

echo "<script language=\"JavaScript\">\n";
echo "alert('";
echo '<pre>';
print_r($matches[0]);
echo '</pre>';
echo "we made it this far');\n";
echo "</script>";

// loop to allow array of inserts
for($i = 0; $i < sizeof($matches); $i++) {

{
$WA_connection = $emailList;
$WA_table = "phplist_user_user";
$WA_sessionName = "phplist_user_user_id";
$WA_redirectURL = "";
$WA_keepQueryString = false;
$WA_indexField = "id";
$WA_fieldNamesStr = "email|confirmed|uniqid|htmlemail|subscribepage";
$WA_fieldValuesStr = "$matches[$i]" . "|" . "1" . "|" . md5(uniqid(mt_rand())) . "|" . "1" . "|" . "4";
$WA_columnTypesStr = "',none,''|none,none,NULL|',none,''|none,none,NULL|none,none,NULL";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_emailList;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
}
}
?>
Sign in to reply to this post

Jason ByrnesWebAssist

create your match array as a session so that it will be available when the form submits

Sign in to reply to this post

Steve

Thanks again Ray it worked...

I'll post the code in case anyone else needs similar methods. There are also a couple cool things like the random ID generation and I also do a setting of DateTime to the DB.

<?php
session_start(); // Start session
?>

<?php
// WA Application Builder Insert
if (isset($_POST["InsertMe"])) // Trigger
{

// loop to allow array of inserts
for($i = 0; $i < sizeof($_SESSION['match']); $i++) {

{
$WA_connection = $emailList;
$WA_table = "phplist_user_user";
$WA_sessionName = "phplist_user_user_id";
$WA_redirectURL = "";
$WA_keepQueryString = false;
$WA_indexField = "id";
$WA_fieldNamesStr = "email|confirmed|entered|uniqid|htmlemail|subscribepage";
$WA_fieldValuesStr = "".$_SESSION['match'][$i] ."" . "|" . "1" . "|" . (date('Y-m-d H:i:s')) . "|" . md5(uniqid(mt_rand())) . "|" . "1" . "|" . "4";
$WA_columnTypesStr = "',none,''|none,none,NULL|',none,NULL|',none,''|none,none,NULL|none,none,NULL";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_emailList;
mysql_select_db($WA_connectionDB, $WA_connection);
if (!session_id()) session_start();
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
$_SESSION[$WA_sessionName] = mysql_insert_id();
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
}
}
unset($_SESSION['match']);
?>



note iteration of array :
$_SESSION['match'][$i]


Unique query results added to the session here:

// put results into session as an array
$_SESSION['match'] = (array_unique($matches[0]));
Sign in to reply to this post

Jason ByrnesWebAssist

Great, glad to hear it worked.

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