PHP Bulletin Board Home
News About Home
Features of phpBB Test drive phpBB Downloads Support for phpBB The phpBB Community Styles for customising phpBB 3rd party modifications to phpBB

Support Home | Knowledge Base Home | Submit Article | Search Articles | Browse Articles
 Formatting Database output into rows and columns 
Description: How to get the template to make cellular rather than linear output
Author: Xore
Date: Sat Jul 26, 2003 3:38 am
Type: HowTo
Keywords: database, output, rows, columns, cells
Category: MODifications
Basically, this is just a small How-To on how to get your database outputting in a table/cellular format rather than in one long vertical set of rows or one long horizontal set of columns

The key here is the modulus, without it, you can't get it to work

First of all, in your php file, you need to specify how many cells you want per row in your table.

Code:

$columns_per_row = 3;  // or whatever you want


Then, you can go ahead and read off the database, with some code similar to this:
Code:

// create and execute our database query....
$sql = "SELECT *
        FROM " . USERS_TABLE . "
        WHERE user_level = " . ADMIN . " OR user_level = " . MOD;
if ( !($result = $db->sql_query($sql))
{
  message_die(GENERAL_ERROR, "Could not query user table.", '', __LINE__, __FILE__, $sql);
}

// now here comes the interesting stuff
$i = 0;
while ( $row = $db->sql_fetchrow($result) )
{


so far so good.

This is the interesting part. on our first iteration
$i = 0,
so $i % $columns is also 0
and thus !0 = 1 (or true)

so, on our first iteration, we create a new row.
It's important that your code actually catches this on the first iteration, or you'll end up with a lot of problems

Code:

  if ( !($i % $columns_per_row) ) // modulus kicks butt
  {
    $template->assign_block_vars('row',array());
  }

so, on our first, and every (3rd, in this case) time we go through this loop, it will create a new row

and, for every iteration, we create a cell, that gets put into the current row
Code:

  $template->assign_block_vars('row.cell', array(
                                                  'CONTENT' => $row['username']
                                                  )
                             );

  $i++
}

all done the php now!

Now, we just need to display it in our template

This isn't particularly hard to do:
Code:
<table>
<!-- BEGIN row -->
 <tr>
  <!-- BEGIN cell -->
   <td>{row.cell.CONTENT}</td>
  <!-- END cell -->
 </tr>
<!-- END row -->
</table>


And now we're done

for a slightly different look on the template, try this:
Code:
<!-- BEGIN row -->
<table>
 <tr>
  <!-- BEGIN cell -->
   <td>{row.cell.CONTENT}</td>
  <!-- END cell -->
 </tr>
</table>
<!-- END row -->


this will make the items in the last row expand to fill the entire row, so you won't get empty columns.

Username: Password:
News | Features | Demo | Downloads | Support | Community | Styles | Mods | Links | Merchandise | About | Home
 © Copyright 2002 The phpBB Group.