Site maintenance: Tuesday 17 Nov, from 10:00 UTC, about 4–6 hours. Details

This cutover relies on some testing that is still incomplete so may be cancelled at short notice

Forums / Customization from the Admin / Creating a product html generator for emails

Creating a product html generator for emails

Started by smithdp1 on 31 Jul 2012, 10:42 PM · Views: 38,220

Results 1 to 13 of 13
31 Jul 2012, 10:42 PM
#1
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Creating a product html generator for emails

Here I go again getting out of my comfort zone of knowledge. What I am trying to do is, in the admin I want to be able to input product id's into an input field and clcik a button and have html generated with that product(s) image(href linked to product), title(href linked to product), description and price. I found an old script that someone did for os commerce and I am trying to make it work for zen cart. THIS COULD BE A NICE MOD RELEASE!

There are 2 files:

  1. generate_email.php
  2. email.tpl.php

Here is the code for generate_email.php

<?php

//
// Email Generator V1.00
//
// This script generates an HTML email from a list of product IDs for
// use in an email campaign.
//
// 
// 
//

$template_file = "/catalog/zcadmin/email.tpl.php";

require('includes/application_top.php');
//require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PRODUCT_INFO);

// Check login

//
// Get the action, either from the form that was posted or from the URL.
//

if (isset($_GET['action'])) {

    $action = $_GET['action'];

} else {

    $action = $_REQUEST['form']['action'];

}

//
// Perform based on the particular action that was defined.
//

if ($action == "generateemail") {

    $form = $_REQUEST['form'];

    //
    // Clean the product list that the user submitted by removing and carriage returns
    // or line feeds.  Also replace and commas with spaces and kick out multiple spaces
    // from the string.  Finally, we trim any spaces from the beginning or end of the
    // string and strip out any values that are not numeric (since all of the product
    // IDs are numeric).
    //

    $form['productlist'] = preg_replace("/[\r\n\,]/", " ", $form['productlist']);
    $form['productlist'] = preg_replace("/\s+/", " ", $form['productlist']);
    $form['productlist'] = trim($form['productlist']);

    //
    // If the product list was defined, then we split it out into an array at the spaces.
    //

    if (strlen($form['productlist']) > 0) {

        $product_ids = explode(" ", $form['productlist']);

    }

    //
    // If we have product IDs, then we need to get the necessary product information from
    // the database.  We store the product information from the database into an array for
    // reference in the output.  For compatability, we use the same functions that are
    // used by OScommerce.
    //

    if (count($product_ids) > 0) {

        $sql = "SELECT p.products_id, p.products_status, pd.products_name, pd.products_description,
                  p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price,
                  p.products_tax_class_id, p.products_date_added, p.products_date_available,
                  p.manufacturers_id FROM " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
                WHERE p.products_id IN ('".implode("','", $product_ids)."') AND pd.products_id = p.products_id
                  AND pd.language_id = '" . $languages_id . "'";


        $results = $db->Execute($sql);

        if ($results->RecordCount() > 0) {

            while ($row = $db->Execute($results)) {

                $product['id'.$row['products_id']] = $row;

            }

        }

        unset($sql, $row, $results);

        //
        // Get the output template information from the file and store the data for
        // reference.
        //

        $fhandle = fopen($template_file, "r");
        $content = fread($fhandle, filesize($template_file));
        fclose($fhandle);

        $lines = preg_split("/[\r\n]/", $content);

        if (count($lines) > 0) {

            foreach ($lines as $line) {

                if (preg_match("/\\$\\$(\S+)/", $line, $matches)) {

                    $section = strtolower($matches[1]);

                } elseif (isset($section)) {

                    $template[$section] .= $line."\n";

                }

            }

        }

        unset($content, $sections, $section);

        $page = 2;

    }

    $edit = $form;

    unset($form, $action);

}

?>
<html>
<head>
<title>Email Generator V1.00</title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<style type="text/css">
<!--
body {
	background-color: #FFFFFF;
	font-family: tahoma;
	font-size: 11px;
}

label {
	margin: 3px 0 0 0;
	display: block;
	width: 130px;
	float: left;
	padding: 3px 0 0 0;
	clear: both;
}

label.required {
	font-weight: bold;
}

table {
	font-size: 11px;
}
-->
</style>
</head>
<body>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php //require(DIR_WS_INCLUDES . 'column_left.php'); THIS WAS CAUSING ERRORS SO I COMMENTED OUT FOR NOW ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading">Email Generator V1.00</td>
            <td class="pageHeading" align="right"><?php echo zen_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>
