[SOLVED] - Making a three column table result
Posted: Thu Mar 18, 2004 12:48 pm
I am trying to create a three column table output using an array of values taken from a MySQL database. I am using the Template class file used by phpBB to separate my PHP code and HTML. Everything works in the code but I don't think I am invoking this thing the right way. What I have so far is:
The template that this fills is:
My challenge is this: When the first column block runs, it populates the template, then moves on to the second column, then to the third. Essentially the sequence is by column. What I am looking for is a way to make this sequence run by CELL (ie, fill R1C1, then R1C2, then R1C3 THEN R2C1, then R2C2, then R2C3 and so on).
Anyone have any clue as to what I could do to make this happen? Thanks for the help and sorry for the lengthy post.
PS An HTML version of this (which I am trying to convert to PHP) is at http://www.ez1stmortgage.com/testimonials.shtml.
SOLUTION
I messed with this thing a little bit and came up with something. This might be useful if you are ever trying to output a result set into a a three column table...
And here is the template code that it fills...
Hope this can help someone.
Code: Select all
<?php
$testimony = array();
while ( $row = $db->sql_fetchrow($result) )
{
$testimony[] = $row;
}
for ($i = 0; $i < count($testimony); $i++)
{
if ( (($i % 3) == 0) )
{
// This is the left column in the table, remainder 0
$tpl->assign_block_vars('column_0', array(
'WRITER' => $testimony[$i]['Writer'],
'LOCALE' => $testimony[$i]['Locale'],
'WORDS' => $testimony[$i]['Testimony'])
);
}
elseif ( (($i % 3) == 2) )
{
//This is the second column in the table, remainder 2
$tpl->assign_block_vars('column_1', array(
'WRITER' => $testimony[$i]['Writer'],
'LOCALE' => $testimony[$i]['Locale'],
'WORDS' => $testimony[$i]['Testimony'])
);
}
else
{
//this is the third column of the table, remainder 1
$tpl->assign_block_vars('column_2', array(
'WRITER' => $testimony[$i]['Writer'],
'LOCALE' => $testimony[$i]['Locale'],
'WORDS' => $testimony[$i]['Testimony'])
);
}
}
?>Code: Select all
<p>
<table width='700' border='0'>
<!-- BEGIN column_0 -->
<tr>
<td width='220'>
{column_0.WRITER},<br />
{column_0.LOCALE}
<hr />
{column_0.WORDS}
</td>
<td width='20'>&nbsp;</td>
<!-- END column_0 -->
<!-- BEGIN column_1 -->
<td width='220'>
{column_1.WRITER},<br />
{column_1.LOCALE}
<hr />
{column_1.WORDS}
</td>
<td width='20'>&nbsp;</td>
<!-- END column_1 -->
<!-- BEGIN column_2 -->
<td width='220'>
{column_2.WRITER},<br />
{column_2.LOCALE}
<hr />
{column_2.WORDS}
</td>
</tr>
<!-- END column_2 -->
</table>
</p>Anyone have any clue as to what I could do to make this happen? Thanks for the help and sorry for the lengthy post.
PS An HTML version of this (which I am trying to convert to PHP) is at http://www.ez1stmortgage.com/testimonials.shtml.
SOLUTION
I messed with this thing a little bit and came up with something. This might be useful if you are ever trying to output a result set into a a three column table...
Code: Select all
<?php
$testimony = array();
while ( $row = $db->sql_fetchrow($result) )
{
$testimony[] = $row;
}
for ($i = 0; $i < count($testimony); $i++)
{
// All one block, just case out the beginning and end of rows
if ( (($i + 3) % 3) == 0 )
{
$begin_row = '<tr>';
$spacer = "<td width='20'> </td>";
if ( $i == (count($testimony) - 1) )
{
$end_row = "<td><!-- col_2 --> </td> " . $spacer . " <td><!-- col_3 --> </td> </tr> ";
}
else
{
$end_row = "";
}
}
elseif ( (($i + 3) % 3) == 1 )
{
$begin_row = '';
$spacer = "<td width='20'> </td>";
if ( $i == (count($testimony) - 1) )
{
$end_row = "<td><!-- col_3 2nd --> </td> </tr> ";
}
else
{
$end_row = "";
}
}
elseif ( (($i + 3) % 3) == 2 )
{
$begin_row = '';
$spacer = '';
$end_row = "</tr>";
}
$tpl->assign_block_vars('praise_table', array(
'CLASS_NAMES' => 'praise_names',
'ROW_START' => $begin_row,
'SPACER' => $spacer,
'WRITER' => $testimony[$i]['Writer'],
'LOCALE' => $testimony[$i]['Locale'],
'WORDS' => $testimony[$i]['Testimony'],
'ROW_STOP' => $end_row)
);
}
?>Code: Select all
<p>
<table width='700' border='0'>
<!-- BEGIN praise_table -->
{praise_table.ROW_START}
<td width='220' class='{praise_table.CLASS_NAMES}'>
<span class='mission'>{praise_table.WRITER},<br />
{praise_table.LOCALE}</span>
<hr />
"{praise_table.WORDS}"
</td>
{praise_table.SPACER}
{praise_table.ROW_STOP}
<!-- END praise_table -->
</table>
</p>