Page 1 of 1
Loops to output columns
Posted: Wed Jan 04, 2006 1:05 pm
by dallasx
I almost feel dumb for asking this but what is the logic that would let me output X number of table columns. I have data that if I were to output normally would be about 3 pages long. So I guess I could say that I am trying to shorten it.
Posted: Wed Jan 04, 2006 1:19 pm
by Charles256
Code: Select all
$i=0;
while ($i<10)
{
echo "<td></td>";
}
// tada.we have outputted 10 table columns
Posted: Wed Jan 04, 2006 1:46 pm
by dallasx
Charles256 wrote:Code: Select all
$i=0;
while ($i<10)
{
echo "<td></td>";
}
// tada.we have outputted 10 table columns
And a $i++ in there too. hehe
Now, this is the part where I get to a standoff with myself. Where would this go?
Code: Select all
while($row=mssql_fetch_array($result)
{
echo $variable 1 ." - ". $variable2;
}
Posted: Wed Jan 04, 2006 1:57 pm
by s.dot
Code: Select all
$x = 10 // number of table columns
$i = 1 // start counter
// proceed with the loop
while($array = mysql_fetch_assoc($result))
{
// check if counter is = to number of table columns
if($i == $x)
{
echo "</tr><tr>";
$i = 0; // reset counter
}
echo "<td>{$array['field']}</td>";
// increment counter
$i++;
}
Posted: Wed Jan 04, 2006 2:24 pm
by dallasx
Thanks scrotaye! With a little tweakin, I got it to work.
Code: Select all
$x = 10 // number of table columns
$i = 1 // start counter
// proceed with the loop
while($array = mysql_fetch_assoc($result)
{
// check if counter is = to number of table columns
if($i == $x)
{
echo "</tr><tr>";
$i = 0; // CHANGE: I had to reset the counter.
}
echo "<td>{$array['field']}</td>";
// increment counter
$i++;
}
Posted: Wed Jan 04, 2006 2:36 pm
by s.dot
ah, yes
I fixed it in my post in case someone else reads this and uses that code

thanks
Posted: Wed Jan 04, 2006 3:00 pm
by John Cartwright
Note: that code will only produce an ending </tr> if there count is equal in denominations..
Posted: Wed Jan 04, 2006 3:25 pm
by pilau
Jcart wrote:Note: that code will only produce an ending </tr> if there count is equal in denominations..
So you could just manually add one.
Posted: Wed Jan 04, 2006 3:51 pm
by feyd
one of the first few links in the Useful Posts thread talks about how to do this with arbitrary numbers of columns..