<?php

//
// The output depends on what page is currently being displayed.
//

if ($page == 2) {
?>
<br>
<b>Results</b><br>
<?php
    if (count($product_ids) > 0) {
?>
<table cellpadding="2" cellspacing="0">
<tr style="font-weight: bold;"><td>Product ID</td><td>Status</td><td>Name</td></tr>
<?php

        $html_string = $template['header'];

        foreach ($product_ids as $product_id) {

            if (isset($product['id'.$product_id])) {

                $template_data = $product['id'.$product_id];
                $template_data['products_price'] = number_format($template_data['products_price'], 2);

                $html_string .= preg_replace("/\<\<(.*?)\>\>/e", "\$template_data['\\1']", $template['content']);

?>
<tr><td><?=$product_id?></td><td>Found</td><td><?=htmlentities($product['id'.$product_id]['products_name'])?></td></tr>
<?php
            } else {
?>
<tr><td><?=$product_id?></td><td>Not Found</td><td>N/A</td></tr>
<?php
            }

        }

        $html_string .= $template['footer'];
?>
</table>
<?php
    }
?>
<br>
<b>HTML Code</b><br>
<form>
<textarea cols="60" rows="10">
<?=htmlentities($html_string)?>
</textarea>
</form>
<br>
<?php
}
?>
Enter the product IDs in the order in which you'd like for them to appear in the email.
<br>
<form method="post" action="generate_email2.php">
<input type="hidden" name="form[action]" value="generateemail">
<label class="required" for="productlist">Product IDs</label><textarea id="productlist" name="form[productlist]" cols="30" rows="5"><?=$edit['productlist']?></textarea>
<br> <br>
<input type="submit" value="Generate">
</form>
</td>
      </tr>
    </table></td>
  </tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php

require(DIR_WS_INCLUDES . 'application_bottom.php');

//
// Functions
//

function convertHTMLArray($array)
{
    // This function accepts a single-dimensional array, runs through the elements
    // and converts special characters to HTML tags.

    if (is_array($array)) {
        foreach ($array as $key => $val) {
            $array[$key] = htmlspecialchars(stripslashes($val));
        }
    } else {
        $array = NULL;
    }
    return $array;

}
?>

Here is the code for email.tpl.php:

$$HEADER
<TABLE CELLPADDING="15" CELLSPACING="0">
$$CONTENT
<TR>
<TD VALIGN="top" WIDTH="1%"><A HREF="http://www.feelgoodwatches.com/product_info.php/manufacturers_id/<<manufacturers_id>>/products_id/<<products_id>>" TARGET="_blank"><IMG SRC="http://www.feelgoodwatches.com/catalog/images/<<products_image>>" BORDER="0" ALT="<<products_name>>" WIDTH="125"></A></TD>
<TD VALIGN="top" WIDTH="99%"><FONT COLOR="#000000" FACE="Arial" SIZE=2><A HREF="http://www.feelgoodwatches.com/catalog/product_info.php/manufacturers_id/<<manufacturers_id>>/products_id/<<products_id>>" TARGET="_blank"><FONT COLOR="#6868C9" FACE="Arial" SIZE=3><B><<products_name>></B></FONT></A><BR>
<<products_description>>
<BR><FONT COLOR="#FF0000" FACE="Arial" SIZE=3><B>Price:</B> <B>$<<products_price>></B></FONT>
</FONT></TD>
</TR>
<TR>
<TD COLSPAN="2"><HR WIDTH="100%" SIZE="1" COLOR="#7777D8" NOSHADE></TD>
</TR>
$$FOOTER
</TABLE>

As it sits I am getting this error:

PHP Catchable fatal error: Object of class queryFactoryResult could not be converted to string in /includes/classes/db/mysql/query_factory.php on line 101

Please Help????

31 Jul 2012, 10:50 PM
#2
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Re: Creating a product html generator for emails

It is safe to drop these in the root of admin to test.

1 Aug 2012, 1:43 AM
#3
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Re: Creating a product html generator for emails

Help Ajeh!!!

1 Aug 2012, 10:07 AM
#4
rstevenson avatar

rstevenson

Totally Zenned

Join Date:
Nov 2006
Location:
Dartmouth, NS Canada
Posts:
2,400
Plugin Contributions:
0

