-2

I have to code a Newsletter shipping hub in PHP. The DB architecture (which I can't change) is built like this:

Table: <customer_prefix>_data: Contains the data as XML Strings (with data such as customer name, email address, etc.) and the link to the tpl-Table. Each Row holds one customer email, plus further content which can change from newsletter to newsletter

Table: <customer_prefix>_tpl: Contains all the Newsletters (the HTML) with {$var}-Variables which should represent the XML-Nodes (and their data, so <node>value</node> should become {$node} and this variable should hold 'value'). Plus the XSD Schema, with which the XML-Strings should be validated (I got that working).

The problem is, that the XML Strings can differ from Newsletter to Newsletter and everything has to happen dynamically (since I have to code a Cronjob that gathers all data, renders the mail content and then sends it). I've been told to use Smarty, but I dont know how to (DYNAMICALLY) convert the XML Nodes to Smarty Variables...

If there's a better way than using Smarty (str_replace is not an option), I'm all ears...

I hope you get my problem... If you need any further information, I'll be glad to provide them.

hakre
  • 193,403
  • 52
  • 435
  • 836
Patrick Manser
  • 1,133
  • 2
  • 12
  • 31
  • 1
    I get your problem however it's not clear why you're not able to solve it. Stackoverflow is not a programming problem site, but a programming question site, so what is your concrete programming question? Where is your sample data? Where is your sample code that demonstrates a concrete issue? – hakre Jul 03 '13 at 03:27
  • 1
    See http://php.net/book.simplexml and [Parsing and processing HTML/XML?](http://stackoverflow.com/q/3577641/367456) – hakre Jul 03 '13 at 03:29
  • 1
    This might also contain some pointers: [XML parsing using but Element Names are Dynamic](http://stackoverflow.com/q/13221155/367456) – hakre Jul 03 '13 at 04:01
  • Thanks for your inputs, I figured it out by myself... I just didn't see it anymore yesterday, I was already coding for 11 hours straight – Patrick Manser Jul 03 '13 at 08:52
  • 1
    Then please take a short moment and answer your own question leaving a code-example there. – hakre Jul 03 '13 at 18:26
  • I've added the snippet of how it worked for me :) – Patrick Manser Jul 08 '13 at 06:30
  • 1
    please only as answer, not as the edit of the question. – hakre Jul 08 '13 at 06:36
  • Okay, thanks. I added the answer after editing the post and didn't think of editing the post again :) – Patrick Manser Jul 08 '13 at 07:02
  • 1
    Yes, thought so, just was leaving a note to show how it's intended and done more correctly. As you've seen I just edited for you alreay, so really just for explanation / next time ;) – hakre Jul 08 '13 at 07:28

1 Answers1

1

I got it figured out like this:

<?php 

// foreach loop is started here to count through a number of database entries
// that explains the $i. Just adjust that to your needs

// Get DOMDocument here and validate with XSD Schema

// ...

// Get XML Elements and assign them to Smarty variables
$elements[$key][$i] = $dom->documentElement;
foreach ($elements[$key] as $element) {
  foreach ($element->childNodes as $el) {
    $tpl->assign($el->nodeName, $el->nodeValue);
    $params[$el->nodeName] = $el->nodeValue;
  }
}

?>

That's how it worked for me

Patrick Manser
  • 1,133
  • 2
  • 12
  • 31