Well I'm pretty green at this whole scripting business, but results so far have been pretty good.
I came up with the following simple technique out of necessity, and thought I'd share it, see what y'all think and if you already use it.
Here's my scenario- apply the concept to whatever you want:
I needed to generate a string of table column headings for an SQL SELECT statement.
My columns were named q1, q2, q3, q4... since you can't name columns simply as integers.
Anyway, in order to generate the string, I obviously needed to employ a loop, but what I figured out was that if I defined the variable name for the string as equal to itself plus the next entry in the series, it would progressively grow in length over the course of the loop's execution.
Like this:
for($i = 0; $i < 20; $i++)
{
$colq = $colq.' '.'q'.($i+1).',';
};
This results in $colq = q1, q2, q3, q4,... The only issue being an extra comma needing to be trimmed off the last column name; so all I did then was this:
$colsq = rtrim($colq,',');
Bingo bango.
I seem to use this a lot now. Maybe you can too. Or maybe it's all completely unnecessary cause there's a much easier way I'm unaware of. Who knows? Enjoy, and please let me know what you think.
-panamade