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

I get checkbox array and want to put it in 3 colums

 
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
lean4huskytop
Smarty Regular


Joined: 19 Aug 2003
Posts: 41

PostPosted: Mon Aug 25, 2003 12:35 am    Post subject: I get checkbox array and want to put it in 3 colums Reply with quote

I know how to display checkboxes from an array, but how can I format the output so that the checkboxes are in nice 3 or 4 columns, depending on how many checkboxes I get from the database.

thanks
Paul
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Aug 25, 2003 1:02 am    Post subject: Reply with quote

Hi Paul.

The following may not work exactly as I didn't bother to test it :), but it should give you an idea of how to approach this. I suspect that there are many ways to tackle this and I offer this only as an example.

One column is simple (using a table for simplicity):
Code:
<table><tr>
<td>
{foreach from=$list item=rec}
    {$rec.title}<br/>
{/foreach}
</td>
</tr></table>


Now lets say you want to limit columns to 15 rows. Before you pass the data to your template, you can partition it in an array of cols:
[php:1:11b5cd28ea]$maxrows = 15;
$ii = ceil(count($list)/$maxrows);
for ($i=0; $ii; $i++;) {
$cols[$i] = array_slice($list, $i*$maxrows, $maxrows);
}
$smarty->assign('col_list', $cols);[/php:1:11b5cd28ea]

You should now have an array made up of several sub-arrays, each made up of $maxrows elements. You now use two loops (nested) in your template, one for the columns, one for the rows:
Code:
<table><tr>
{foreach from=col_list item=list}
<td>
  {foreach from=$list item=rec}
      {$rec.title}<br/>
  {/foreach}
</td>
{/foreach}
</tr></table>


HTH
Back to top
View user's profile Send private message
mose
Smarty n00b


Joined: 28 Jul 2003
Posts: 2
Location: Taiwan

PostPosted: Mon Aug 25, 2003 4:42 am    Post subject: Re: I get checkbox array and want to put it in 3 colums Reply with quote

I got a smarty-only code for that, it's just good for 3 columns or few more. Not really clean, maybe : consider it as a hack.

Code:
{* variable $schema is a simple array *}
<table><tr><td valign="top">
{counter start=0 print=false assign=c}
{section name=x loop=$schema}
{counter}{$schema[x]}<br/>
{if $smarty.section.x.index eq floor($smarty.section.x.total / 3)
 or $smarty.section.x.index eq floor(2 * $smarty.section.x.total / 3)}
</td><td valign="top">
{/if}
{/section}
</td></tr></table>


mose
Back to top
View user's profile Send private message
lean4huskytop
Smarty Regular


Joined: 19 Aug 2003
Posts: 41

PostPosted: Mon Aug 25, 2003 5:26 am    Post subject: thanks, but your code is too ... Reply with quote

Hey guys..both of the code samples are good, but not what I was looking for.

I am looking for a checkbox implementation...not slicing arrays. Yes I know - you'd think that once I know how to slice an array, I can put different parts in different columns...but then if I want to have a few preselected..then......we run into other questions and problems...

I am looking for a checkbox way of doing it...not an array that is split.
Show me an example of a checkbox column interpretation of your code.

I have two arrays...one with values and one for output. I need to use them to create 4 colums of checkboxes.



thanks
Paul
Back to top
View user's profile Send private message
lean4huskytop
Smarty Regular


Joined: 19 Aug 2003
Posts: 41

PostPosted: Mon Aug 25, 2003 6:21 am    Post subject: okey guys..i got it - heres the code Reply with quote

its a sample of what I have now..its working...it splits the array into $maxrows.




Code:
$maxrows = 4;
      $ceil = ceil(count($certificationNames)/$maxrows);
      for ($i=0; $i < $ceil; $i++) {
        $certNames[$i] = array_slice($certificationNames, $i*$maxrows, $maxrows);
        $certIds[$i] = array_slice($certificationIds, $i*$maxrows, $maxrows);
      }
      $smarty->assign('col_list', $certNames);



$smarty->assign('certIds',$certIds);
$smarty->assign('certNames',$certNames);

Template code here:


Code:
<table cellpadding=3 cellspacing=2 class=smtextBig><tr>
{section name=newId loop=$certIds}
   <td>
   {html_checkboxes values=$certIds[newId] output=$certNames[newId] separator="<br />"}
   </td>
{/section}
</tr>
</table>


Thats it...it works beautifully. I LOVE IT!
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