Re: Creating a product html generator for emails

She doesn't usually need help.

Rob

Breakfast: the most important donut of the day.

1 Aug 2012, 5:12 PM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Creating a product html generator for emails

smithdp1:

while ($row = $db->Execute($results)) {
$product['id'.$row['products_id']] = $row;
}Can't do that.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

1 Aug 2012, 7:12 PM
#6
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Re: Creating a product html generator for emails

Here is the original osc code. Sorry DrByte...i tried to convert some of the code myself to zen.

Thanks for looking!

<?php

//
// Email Generator V1.00
//
// This script generates an HTML email from a list of product IDs for
// use in an email campaign.
//


$template_file = "/catalog/admin/email.tpl.php";

require('includes/application_top.php');
//require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PRODUCT_INFO);

// Check login

//
// Get the action, either from the form that was posted or from the URL.
//

if (isset($_GET['action'])) {

    $action = $_GET['action'];

} else {

    $action = $_REQUEST['form']['action'];

}

//
// Perform based on the particular action that was defined.
//

if ($action == "generateemail") {

    $form = $_REQUEST['form'];

    //
    // Clean the product list that the user submitted by removing and carriage returns
    // or line feeds.  Also replace and commas with spaces and kick out multiple spaces
    // from the string.  Finally, we trim any spaces from the beginning or end of the
    // string and strip out any values that are not numeric (since all of the product
    // IDs are numeric).
    //

    $form['productlist'] = preg_replace("/[\r\n\,]/", " ", $form['productlist']);
    $form['productlist'] = preg_replace("/\s+/", " ", $form['productlist']);
    $form['productlist'] = trim($form['productlist']);

    //
    // If the product list was defined, then we split it out into an array at the spaces.
    //

    if (strlen($form['productlist']) > 0) {

        $product_ids = explode(" ", $form['productlist']);

    }

    //
    // If we have product IDs, then we need to get the necessary product information from
    // the database.  We store the product information from the database into an array for
    // reference in the output.  For compatability, we use the same functions that are
    // used by OScommerce.
    //

    if (count($product_ids) > 0) {

        $sql = "SELECT p.products_id, p.products_status, pd.products_name, pd.products_description,
                  p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price,
                  p.products_tax_class_id, p.products_date_added, p.products_date_available,
                  p.manufacturers_id FROM " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
                WHERE p.products_id IN ('".implode("','", $product_ids)."') AND pd.products_id = p.products_id
                  AND pd.language_id = '" . $languages_id . "'";

        $results = tep_db_query($sql);

        if (tep_db_num_rows($results) > 0) {

            while ($row = tep_db_fetch_array($results)) {

                $product['id'.$row['products_id']] = $row;

            }

        }

        unset($sql, $row, $results);

        //
        // Get the output template information from the file and store the data for
        // reference.
        //

        $fhandle = fopen($template_file, "r");
        $content = fread($fhandle, filesize($template_file));
        fclose($fhandle);

        $lines = preg_split("/[\r\n]/", $content);

        if (count($lines) > 0) {

            foreach ($lines as $line) {

                if (preg_match("/\\$\\$(\S+)/", $line, $matches)) {

                    $section = strtolower($matches[1]);

                } elseif (isset($section)) {

                    $template[$section] .= $line."\n";

                }

            }

        }

        unset($content, $sections, $section);

        $page = 2;

    }

    $edit = $form;

    unset($form, $action);

}

?>
<html>
<head>
<title>Email Generator V1.00</title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<style type="text/css">
<!--
body {
	background-color: #FFFFFF;
	font-family: tahoma;
	font-size: 11px;
}

label {
	margin: 3px 0 0 0;
	display: block;
	width: 130px;
	float: left;
	padding: 3px 0 0 0;
	clear: both;
}

label.required {
	font-weight: bold;
}

table {
	font-size: 11px;
}
-->
</style>
</head>
<body>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading">Email Generator V1.00.1</td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>
<?php

//
// The output depends on what page is currently being displayed.
//

if ($page == 2) {
?>
<br>
<b>Results</b><br>
<?php
    if (count($product_ids) > 0) {
?>
<table cellpadding="2" cellspacing="0">
<tr style="font-weight: bold;"><td>Product ID</td><td>Status</td><td>Name</td></tr>
<?php

        $html_string = $template['header'];

        foreach ($product_ids as $product_id) {

            if (isset($product['id'.$product_id])) {

                $template_data = $product['id'.$product_id];
                $template_data['products_price'] = number_format($template_data['products_price'], 2);

                $html_string .= preg_replace("/\<\<(.*?)\>\>/e", "\$template_data['\\1']", $template['content']);

?>
<tr><td><?=$product_id?></td><td>Found</td><td><?=htmlentities($product['id'.$product_id]['products_name'])?></td></tr>
<?php
            } else {
?>
<tr><td><?=$product_id?></td><td>Not Found</td><td>N/A</td></tr>
<?php
            }

        }

        $html_string .= $template['footer'];
?>
</table>
<?php
    }
?>
<br>
<b>HTML Code</b><br>
<form>
<textarea cols="60" rows="10">
<?=htmlentities($html_string)?>
</textarea>
</form>
<br>
<?php
}
?>
Enter the product IDs in the order in which you'd like for them to appear in the email.
<br>
<form method="post" action="generate_email.php">
<input type="hidden" name="form[action]" value="generateemail">
<label class="required" for="productlist">Product IDs</label><textarea id="productlist" name="form[productlist]" cols="30" rows="5"><?=$edit['productlist']?></textarea>
<br> <br>
<input type="submit" value="Generate">
</form>
</td>
      </tr>
    </table></td>
  </tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php

require(DIR_WS_INCLUDES . 'application_bottom.php');

//
// Functions

//

function convertHTMLArray($array)
{
    // This function accepts a single-dimensional array, runs through the elements
    // and converts special characters to HTML tags.

    if (is_array($array)) {
        foreach ($array as $key => $val) {
            $array[$key] = htmlspecialchars(stripslashes($val));
        }
    } else {
        $array = NULL;
    }
    return $array;

}
?>
1 Aug 2012, 9:51 PM
#7
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Re: Creating a product html generator for emails

rstevenson - yes I know she needs no help haha. All of you guys have really been a great help. I am now totally converted to zen. I have been using other carts for years but to be honest after seeing the support and all of the free add-ons for zen cart i will never install anything different for sure!

So what do you think...is this even possible? I was trying to use this section for some of the code changes...but DrBytes says one of them is not possible...or i just kinda messed it up.

http://www.zen-cart.com/wiki/index.php/Developers_-_Porting_modules_from_osC

2 Aug 2012, 7:53 PM
#8
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Creating a product html generator for emails

