Below is a demonstration of how I created variable amount of columns in a table listing. In this example I was creating a picture gallery and using template files, so you can see even more than the dynamic columns, but anyway…
The only bit of important info here are these two snippets of code:
//Every x iteration based on GALLERYLISTCOLS, when 0, start new row
if (($c % GALLERYLISTCOLS)==0) {
$sdelim = $gll_startdelim;
} else {
$sdelim = ‘ ‘;
}
//Now, when x iteration is x – 1, then end the row (GALLERYLISTCOLS – 1)
if (($c % GALLERYLISTCOLS)==(GALLERYLISTCOLS – 1)) {
$edelim = $gll_enddelim;
} else {
$edelim = ‘ ‘;
}
It is using the modulus operator. And when we reach every x amount of columns, specified by GALLERYLISTCOLS global var, we make the $sdelim and/or the $edelim = the template row delimiters. (<tr> or </tr>) This makes the new row. If we set GALLERYLISTCOLS = 4, then we make 4 columns.
There are a lot of variable coming from external sources, so you’ll have to figure it out from there, but at least you can get the idea. (and more importantly, I can view this and remember how the hell I did it!)
—————————————
$gll_start= readFromFile(TEMPLATEDIR.’/gll_start.tpl’);
$gll_startdelim= readFromFile(TEMPLATEDIR.’/gll_startdelim.tpl’);
$gll_item= readFromFile(TEMPLATEDIR.’/gll_item.tpl’);
$gll_enddelim= readFromFile(TEMPLATEDIR.’/gll_enddelim.tpl’);
$gll_end= readFromFile(TEMPLATEDIR.’/gll_end.tpl’);
//////////////////////////////////
///Â Show the main gallery
$gallinks = “”;
$webgallery=WEBGALLERY;
$webgallerythumbs=WEBGALLERYTHUMBS;
require_once ‘DB.php’;
$db =& DB::connect(“sqlite:///$sdb”);
if (DB::isError($db)) {
die($db->toString());
}
$sql = “SELECT imgid, imgname,imgthumb,imgcategory,category,imgcomment FROM vw_gallery where category = ‘$gallery'”;
$res = $db->query($sql);
if (PEAR::isError($res)) {
die($res->getMessage());
} else {
$c = 0;
$rc = $res->numRows();
$parsedresult = ”;
while ($res->fetchInto($row)) {
$imgid = $row[‘0’];
$imgname = $row[‘1’];
$imgthumb = $row[‘2’];
$imgcomment = $row[‘5’];
$imgcomment = str_replace(‘\\”‘, ‘”‘, $imgcomment);
$imgcomment = str_replace(“\\'”, “‘”, $imgcomment);
$thispage = “$me?imgid=$imgid”;
/*
Here we want every x amount of iterations.
Ex. If GALLERYLISTCOLS = 3, then every third item.
$c is the current iteration of records. 1,2,3,4,…
So, when the modulus of $c and 3 is equal to 0, then
we have a group of three and should start a new row.
Modulus meaning, 3 goes into $c with x left over.
If current rowcount is 17, then 3 mod 17 = 5 with 2 left over.
Soon as we hit 18, 3 goes into it 6 times, with 0 left over,
making it another group of three.
To get start of col amount we to (($c % GALLERYLISTCOLS) == 0).
To get the end of the col we need one less than GALLERYLISTCOLS.
And we have (($c % GALLERYLISTCOLS) == (GALLERYLISTCOLS – 1)).
(the max leftover, else it would be 0)
*/
//Every x iteration based on GALLERYLISTCOLS, when 0, start new row
if (($c % GALLERYLISTCOLS)==0) {
$sdelim = $gll_startdelim;
} else {
$sdelim = ‘ ‘;
}
//Now, when x iteration is x – 1, then end the row (GALLERYLISTCOLS – 1)
if (($c % GALLERYLISTCOLS)==(GALLERYLISTCOLS – 1)) {
$edelim = $gll_enddelim;
} else {
$edelim = ‘ ‘;
}
$parsed = $sdelim . $gll_item;
$parsed = tplParser(‘{gll_thumbsrc}’,$webgallerythumbs . ‘/’ . $imgthumb,$parsed);
$parsed = tplParser(‘{gll_comment}’,$imgcomment,$parsed);
$parsed = tplParser(‘{gll_imageurl}’,$thispage,$parsed);
$parsed = tplParser(‘{gll_imagename}’,$imgname,$parsed);
$parsedresult = $parsedresult . $parsed . $edelim;
$c++;
}
echo($gll_start.$parsedresult.$gll_end);
}