I am an old hat with CGI programming, and a greenhorn with PHP. I am bit of a purist when it comes to template engines and the seperation of logic and presentation. I do not believe that any logic should ever be in the web page and that html should never be in the logic.

Here is the purist's approach, and I believe this is equally valid with Smarty 2.50 and 2.6:

/*
* 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.
$head;
$hash;
$row;
$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);
$x++;

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

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):

Hi,
boots your example was great!! Just one question. What if there is only one row returned from the database and your $cols is 2 per column?
At the moment it just returns a blank column, which would be fine except that it returns an image. so I get broken image link. Is there anyway to display remainders with a max number of columns X? .. If that made any sense at all

table_foreach - The beauty of foreach with the power of html_table... (smarty code inside each cell as with foreach / $item and $key available in each cell / possibility of html table from array of arrays or from array of objects / possibility of nested tables... )

I would be gratefull to get some review/comment/bug checks...
Thanks,_________________Ben

Hi, I am quite new with PHP and I don't understand how PHP works with memory. These dynamic tables in SMARTY look good, but what will happen if the assigned array to loop through is realy big, for example if i have a table with 1000 rows and 15 colums? PHP has to keep all the table data in memory, doesn't it? But when I do it without SMARTY I print each row immediately as i get it from database and PHP only puts it on an output buffer or sends it to the browser. Please can you explain it to me?

@wham: yes, you'll have the full array in memory. but with 1000 rows and 15 columns i would be more concerned about the amout of the html-code that has to travel through the net to the user, than the consumed memory during creation of the page.

Hi Al, let me say I'm not a coder, so it's difficult for me understand all you are talking about.
I would like to use the HtmlTableAdvPlugin in my 2.6 smarty.
So, I cp the correctfile in the smarty plugin directory, and I modified the .tpl
[b]this is my team.tpl code:[/b]
[code]{html_table_adv loop=$data}[[name]],Hello [[name]], you're from [[city]].{/html_table_adv}[/code]
[b]My index.php page is: [/b]
...
[code]if ($Viewpeople=='no') {
$data=new Smarty;
$data = array(array('name'=>'Marc', 'city'=>'Miami'),array('name'=>'Someone', 'city'=>'Somewhere'));
$data->display('team.tpl');
}[/code]
...

The problem is that I got this error in the browser...
Fatal error: Call to a member function on a non-object in ....../index.php on line 399
[b]Let me say I need to intagrate a db call such us (in the index.php):[/b]
[code] $vis=new Smarty;
$vis->assign ('ris',$db->get_results("SELECT * FROM team WHERE People='$People' ORDER BY name" ));
$vis->display('team.tpl');[/code]

[quote="boots"]messju posted this on the IRC channel. It is an update of [url=http://smarty.php.net/contribs/examples/dynamic_table_columns/table.tpl.txt][u]Monte's tried and true solution[/u][/url] but takes advantadge of 2.6's math features to be a bit cleaner:

NOTE: "& nbsp;" above should have no space--but this forum doesn't like that[/quote]

Hi all,
step by step I'm trying to use the below code to put in a table the db data.
So. I'm quite happy because now I can view a table with something inside....the problem is that I can only view a table with the word OBJECT inside... What a low profile coder I am..
please have a look on my code...
PHP:
$data=new Smarty;
$data->assign ('data',$db->get_results("SELECT * FROM team ORDER BY name" ));
$data->display('team.tpl');
TEAM.TPL:
{* $cols is the number of columns you want *}
<table border=5>
{section name=tr loop=$data step=2}
<tr>
{section name=td start=$smarty.section.tr.index
loop=$smarty.section.tr.index+2}
<td>{$data[td]|default:"&"}</td>
{/section}
</tr>
{/section}
</table>

Hi GeXus, I disagree I have just spend a little time tring to get the orginal code to work, it appears to work correctly if you have a white background but since I put a border around my tables using css I noticed that the orginal code did not put the extra

Code:

<td>&nbsp:</td>

for some reason and I got this ugly black space,

I have done some testing.. and here is my results.

With the Orginal Code

1. If you set it to 3 cols and you have 6 results it looks great but if you have 5 you got problems..

2. if you set it to 6 cols and you have 5 results you got the black space problem. But if you set 6 cols and you have 12 results it looks great.

Now the new code that boots provided did not work at first because instead of assigning the var I just put the number 3 every where $cols was thinking it would work.. and I passed it off as not working then I went back and then I tried to new orginal code but this time I assigned the var $cols and it worked great for me..

I have set it $cols to 1 with 10 results and it worked great.. I set it to 3 with 10 results and it worked great. I did modify it a bit also.. to fit my needs

In the orginal new code boots had it to where only one vars was checked and a default was place, I just put in a {if}{else}{/if} so I knew if I checked one value in the array the rest would be there, allowing me to pass more than one value to get the output that I was looking for.

So for Smarty Verion 2.6.14 the new code from boots helped me greatly and does work and it's much cleaner, I think that was the entire point. Now that's not to say the old version does not work also, maybe there was something I missed I am not sure.. I am just letting you know what took me 2 hours to find out..