It’s a long question but not too complicated so please bear with me.
I’m building a WordPress site.
One of the pages is supposed to show a list of shops.
There are 5 fields which I’m supposed to populate with shops I’m getting from the DB.
For now I’m doing it like this (in the page templates):
There’s a javascript ajax request that works once the html is loaded and that asks for the PHP code on the server’s side to get the shops from the DB.
When they’re returned, the javascript then populates the html fields with those shops.
Now, I know this is wrong performance wise.
I should populate the fields on the server side before the html is sent to the client.
So my question is this:
There’s no sense in accessing the DB 5 times (for the 5 shops and fields).
I just want to access it once, get all the 5 shops, and populate the fields.
But how do I use the returned array from the DB in 5 different places in the html?
In this example I’m getting all the shops into one field. How do I pass them to the rest of the fields?
<tr><td><input id="shop1" type="textbox" value="<?php global $wpdb; echo $wpdb->get_results("SELECT * FROM shops"); ?>"></td></tr>
<tr><td><input id="shop2" type="textbox" value=""></td></tr>
<tr><td><input id="shop3" type="textbox" value=""></td></tr>
<tr><td><input id="shop4" type="textbox" value=""></td></tr>
<tr><td><input id="shop5" type="textbox" value=""></td></tr>
In case there is no HTML between shops:
And if there is, something like this: