Re: Alternating Table Rows, Part Deux..
| From: | Shaun Thomas | Date: | Fri, 10 May 2002 22:11:41 +0000 |
| Subject: | Re: Alternating Table Rows, Part Deux.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-97027@lists.php.net to get a copy of this message | ||
On Fri, 10 May 2002, Glenn Sieb wrote:
> Thanks for all the hints--here's what my boss and I eventually came
> out with:
This could use a few tweaks. First off, putting mysql_num_rows in the
actual for loop executes it every iteration. Bad. Second, consider
giving your code some readability by returning an associative array.
You also duplicate your if statements for company name. You can also
rotate your column in the same IF statement to rotate the color. Try
this (I'll assume column names...):
----------------------------- CUT HERE -----------------------------
$sPrevCompany = $sColor = '';
while ($aRow = mysql_fetch_assoc($stmt))
{
$sCompanyName = $aRow['companyname'];
// If our previous company name is the same as this one, don't print
// the company name, but print the data.
if ($sPrevCompany == $sCompanyName)
{
$sColor = ($sColor == 'FF') ? '00' : 'FF';
$sCompanyName = ' '
}
// We're past our if statement, so save the previous company for
// nex time.
$sPrevCompany = $aRow['companyname'];
print
"<TR BGCOLOR=#FFFF$sColor>\n".
"\t<TD>$aRow[somecolumn]</TD>\n".
"\t<TD>$sCompanyName</TD>\n".
"\t<TD>$line[anothercolumn]</TD>\n".
"</TR>\n";
}
----------------------------- CUT HERE -----------------------------
And done. Infinitely more maintainable.
--
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
| Shaun M. Thomas INN Database Administrator |
| Phone: (309) 743-0812 Fax : (309) 743-0830 |
| Email: sthomas@townnews.com AIM : trifthen |
| Web : www.townnews.com |
| |
| "Most of our lives are about proving something, either to |
| ourselves or to someone else." |
| -- Anonymous |
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+