Loops to output columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Loops to output columns

Post 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.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

Code: Select all

$i=0;
while ($i<10)
{
   echo "<td></td>";
}
// tada.we have outputted 10 table columns
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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;
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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++;
}
Last edited by s.dot on Wed Jan 04, 2006 2:35 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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++;
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

ah, yes

I fixed it in my post in case someone else reads this and uses that code ;) thanks
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Note: that code will only produce an ending </tr> if there count is equal in denominations..
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

one of the first few links in the Useful Posts thread talks about how to do this with arbitrary numbers of columns..
Post Reply