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

adding columns=N option to html_checkboxes

 
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
pcg
Smarty n00b


Joined: 25 Aug 2003
Posts: 1

PostPosted: Mon Aug 25, 2003 8:50 pm    Post subject: adding columns=N option to html_checkboxes Reply with quote

I found it useful, maybe others would too. It's really simplistic, but gets the job done.

Code:

--- function.html_checkboxes.php.old    Mon Aug 25 16:39:34 2003
+++ function.html_checkboxes.php        Mon Aug 25 16:38:51 2003
@@ -55,6 +55,7 @@
       switch($_key) {
       case 'name':
       case 'separator':
+      case 'columns':
          $$_key = $_val;
          break;
 
@@ -97,20 +98,40 @@
    settype($selected, 'array');
    $_html_result = '';
 
+   if (isset($columns) && is_numeric($columns) && $columns > 0) {
+      $_html_result .= '<table width=100%><tr><td>';
+      $col = 0;
+   } else {
+      $columns = 0;
+   }
+
    if (is_array($options)) {
 
-      foreach ($options as $_key=>$_val)
+      foreach ($options as $_key=>$_val) {
          $_html_result .= smarty_function_html_checkboxes_output($name, $_key,
$_val, $selected, $extra, $separator, $labels);
+         if ($columns && ++$col >= count($options)/$columns) {
+            $_html_result .= '</td><td>';
+            $col = 0;
+         }
+      }
 
 
    } else {
       foreach ($values as $_i=>$_key) {
          $_val = isset($output[$_i]) ? $output[$_i] : '';
          $_html_result .= smarty_function_html_checkboxes_output($name, $_key,
$_val, $selected, $extra, $separator, $labels);
+         if ($columns && ++$col >= count($values)/$columns) {
+            $_html_result .= '</td><td>';
+            $col=0;
+         }
       }
 
    }
 
+   if ($columns) {
+      $_html_result .= '</td></tr></table>';
+   }
+
    return $_html_result;
 
 }


The relevant template code:

Code:

{html_checkboxes name="title_id" options=$titles checked=$titles_checked separator="<br />" columns=2}


And the output (spaced a little more readably):

Code:

<table width=100%>
  <tr>
    <td>
      <label><input type="checkbox" name="title_id[]" value="1" checked="checked" />Title One</label><br />
      <label><input type="checkbox" name="title_id[]" value="12" />Title Twelve</label><br />
    </td>
    <td>
      <label><input type="checkbox" name="title_id[]" value="5" />Title Five</label><br />
      <label><input type="checkbox" name="title_id[]" value="6" />Title Six</label><br />
    </td>
    <td>
    </td>
  </tr>
</table>


Dunno about that last <td></td> but it's good enough for what I'm doing.

Hope this is the right place; please move it if it isn't (moderator). Thanks!
Back to top
View user's profile Send private message
einlemming
Smarty Rookie


Joined: 04 May 2004
Posts: 10
Location: Stuttgart, Germany

PostPosted: Fri Jul 02, 2004 1:46 pm    Post subject: Reply with quote

Thanks for that one!

I really missed this one in the html_checkboxes tag.
While html_options does not need a separator because it is surrounded with a html select tag, the html_checkboxes do.

Its a main disadvantage that html_checkboxes can only be separated by a separator at the recent version of Smarty. Nearly no one would ever separate html_checkboxes with a br tag for example.
Usually you use a table or a div tag in XHTML. But this is not possible with the separator tag in Smarty. So it becomes almost impossible to insert html_checkboxes in virtually every design template.

Imagine you want to have 50 checkboxes in an ordinary form. If you separate them with a br, you'll probably get a very long list instead of ordering them in parallel Wink
_________________
See ya, Lemming
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jul 02, 2004 1:59 pm    Post subject: Reply with quote

The problem with this design is that the html_checkbox is generating HTML tags that you have no control over.

How about this idea:

{html_checkboxes ... assign="cbox"}

Then {$cbox} would contain the output of html_checkboxes as an array. From there, you can feed it into {html_table} or use it in a section loop.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jul 02, 2004 2:15 pm    Post subject: Reply with quote

Give CVS a try, html_checkboxes and html_radios now have an assign attribute which will dump the output into a template variable as an array.
Back to top
View user's profile Send private message Visit poster's website
einlemming
Smarty Rookie


Joined: 04 May 2004
Posts: 10
Location: Stuttgart, Germany

PostPosted: Mon Jul 05, 2004 9:29 am    Post subject: Reply with quote

Thanks, this works of course, too.
Do you have any clue, if the html_checkboxes tag is ever adjusted to work with html tables like described above?
_________________
See ya, Lemming
Back to top
View user's profile Send private message 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