I’m getting some database results from a database in a SQL array using PHP.
I could potentially just create the HTML design code inside PHP for each result but this is very messy.
Is there a way to pass the array results back to EE in the template? This way I can just code the regular HTML I need in the template (outside the PHP tags) and use the results?
The problem is that array uses while to loop the results. And I don’t actually know how many results I will get, it can be different depending on the query but I need to create a more complex HTML table for each result.
I would ideally prefer to design this on the EE template on its own and then just fill the results. Using echo to output each HTML line gets messy very quickly.
This is what I did and seems to work, but I’m not entirely sure if this is the best way since is more a hack approach.
In my PHP SQL query, I echo an embed template file for EE. I then pass the PHP $var I obtained from the SQL query to this template as an embed. In this new template file I can then get the data as EE embed tags.
I can now use the SQL data in HTML in my code completely outside of PHP. This new file does not even have to run PHP anymore, and I can use completely native EE tags and use my SQL data obtained from the previous file.
It works, but Ideally I would like to use the data directly in the same template file instead of that hack.
Creative solution.
The “EE way” to handle this would be to use template tags and a custom add-on. So you’d have something like this in your template:
{exp:vw00addon:get_my_data}
{name}
{title}
{data1}
{/exp:vw00addon:get_my_data}
Then in your addon:
public function get_my_data() {
//PHP to get data from database
// Then assign results to array that matches tag names (i.e. name, title, data1 from example above)
// use ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $data) to replace data in template
}
For a much better explanation see the docs here: https://docs.expressionengine.com/latest/development/legacy/libraries/template.html#parsing-variables
Hi, but creating an add-on might seem even more work (I never created one before but plan to learn it in some future to maximize my EE skills…).
I actually found my solution by mistake and just playing around. It does work and I can basically just use {embed:mytag} as a placeholder for each PHP variable directly in HTML in the template.
It basically works like this:
EE template has PHP code which runs anything you want, in my case SQL queries.
PHP code makes the query, puts data into PHP variable or array
Same PHP code then makes an echo which outputs an EE line that embeds another EE template file and passes the variables as EE embed tags in the same line.
I can now just use vanilla HTML on that new template file and use the data without having to mess with HTML in PHP.
Ideally I would like to make the same in same 1 template instead of using another one and not sure how it affects performance, but it does work.
As an extra benefit you can elso include partial templates inside your PHP code to make things nicer. Example, if your SQL or PHP defaults to an error, instead of just using an echo to display the error you can just insert an EE partial code with a nice HTML error or success message.
This allows me to keep code separated from data and presentation.
This is what I love about EE, its more than just a CMS, you can actually get quite create and build a software or application around it.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.