By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,715 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,715 IT Pros & Developers. It's quick & easy.

multiple rows and alternate color

P: n/a
Below I found a code to make multiple colums from the output of a DB, how
can I incorporate alternat colors to the multiple row snippet?

<?php
//set the number of columns
$columns = 2;

mysql_connect('localhost','','');
mysql_select_db('test');
$query = "SELECT stuff FROM mystuff ORDER BY stuff";
$result = mysql_query($query);

//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\">\n";

//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
$row = mysql_fetch_array($result);
if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
echo "<TR>\n";
}
echo "<TD>" . $row['stuff'] . "</TD>\n";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
//if there is a remainder of 1, end the row
//or if there is nothing left in our result set, end the row
echo "</TR>\n";
}
}
echo "</TABLE>\n";
?>
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
aznFETISH wrote:
Below I found a code to make multiple colums from the output of a DB, how
can I incorporate alternat colors to the multiple row snippet?

<?php
//set the number of columns
$columns = 2;

....

This is a really tedious way of doing things. Have a look at
HTML_Table package from PEAR. Alternating row colors is just one line
into your code. Something like this (given that $t is a HTML_Table):

....
$light = array('bgcolor' => '#CCCCCC');
$dark = array('bgcolor' => '#AAAAAA');
$t->altRowAttributes(1,$light,$dark,TRUE);
....

/Marcin
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.