Guessing that it might be better to replace this:```
if ($results->RecordCount() > 0) {
while ($row = $db->Execute($results)) {
$product['id'.$row['products_id']] = $row;
}
}

while (!$results->EOF) {
$product['id'.$row->fields['products_id']] = $row->fields;
$results->MoveNext();
}

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Aug 2012, 8:31 PM
#9
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Re: Creating a product html generator for emails

DrByte now I am getting this error:

PHP Fatal error: Cannot use object of type template_func as array in /nfs/c02/h06/mnt/19739/domains/feelgoodwatches.com/html/catalog/zcadmin/generate_email2.php on line 111

2 Aug 2012, 10:05 PM
#10
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Creating a product html generator for emails

Your script is clashing with the $template variable that Zen Cart uses.

You have two options:
a) in your script rename the handful of references to $template to something else
or
b) possibly move the line (near the top of your script) that declares the $template variable to a lower line after application_top is loaded. But this may break the calls your script does to other core Zen Cart components like header.php. So I'd suggest using (a) instead.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Aug 2012, 10:48 PM
#11
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Re: Creating a product html generator for emails

ok so I changed it to this and now no debug errors but it does nothing at all???? When I hit submit it just refreshes and nothing...

I changed $template to $emailtemp

<?php

//
// Email Generator V1.00
//
// This script generates an HTML email from a list of product IDs for
// use in an email campaign.
//
// 
// 
//

$template_file = "email.tpl.php";

require('includes/application_top.php');
//require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_PRODUCT_INFO);

// Check login

//
// Get the action, either from the form that was posted or from the URL.
//

if (isset($_GET['action'])) {

    $action = $_GET['action'];

} else {

    $action = $_REQUEST['form']['action'];

}

//
// Perform based on the particular action that was defined.
//

if ($action == "generateemail") {

    $form = $_REQUEST['form'];

    //
    // Clean the product list that the user submitted by removing and carriage returns
    // or line feeds.  Also replace and commas with spaces and kick out multiple spaces
    // from the string.  Finally, we trim any spaces from the beginning or end of the
    // string and strip out any values that are not numeric (since all of the product
    // IDs are numeric).
    //

    $form['productlist'] = preg_replace("/[\r\n\,]/", " ", $form['productlist']);
    $form['productlist'] = preg_replace("/\s+/", " ", $form['productlist']);
    $form['productlist'] = trim($form['productlist']);

    //
    // If the product list was defined, then we split it out into an array at the spaces.
    //

    if (strlen($form['productlist']) > 0) {

        $product_ids = explode(" ", $form['productlist']);

    }

    //
    // If we have product IDs, then we need to get the necessary product information from
    // the database.  We store the product information from the database into an array for
    // reference in the output.  For compatability, we use the same functions that are
    // used by OScommerce.
    //

    if (count($product_ids) > 0) {

        $sql = "SELECT p.products_id, p.products_status, pd.products_name, pd.products_description,
                  p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price,
                  p.products_tax_class_id, p.products_date_added, p.products_date_available,
                  p.manufacturers_id FROM " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
                WHERE p.products_id IN ('".implode("','", $product_ids)."') AND pd.products_id = p.products_id
                  AND pd.language_id = '" . $languages_id . "'";


        $results = $db->Execute($sql);

       while (!$results->EOF) {
  $product['id'.$row->fields['products_id']] = $row->fields;
  $results->MoveNext();
}

        unset($sql, $row, $results);

        //
        // Get the output template information from the file and store the data for
        // reference.
        //

        $fhandle = fopen($template_file, "r");
        $content = fread($fhandle, filesize($template_file));
        fclose($fhandle);

        $lines = preg_split("/[\r\n]/", $content);

        if (count($lines) > 0) {

            foreach ($lines as $line) {

                if (preg_match("/\\$\\$(\S+)/", $line, $matches)) {

                    $section = strtolower($matches[1]);

                } elseif (isset($section)) {

                    $emailtemp[$section] .= $line."\n";

                }

            }

        }

        unset($content, $sections, $section);

        $page = 2;

    }

    $edit = $form;

    unset($form, $action);

}

?>
<html>
<head>
<title>Email Generator V1.00</title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<style type="text/css">
<!--
body {
	background-color: #FFFFFF;
	font-family: tahoma;
	font-size: 11px;
}

label {
	margin: 3px 0 0 0;
	display: block;
	width: 130px;
	float: left;
	padding: 3px 0 0 0;
	clear: both;
}

label.required {
	font-weight: bold;
}

table {
	font-size: 11px;
}
-->
</style>
</head>
<body>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php //require(DIR_WS_INCLUDES . 'column_left.php'); THIS WAS CAUSING ERRORS SO I COMMENTED OUT FOR NOW ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading">Email Generator V1.00</td>
            <td class="pageHeading" align="right"><?php echo zen_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>
<?php

//
// The output depends on what page is currently being displayed.
//

if ($page == 2) {
?>
<br>
<b>Results</b><br>
<?php
    if (count($product_ids) > 0) {
?>
<table cellpadding="2" cellspacing="0">
<tr style="font-weight: bold;"><td>Product ID</td><td>Status</td><td>Name</td></tr>
<?php

        $html_string = $emailtemp['header'];

        foreach ($product_ids as $product_id) {

            if (isset($product['id'.$product_id])) {

                $template_data = $product['id'.$product_id];
                $template_data['products_price'] = number_format($template_data['products_price'], 2);

                $html_string .= preg_replace("/\<\<(.*?)\>\>/e", "\$template_data['\\1']", $emailtemp['content']);

?>
<tr><td><?=$product_id?></td><td>Found</td><td><?=htmlentities($product['id'.$product_id]['products_name'])?></td></tr>
<?php
            } else {
?>
<tr><td><?=$product_id?></td><td>Not Found</td><td>N/A</td></tr>
<?php
            }

        }

        $html_string .= $emailtemp['footer'];
?>
</table>
<?php
    }
?>
<br>
<b>HTML Code</b><br>
<form>
<textarea cols="60" rows="10">
<?=htmlentities($html_string)?>
</textarea>
</form>
<br>
<?php
}
?>
Enter the product IDs in the order in which you'd like for them to appear in the email.
<br>
<form method="post" action="generate_email2.php">
<input type="hidden" name="form[action]" value="generateemail">
<label class="required" for="productlist">Product IDs</label><textarea id="productlist" name="form[productlist]" cols="30" rows="5"><?=$edit['productlist']?></textarea>
<br> <br>
<input type="submit" value="Generate">
</form>
</td>
      </tr>
    </table></td>
  </tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php

require(DIR_WS_INCLUDES . 'application_bottom.php');

//
// Functions
//

function convertHTMLArray($array)
{
    // This function accepts a single-dimensional array, runs through the elements
    // and converts special characters to HTML tags.

    if (is_array($array)) {
        foreach ($array as $key => $val) {
            $array[$key] = htmlspecialchars(stripslashes($val));
        }
    } else {
        $array = NULL;
    }
    return $array;

}
?>
3 Aug 2012, 3:17 AM
#12
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: Creating a product html generator for emails

Wow, several other conflicts with Zen Cart variables, logic problems, and more.
The following should work:```

