+ Reply to Thread
Results 1 to 3 of 3

Thread: Smarty - Part 3 - Creating tables and lists

  1. #1
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Smarty - Part 3 - Creating tables and lists

    Content
    This tutorial will be in several parts:

    Part one, Installation and Setup
    Part two, Smartify your Forms
    Part three, Creating tables and lists
    ... more to come

    This Part
    Finally, I have the drive to write this third part of the Smarty tutorial. First, I'd say that Version 3.0 of Smarty is coming soon in a beta release, so a complete reworked Smarty will probably come in a while, more info on the Smarty webpage.
    This part will be about how to easily create tables and/or lists with help of Smarty.

    A basic list
    You all probably know how to do a normal bullet list in HTML. With Smarty, it is as simple as it is while outputing straight from php, almost.

    Your php-code:
    Code:
    $res mysql_query("SELECT * FROM MyTable");
    while (
    $row mysql_fetch_array($res)) {
        
    $mylist[] = $row['item'];
    }
    $smarty->assign("mylist"$mylist); 
    Here, we just create a normal array of the items, and assign it to smarty.

    Smarty:
    HTML Code:
    <ul>
    {section name=myitemslist loop=$mylist}
    <li>{$mylist[myitemslist]}</li>
    {/section}
    </ul>
    This will simply create a bulleted list for you...

    Ponder that you want it reversed?
    HTML Code:
    <ul>
    {section name=myitemslist loop=$mylist step=-1}
    <li>{$mylist[myitemslist]}</li>
    {/section}
    </ul>
    there you go, the list, but backwards. if you want every second item, just do step=2, and you know the drill I assume.

    Tables
    Tables is a little more complex, as you want more than one field to be shown. but not much more complex.

    PHP:
    Code:
    $res mysql_query("SELECT * FROM MyTable");
    while (
    $row mysql_fetch_array($res)) {
        
    $mylist[$row['id']] = $row['item'];
    }
    $smarty->assign("mylist"$mylist); 
    Here is another, almost identical array, but now it's associated, not pure unsociative as the last one, therefore, we use the {foreach} instead of the {section} which is only for unassociative arrays. (mind that section can be used if you make an unassociative array of associatived arrays (nested))

    Smarty:
    HTML Code:
    <table>
    {foreach from=$mylist key=k item=i}
    <tr><td>{$k}</td><td>{$i}</td></tr>
    {/foreach}
    </table>
    Now, we have a simple table here.

    Advanced tables
    Think that you might want to give every other row a different color, which isn't unusual. This is sooo somple.

    HTML Code:
    <table>
    {foreach from=$mylist key=k item=i}
    <tr style="background-color: {cycle values="#eeeeee,#d0d0d0"}"> <td>{$k}</td><td>{$i}</td></tr>
    {/foreach}
    </table>
    That's not hard, was it? If you want to apply an different class instead, well, do it with the class names instead. if you want it to be a each three rows to be marked, do a {cycle values="#eeeeee,#eeeeee,#d0d0d0"}

    More comprahensive tables
    Well, a table with two columns is very limited, we want more columns!

    Code:
    $res mysql_query("SELECT * FROM MyTable");
    while (
    $row mysql_fetch_array($res)) {
        
    $mylist[$row['id']]['item'] = $row['item'];
        
    $mylist[$row['id']]['cost'] = $row['cost'];
        
    $mylist[$row['id']]['size'] = $row['size'];
    }
    $smarty->assign("mylist"$mylist); 
    Now, we have a little bit more, we have the id, and we have item, cost & size to show. That's not a problem.

    HTML Code:
    <table>
    {foreach from=$mylist key=k item=i}
    <tr style="background-color: {cycle values="#eeeeee,#d0d0d0"}"> <td>{$k}</td><td>{$i.item}</td><td>{$i.cost}</td><td>{$i.size}</td></tr>
    {/foreach}
    </table>
    What about empty listings then?
    At times, your queries might not have any results. then Smarty can handle that in a special way too.

    HTML Code:
    <table>
    {foreach from=$mylist key=k item=i}
    <tr style="background-color: {cycle values="#eeeeee,#d0d0d0"}"> <td>{$k}</td><td>{$i.item}</td><td>{$i.cost}</td><td>{$i.size}</td></tr>
    {foreachelse}
    <tr><td colspan="3">No items to list</td></tr>
    {/foreach}
    </table>
    Same with the section, but you use sectionelse instead then...

    Conclusion
    There are more with this if you like to, but this is at least the basics on the section and the foreach parts of Smarty. More will come about smarty in the next part, but what it will be, I don't know right now.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Smarty - Part 3 - Creating tables and lists

    Great Tutorial +rep

  4. #3
    Jordan Guest

    Re: Smarty - Part 3 - Creating tables and lists

    Well done, Orjan! I often use your other two smarty tutorials for reference. +Rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 08-28-2009, 07:32 PM
  2. Smarty - Part 1 - Installation and Setup
    By Orjan in forum PHP Tutorials
    Replies: 13
    Last Post: 08-03-2009, 09:35 AM
  3. Smarty - Part 2 - Smartify your Forms
    By Orjan in forum PHP Tutorials
    Replies: 4
    Last Post: 07-22-2009, 05:52 PM
  4. Intermediate ADTs: Stacks and Queues Part 2: Using Lists
    By WingedPanther in forum C Tutorials
    Replies: 4
    Last Post: 01-09-2009, 12:38 PM
  5. Creating Tables
    By leon in forum Database & Database Programming
    Replies: 2
    Last Post: 06-12-2006, 07:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts