Categories & Columns
2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 21, 2009 (RSS)
By Kenny - September 21, 2009
I am trying to combine a couple of chunks of code without much luck.
I have a list of items divided up into categories.
So using the following code I can get the name of the category to to only show up once and then list the items in that category.
But instead of going straight down the page, I want to put them into columns of three. Normally I would use this:
Now when I tried to fuse the two together I get horrible results (like Dr Frankenstein).
Any ideas?
Kenny
I have a list of items divided up into categories.
So using the following code I can get the name of the category to to only show up once and then list the items in that category.
<?php
$lastCategory = "";
foreach ($linksRecords as $record):
$showHeader = false;
if ($record['category'] != $lastCategory) {
$lastCategory = $record['category'];
$showHeader = true;
}
?>
<?php if ($showHeader): ?>
<table border="0" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td><b><font face="Arial" color="#800000"><?php echo $record['category'] ?></font></b></td>
</tr>
</table>
<?php endif ?>
<table border="0" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td><font face="Arial" size="2">
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></font><br></td>
</tr>
</table>
<?php endforeach; ?>
But instead of going straight down the page, I want to put them into columns of three. Normally I would use this:
<table>
<?php foreach ($linksRecords as $record): ?>
<td>
//DISPLAY ALL CODE HERE
</td>
<?php $maxCols=3; if (@++$count % $maxCols == 0): ?>
</tr>
<tr>
<?php endif ?>
<?php endforeach ?>
</table>
Now when I tried to fuse the two together I get horrible results (like Dr Frankenstein).
Any ideas?
Kenny
Re: [sagentic] Categories & Columns
By Chris - September 21, 2009
Hi Kenny,
At some point, it becomes simpler to restructure your data.
Try this:
Hope this helps! Please let me know if you have any questions. :)
At some point, it becomes simpler to restructure your data.
Try this:
<?php
// construct a list of categories
$categories = array();
foreach ($linksRecords as $record) {
if (@$record['category']) {
if (!@$categories[$record['category']]) {
$categories[$record['category']] = array(
'label' => $record['category'],
'records' => array()
);
}
$categories[$record['category']]['records'][] = $record;
}
}
?>
<?php foreach ($categories as $category): ?>
<!-- category header -->
<table border="0" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td><b><font face="Arial" color="#800000"><?php echo $category['label'] ?></font></b></td>
</tr>
</table>
<!-- records in this category -->
<table>
<tr>
<?php $count = 0; ?>
<?php foreach ($category['records'] as $record): ?>
<?php $maxCols=3; @$count++; if (($count-1) % $maxCols == 0 && $count != 1): ?>
<!-- next row -->
</tr><tr>
<?php endif ?>
<!-- record start -->
<td><font face="Arial" size="2">
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></font><br>
</td>
<?php endforeach; ?>
</tr>
</table>
<?php endforeach; ?>
Hope this helps! Please let me know if you have any questions. :)
All the best,
Chris
Chris