Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Dynamic Table Columns
Goto page Previous  1, 2
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Smarty Rookie

Joined: 09 Feb 2006
Posts: 6
Location: Manila

PostPosted: Tue Oct 31, 2006 1:28 am    Post subject: Re: New plugin for advanced html tables Reply with quote

This is great, you should used this plugin! Great work!
Learning Mode
Back to top
View user's profile Send private message
Smarty n00b

Joined: 22 Feb 2008
Posts: 3
Location: Philippines

PostPosted: Fri Feb 22, 2008 12:15 am    Post subject: Re: Better dynamic columns Reply with quote

thanks body
thawes wrote:
Let's say I need table headings for my columns. What to do?

If your data happens to be comming from an SQL database, you are in good shape. You create your table headings from your SQL call. Here's what it looks like:

Template file index.tpl

<table border="1">
  <tr valign="top" align="center">
  {section name=col loop=$headers}
  {section name=myrow loop=$test}
  <tr valign="top">
    {section name=mycolumn loop=$test[myrow]}

OK, now for the calling PHP. I am using Pear:Very HappyB as my example.
PHP file index.php

// create object
$smarty = new Smarty;

$dsn = "dbtype://username:password@hostname/dbname";
$db = DB::connect($dsn, true);
if (DB::isError($dsn))
die ($db->getMessage());

* Here is the SQL statement we use to specify
* our table headings.
$sql = "SELECT
first_name AS \"First name\",
last_name AS \"Last Name\",
company AS \"Company\",
address_1 AS \"Address 1\",
address_2 AS \"Address 2\",
city AS \"City\",
state AS \"State\",
zip_code AS \"Zip Code\",
tel_work AS \"Phone (Work)\",
tel_cell AS \"Cell\",
tel_fax AS \"FAX\",
email AS \"Email\",
url AS \"Website\"
FROM contacts
ORDER BY last_name";

$result = $db->query($sql);

if (DB::isError($result))
die ($result->getMessage());

// Initialize some new variables we will now use.
$x = 0;

* Important to create an associative array, as this will
* return the headers as we had specified in our SQL
* statement.
$hash = $result->fetchRow(DB_FETCHMODE_ASSOC);
$head = array_keys($hash);
$row[$x] = array_values($hash);

// Now we fetch the other rows from our query.
while ($hash = $result->fetchRow(DB_FETCHMODE_ASSOC))
$row[$x] = array_values($hash);

// Create Smarty arrays.
$smarty->assign('headers', $head);
$smarty->assign('test', $row);

// display it

The out put will look something like:
<table border="1">
<tr valign="top" align="center">
<th>First name</th>
<th>Last Name</th>
<th>Address 1</th>
<th>Address 2</th>
<th>Zip Code</th>
<th>Phone (Work)</th>
<tr valign="top">
... the rest of your data.

It were not for Smarty, I would not even consider PHP.

[url=""]Affordable Web design and Development [/url] | [url=""]Adult Blogs [/url] | [url=""]Web Design Tutorial [/url]
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Smarty Rookie

Joined: 03 Jan 2008
Posts: 15

PostPosted: Sat Sep 27, 2008 5:50 pm    Post subject: columns using smarty Reply with quote

Yep that worked for me!

{assign var='cols' value=0}
{section name="outer" loop=$gallerylist}
{assign var="cols" value=$cols+1}{if $cols eq 2} <br clear=all>
{assign var='cols' value=0} {/if}

z0ink wrote:
I'm new to smarty. I liked the idea of aliasing the column names and using that as the table headers, but it would have broken my assosciative system already in use. Here is how I have my headers setup to support changing the order of the data (ascending, descending):


// get sort id (column) and order_id(asc/desc) -- stripped of error checking
$_GET['s'] = $sort_id;
$_GET['o'] = $order_id;

// create sort and order arrays -- these are used inside a sql query and below
$sort = array('product_id', 'name', 'price', 'description', 'stock');
$order = array('ASC', 'DESC');

// create header -- array(array([name] => 'product_id', [order] => 0), ...)
for($i = 0; $i < 5; $i++) {
  // look for the column i'm currently sorting by
  if($i == $sort_id) {
    $head[$i]['name'] = $sort[$i];
    // if the column i'm sorting is 0, ASC, then change DESC to the next option
    if($order_id == 0) {
      $head[$i]['order'] = 1;
    // if it's 1, DESC, the if will fail and it will become 0, ASC
    } else $head[$i]['order'] = 0;
  // assign a default of 0, ASC, for the items not being used to sort
  } else {
    $head[$i]['name'] = $sort[$i];
    $head[$i]['order'] = 0;

$smarty->assign('head', $head);


{assign var="i" value="0}
{section name=row loop=$head}
<th><a href="foobar.php?s={$i}&o={$head[row].order}">{$head[row].name}</a></th>
{assign var="i" value=$i+1}

It needs some work and probably isn't the easiest to understand, but its a quick and dirty way of doing it. I don't know how effecient it is, but it's a start.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page Previous  1, 2
Page 2 of 2

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP