Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

html_table question

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
ialdeboath
Smarty n00b


Joined: 25 May 2003
Posts: 2

PostPosted: Sun May 25, 2003 3:13 am    Post subject: html_table question Reply with quote

Crying or Very sad Forgive me if it's way late and I'm missing something. Just came across html_table for the first time.

My problem is that I have an array of named links (sorted alphabetically) that needs to be displayed in a table.

The html_table function however produces output with the elements like this:

A B C D
E F G H

etc

where I need it to be
A C E G
B D F H

Is there an easy way to do this with html_table? Should I do this another way? Am I just to tired to notice the obvious.

Thanks in advance. Laughing
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun May 25, 2003 2:07 pm    Post subject: Reply with quote

no, this cannot be done with the current html_table.
OTOH you have three possibilities:

1. do your own loops with section and stuff to achieve this.
(this may need {math} and become complicated and slow)

2. write your own html_table-like plugin to print your lists

3. try my modified version of function.html_table.php:
[php:1:374f050b0b]<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/


/**
* Smarty {html_table} function plugin
*
* Type: function<br>
* Name: html_table<br>
* Date: Feb 17, 2003<br>
* Purpose: make an html table from an array of data<br>
* Input:<br>
* - loop = array to loop through
* - cols = number of columns
* - rows = number of rows
* - table_attr = table attributes
* - tr_attr = table row attributes (arrays are cycled)
* - td_attr = table cell attributes (arrays are cycled)
* - trailpad = value to pad trailing cells with
* - vdir = vertical direction (default: "down", means top-to-bottom)
* - hdir = horizontal direction (default: "right", means left-to-right)
* - inner = inner loop (default "cols": print $loop line by line,
* $loop will be printed column by column otherwise)
*
*
* Examples:
* <pre>
* {table loop=$data}
* {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
* {table loop=$data cols=4 tr_attr=$colors}
* </pre>
* @author Monte Ohrt <monte@ispi.net>
* @version 1.0
* @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
* (Smarty online manual)
* @param array
* @param Smarty
* @return string
*/
function smarty_function_html_table($params, &$smarty)
{
$table_attr = 'border="1"';
$tr_attr = '';
$td_attr = '';
$cols = 3;
$rows = 3;
$trailpad = '&';
$vdir = 'down';
$hdir = 'right';
$inner = 'cols';

extract($params);

if (!isset($loop)) {
$smarty->trigger_error("html_table: missing 'loop' parameter");
return;
}

$loop_count = count($loop);
if (empty($params['rows'])) {
/* no rows specified */
$rows = ceil($loop_count/$cols);
} elseif (empty($params['cols'])) {
if (!empty($params['rows'])) {
/* no cols specified, but rows */
$cols = ceil($loop_count/$rows);
}
}

$output = "<table $table_attr>\n";

for ($r=0; $r<$rows; $r++) {
$output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
$rx = ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols;

for ($c=0; $c<$cols; $c++) {
$x = ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c;
if ($inner!='cols') {
/* shuffle x to loop over rows*/
$x = floor($x/$cols) + ($x%$cols)*$rows;
}

if ($x<$loop_count) {
$output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
} else {
$output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
}
}
$output .= "</tr>\n";
}
$output .= "</table>\n";

return $output;
}

function smarty_function_html_table_cycle($name, $var, $no) {
if(!is_array($var)) {
$ret = $var;
} else {
$ret = $var[$no % count($var)];
}

return ($ret) ? ' '.$ret : '';
}


/* vim: set expandtab: */

?>
[/php:1:374f050b0b]

{html_table loop=... inner=rows} will make the data be printed col-by-col instead of row-by-row.

i think i will update this in the cvs-version soon, but it needs a bit more testing. so if you choose "3" tell me if it works for you.

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 25, 2003 5:23 pm    Post subject: Reply with quote

Aside from messju's (I bet it works!) code sample...

I might also suggest that if the data is stored in ordinal arrays (ie. not associative) then you can transpose your input table, ie:

loop through and END[j, i] = START[i, j] ...

Not pretty, but IMO problems like these are typically rooted in the choice of data structures and the choice of techniques used to manipulate those structures. And lack of sleep Wink

Ask: Why does your input table look like that?
Back to top
View user's profile Send private message
ialdeboath
Smarty n00b


Joined: 25 May 2003
Posts: 2

PostPosted: Mon May 26, 2003 2:25 am    Post subject: Reply with quote

messju wrote:
no, this cannot be done with the current html_table.
OTOH you have three possibilities:

1. do your own loops with section and stuff to achieve this.
(this may need {math} and become complicated and slow)

2. write your own html_table-like plugin to print your lists



Thanks, I'll give it a try and get back to you. Very Happy
Back to top
View user's profile Send private message
TheMightyDude
Smarty n00b


Joined: 28 May 2003
Posts: 1
Location: London UK

PostPosted: Wed May 28, 2003 8:33 pm    Post subject: Reply with quote

Well this is also a html_table question
btw Nice Script Smile

I have had a play at creating Tables with Smarty but I dont see any examples in doing table that will look like what I have at my site which was written in PHP: http://linux.initcorp.co.uk/blacknova/setup_info.php

It needs to have tables like the following:
Row 1 = Title <=== 1 column
Row 2 = Name | Value <=== 2 columns
Row .. = Name | Value <=== 2 columns
Row 10 = Information <=== 1 column

I need to create a page like that using a Template.

Do anyone know how to do a page like that using Smarty?

Thanks in advance
Paul Kirby
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
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