Hello,
When you dynamically need to output records from a database inside your Clip-Bucket, by this moment, you may have noticed that Clip-Bucket is powered by Smarty template engine, therefore, you cannot use a while() loop, for example, like you would normally do in native PHP. In this article we will review the use of {section}{/section} to loop through a MySQL records array.
Difficulty level: Medium (if you know how to use loops in PHP, you’ll find it easier)
1. Create the query to retrieve records from your database (in this example I’ll use queries for MySQL, but you can use any database)
$q = "SELECT * FROM yourtable";
2. Assign the result to the template with Clip-Bucket’s $db->getAll();
Assign('ourarray',$db->getAll($q));
3. Now in your HTML file, we are going to loop through the results of $ourarray
Let’s imagine our table: yourtable, has the following:
+----+-------+-------+ | id | name | views | +----+-------+-------+ | 1 | you | 123 | | 2 | other | 1 | | 5 | rob | 1 | +----+-------+-------+
4. We want to display: NAME has VIEWS views for every name
{section name=myloop loop=$ourarray} {$ourarray[myloop].name} has {$ourarray[myloop].views} <br> {/section}
The output for that code would be:
you has 123 views other has 1 views rob has 1 views
5. So we now know that the correct use to display an element from the array using section, is to use $previouslyassignedarray[nameofoursection].element ({section name=nameofoursection loop=$previouslyassignedarray}{/section})
Remember to close the section with {/section}. If you want to learn more about the other parameters that this loop can use, please refer to: http://www.smarty.net/docsv2/en/language.function.section.tpl . Any questions you may have with these, let me know and I’ll be more than glad to assist you.
Best Regards,
Richi
Owner of Juapo2Services