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 parsing an assoc array 2D

 
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 -> General
View previous topic :: View next topic  
Author Message
virgil
Smarty n00b


Joined: 16 Jul 2003
Posts: 3

PostPosted: Wed Jul 16, 2003 4:45 pm    Post subject: html_table parsing an assoc array 2D Reply with quote

I'm squizing my brains out for this one.

I have this array with 16 iterations:
Code:

while($row = $result->fetchrow(DB_FETCHMODE_ASSOC )){
      $dvd[] = array( "id" => $row["id"],
                              "name" => $row["name"],
                              "email" => $row["email"]
                              );
      }
 

And I want to parse this array into a table with 4 cols using html_table like:
Code:
 
{html_table cols=4 loop=$dvd} //This is not working
 

Please show me the light!
_________________
Virgil Iancu
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
messju
Administrator


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

PostPosted: Wed Jul 16, 2003 5:19 pm    Post subject: Reply with quote

to dump an array with html_table the array-elements have to be strings (one for each cell). your array-elements are arrays.

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
volkris
Smarty Rookie


Joined: 08 Jul 2003
Posts: 11

PostPosted: Wed Jul 16, 2003 6:25 pm    Post subject: Reply with quote

Create an extension, mine's called html_table_byrows, and include the following code. Call it with loop=$records rows=$rows cols=$cols, where $cols and $rows are optional specifying the maximum number of columns and rows. If neither are specified it will display the number of columns as the longest record and all rows passed.

$records is an array of rows, which are themselves arrays of fields, meaning that if you pass it the array you posted it would do exactly what you want. Get it?

Code:
   $table_attr = 'border="1"';
   $tr_attr = '';
   $td_attr = '';
   $trailpad = '&';
   
   extract($params);

    if (!isset($loop)) {
        $smarty->trigger_error("html_table_byrows: missing 'loop' parameter");
        return;
   }
   
   $output  = "<table $table_attr>\n";

   //If rows is specified, cut out all extra rows
   if (isset($rows)) { $loop = array_slice($loop,0,$rows); }
   
   //If cols not specified, find most columns
   if (!isset($cols)) {
      $cols = 0;
      foreach($loop as $row) {
         if (count($row) > $cols)  $cols = count($row);
      }
   }
   
   foreach($loop as $row) {
      //Pad row to the length of the longest
      while(count($row) < $cols) {
         $row[] = $trailpad;
      }
      //Then go
      $output .= "<tr $tr_attr>\n";
      foreach($row as $cell) {
         if (empty($cell)) $cell = $trailpad;
         $output .= "<td $td_attr> $cell </td>";
      }
      $output .= "</tr>";
   }
   
   $output .= "</table>\n";
   
   return $output;
Back to top
View user's profile Send private message Send e-mail
ZeroFill
Smarty Rookie


Joined: 31 Aug 2003
Posts: 28

PostPosted: Fri Sep 05, 2003 11:52 pm    Post subject: Reply with quote

i'm having a little trouble getting your extention to work
template
Code:
<pre><{$url|@print_r}>
<{html_table_byrows loop=$url}>

output
Code:

Array
(
    [form_save_form] => ?&action=save_confirm
    [link_edit_info] => ?&action=edit_info
    [form_edit_info_check] => ?&action=edit_info_check
    [form_edit_info_confirm] => ?&action=edit_info_confirm
)
1

Warning:  Invalid argument supplied for foreach() in /home/union/Smarty/libs/custom/function.html_table_byrows.php on line 40

Warning:  Invalid argument supplied for foreach() in /home/union/Smarty/libs/custom/function.html_table_byrows.php on line 40

Warning:  Invalid argument supplied for foreach() in /home/union/Smarty/libs/custom/function.html_table_byrows.php on line 40

Warning:  Invalid argument supplied for foreach() in /home/union/Smarty/libs/custom/function.html_table_byrows.php on line 40

lines 31-46
[php:1:89a0b5f157]
foreach($loop as $row)
{
//Pad row to the length of the longest
while(count($row) < $cols)
{
$row[] = $trailpad;
}
//Then go
$output .= "<tr $tr_attr>\n";
foreach($row as $cell) // line 40
{
if (empty($cell)) $cell = $trailpad;
$output .= "<td $td_attr> $cell </td>";
}
$output .= "</tr>";
}
[/php:1:89a0b5f157]
_________________
my delimiters are <{ and }>
Back to top
View user's profile Send private message
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 -> General 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