Skip to content
Advertisement

How to build HTML element based on MySQL results

Problem I am constructing an HTML email to send which includes products in our database.

I have all the moving parts, I believe, to complete the task but just not sure how to piece it together.

The HTML template

JavaScript

Template Tags

JavaScript

The element I want to loop

NOTE: This first element is inside the template. I can remove it if we need to, though.

JavaScript

A Simple template system But I believe this will work on only one row (right?)

JavaScript

MySQL Results

JavaScript

So…

Given all that, how would I construct the HTML to loop over the results and include that DIV as many times as there are results?

Thank you for looking.

Ps. I did research this topic on SO , and found similar questions but none quite matched my criteria. Thanks again!

Advertisement

Answer

I believe something like that would work:

JavaScript

The only thing I added is the tmp_template variable. This variable is a copy of the original template where you will replace the variable from your query into it. After replacing all the variable with their value, you will at this tmp_template variable into a html variable. This html variable will be your final result that you will send by email. You could add a header before the loop and a footer after the loop into your html variable for the email.

PLEASE NOTE: I did not test this code, but it should be very close to what you need.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement