How to Loop Through an Array in Clip-Bucket with {section}{/section}

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

Looking for quality ClipBucket hosting? Try out Arvixe and you won’t be disappointed.
Tags: , , , , , , , , , , | Posted under ClipBucket | RSS 2.0

Author Spotlight

Richi González

Richi González

I started programming when I was 12. I'm a Developer and have been working with the Clip-Bucket script since it came out. I became a Liaison between Arvixe and Clip-Bucket, so I am here to assist you with anything you need regarding Clip-Bucket or any particular computer-related subject you need. Best Regards, Richi

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + = 9

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>