<?php // // Email Generator V1.00 // // This script generates an HTML email from a list of product IDs for // use in an email campaign. // $email_template_file = "email.tpl.php"; require('includes/application_top.php'); // set up some variables $action = ''; $section = ''; $emailtemp = array(); // // Get the action, either from the form that was posted or from the URL. // if (isset($_GET['action'])) { $action = $_GET['action']; } elseif (isset($_REQUEST['form'])) { $action = $_REQUEST['form']['action']; } // // Perform based on the particular action that was defined. // if ($action == "generateemail") { $form = $_REQUEST['form']; // Clean the product list that the user submitted by removing // any values that are not numeric (since all of the product // IDs are numeric). // Then remove any double-spaces. // Finally, we trim any spaces from the beginning or end of the string $form['productlist'] = preg_replace("/[^0-9]/", " ", $form['productlist']); while (strstr($form['productlist'], ' ')) $form['productlist'] = str_replace(' ', ' ', $form['productlist']); $form['productlist'] = trim($form['productlist']); // // If the product list is not empty, then we split it out into an array at the spaces. // Then we sanitize to ensure leading zeros etc are removed. // if (strlen($form['productlist']) > 0) { $product_ids = explode(" ", $form['productlist']); } foreach ($product_ids as $key=>$val) { $product_ids[$key] = (int)$val; } $form['productlist'] = implode(' ', $product_ids); // // If we have product IDs, then we need to get the necessary product information from // the database. We store the product information from the database into an array for // reference in the output. if (count($product_ids) > 0) { $sql = "SELECT p.products_id, p.products_status, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id FROM " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd WHERE p.products_id IN ('".implode("','", $product_ids)."') AND pd.products_id = p.products_id AND pd.language_id = " . (int)$_SESSION['languages_id']; $results = $db->Execute($sql); while (!$results->EOF) { $product['id'.$results->fields['products_id']] = $results->fields; $results->MoveNext(); } unset($sql, $results); // // Get the output template information from the file and store the data for // reference. // $fhandle = fopen($email_template_file, "r"); $content = fread($fhandle, filesize($email_template_file)); fclose($fhandle); $lines = preg_split("/[\r\n]/", $content); if (count($lines) > 0) { foreach ($lines as $line) { if (preg_match("/\\$\\$(\S+)/", $line, $matches)) { $section = strtolower($matches[1]); if (!isset($emailtemp[$section])) $emailtemp[$section] = ''; } elseif ($section != '') { $emailtemp[$section] .= $line."\n"; } } } unset($content, $line, $lines, $section); } $edit = $form; unset($form, $action); } ?> <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> <html <?php echo HTML_PARAMS; ?>> <head> <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>"> <title>Email Generator V1.00</title> <link rel="stylesheet" type="text/css" href="includes/stylesheet.css"> <link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS"> <script language="javascript" src="includes/menu.js"></script> <script language="javascript" src="includes/general.js"></script> <script type="text/javascript"> <!-- function init() { cssjsmenu('navbar'); if (document.getElementById) { var kill = document.getElementById('hoverJS'); kill.disabled = true; } } // --> </script> <style type="text/css"> <!-- body { background-color: #FFFFFF; font-family: tahoma; font-size: 11px; } label { margin: 3px 0 0 0; display: block; width: 130px; float: left; padding: 3px 0 0 0; clear: both; } label.required { font-weight: bold; } table { font-size: 11px; } --> </style> </head> <body onload="init()"> <!-- header //--> <?php require(DIR_WS_INCLUDES . 'header.php'); ?> <!-- header_eof //--> <!-- body //--> <table border="0" width="100%" cellspacing="2" cellpadding="2"> <tr> <td width="<?php echo BOX_WIDTH; ?>" valign="top"> </td> <!-- body_text //--> <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="100%" valign="top"><p class="pageHeading">Email Generator V1.00</p></td> </tr> <tr> <td> <?php // // The output depends on what page is currently being displayed. // if (sizeof($emailtemp) > 0) { ?> <br> <b>Results</b><br> <?php if (count($product_ids) > 0) { ?> <table cellpadding="2" cellspacing="0"> <tr style="font-weight: bold;"><td>Product ID</td><td>Status</td><td>Name</td></tr> <?php
    $html_string = $emailtemp['header'];

    foreach ($product_ids as $product_id) {
      if (isset($product['id'.$product_id])) {
        $template_data = $product['id'.$product_id];
        $template_data['products_price'] = number_format($template_data['products_price'], 2);
        $html_string .= preg_replace("/\<\<(.*?)\>\>/e", "\$template_data['\\1']", $emailtemp['content']);

?>

<tr><td><?php echo $product_id?></td><td>Found</td><td><?php echo htmlentities($product['id'.$product_id]['products_name'])?></td></tr> <?php } else { ?> <tr><td><?php echo $product_id?></td><td>Not Found</td><td>N/A</td></tr> <?php }
    }

    $html_string .= $emailtemp['footer'];

?>

</table> <?php } ?> <br> <b>HTML Code</b><br> <form> <textarea cols="60" rows="10"> <?php echo htmlentities($html_string)?> </textarea> </form> <br> <?php } ?> Enter the product IDs in the order in which you'd like for them to appear in the email, separated by commas or spaces or anything that's not a number. <br> <form method="post" action="generate_email2.php"> <input type="hidden" name="form[action]" value="generateemail"> <label class="required" for="productlist">Product IDs</label><textarea id="productlist" name="form[productlist]" cols="30" rows="5"><?php echo (isset($edit)) ? $edit['productlist'] : '';?></textarea> <br> <br> <input type="submit" value="Generate"> </form> </td> </tr> </table></td> </tr> </table> <!-- body_eof //--> <!-- footer //--> <?php require(DIR_WS_INCLUDES . 'footer.php'); ?> <!-- footer_eof //--> <br> </body> </html> <?php

require(DIR_WS_INCLUDES . 'application_bottom.php');

//
// Functions
//

function convertHTMLArray($array)
{
// This function accepts a single-dimensional array, runs through the elements
// and converts special characters to HTML tags.

if (is_array($array)) {
    foreach ($array as $key => $val) {
        $array[$key] = htmlspecialchars(stripslashes($val));
    }
} else {
    $array = NULL;
}
return $array;

}
and
$$HEADER

<TABLE CELLPADDING="15" CELLSPACING="0"> $$CONTENT <TR> <TD VALIGN="top" WIDTH="1%"><A HREF="http://www.feelgoodwatches.com/catalog/index.php?main_page=product_info&products_id=<<products_id>>" TARGET="_blank"><IMG SRC="http://www.feelgoodwatches.com/catalog/images/<<products_image>>" BORDER="0" ALT="<<products_name>>" WIDTH="125"></A></TD> <TD VALIGN="top" WIDTH="99%"><FONT COLOR="#000000" FACE="Arial" SIZE=2><A HREF="http://www.feelgoodwatches.com/catalog/index.php?main_page=product_info&products_id=<<products_id>>" TARGET="_blank"><FONT COLOR="#6868C9" FACE="Arial" SIZE=3><B><<products_name>></B></FONT></A><BR> <<products_description>> <BR><FONT COLOR="#FF0000" FACE="Arial" SIZE=3><B>Price:</B> <B>$<<products_price>></B></FONT> </FONT></TD> </TR> <TR> <TD COLSPAN="2"><HR WIDTH="100%" SIZE="1" COLOR="#7777D8" NOSHADE></TD> </TR> $$FOOTER </TABLE> ```

If it works for you, please show your appreciation here: https://www.zen-cart.com/donate

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

9 Aug 2012, 12:15 AM
#13
smithdp1 avatar

smithdp1

New Zenner

Join Date:
Feb 2011
Posts:
33
Plugin Contributions:
0

Re: Creating a product html generator for emails

DrBytes I just got back to this. Thank you so much for the help and donation coming to zen team for sure!