RE: [PHP] Alternating table rows...
> I've been asked if I can alternate the colors of the > rows to make the report more legible. What I usually do is something like this: if ($colour == "blue") $colour = "red" else $colour = "blue"; Then the bit where you actually output display you use $colour to set display attributes and away you go. CYA, Dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating Table Rows, Part Deux..
On 05:11 PM 5/10/2002 -0500, Shaun Thomas wrote: >This could use a few tweaks. Ok I got it now... This exhibits the behaviour I was looking for, and, thanks to you, is much easier to read and maintain! :))) Thanks, Shaun! Glenn $sPrevCompany = $sColor = ''; while ($aRow = mssql_fetch_row($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'; $sPrevCompany = $sCompanyName; } else { $sCompanyName = ' '; } /* We're past our if statement, so save the previous company for next time. */ print ( "\n". "\t$aRow[0]\n". "\t$aRow[1]\n". "\t$sCompanyName\n". "\t$aRow[2]\n". "\t$aRow[4]\n". "\t$aRow[5]\n". "\t$aRow[6]\n". "\t$aRow[7]\n". "\n"); } print (""); --- Glenn E. Sieb, System Administrator Lumeta Corp. mailto:[EMAIL PROTECTED] +1 732 357-3514 (V) +1 732 564-0731 (Fax) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating Table Rows, Part Deux..
Thanks! I'll play with this!!! :) Glenn On 05:11 PM 5/10/2002 -0500, Shaun Thomas wrote: >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...): --- Glenn E. Sieb, System Administrator Lumeta Corp. mailto:[EMAIL PROTECTED] +1 732 357-3514 (V) +1 732 564-0731 (Fax) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating Table Rows, Part Deux..
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 "\n". "\t$aRow[somecolumn]\n". "\t$sCompanyName\n". "\t$line[anothercolumn]\n". "\n"; } - CUT HERE - And done. Infinitely more maintainable. -- +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+ | Shaun M. ThomasINN Database Administrator | | Phone: (309) 743-0812 Fax : (309) 743-0830| | Email: [EMAIL PROTECTED]AIM : trifthen | | Web : www.townnews.com | | | | "Most of our lives are about proving something, either to | | ourselves or to someone else." | | -- Anonymous | +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating Table Rows, Part Deux..
"1LT John W. Holmes" wrote: > > Read the code again, that's what I first thought, too. He's just not > printing multiple values for the first table cell. So if the current row has > the same company name, just print a blank cell, then print the rest of the > data for that row. In the words of a great man: "Doh!" Cheers, Rob. -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating Table Rows, Part Deux..
- Original Message - From: "Robert Cummings" <[EMAIL PROTECTED]> > Glenn Sieb wrote: > > $cname = (strcmp($previous,$line[0]) > > ? $line[0] : " "); > > print (" > BGCOLOR=#$color>\n\t$line[2]\n\t$cname\n\t$line[3]\n\n"); > > $previous = $line[0]; > > } > > print (""); > > > > This not only takes care of the table row colors, but also removes > > duplicate company names so it looks MUCH neater this way : > > YIKES! I thought there was a SQL keyword for returning a unique > rowset. Read the code again, that's what I first thought, too. He's just not printing multiple values for the first table cell. So if the current row has the same company name, just print a blank cell, then print the rest of the data for that row. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating Table Rows, Part Deux..
Glenn Sieb wrote: > > Hey everyone! > > Thanks for all the hints--here's what my boss and I eventually came out with: > > /* ## > ## And for every row of data we pull, we create a table row... > ## */ > $company = 0; > $previous = ""; > for ($i = 0; $i < mssql_num_rows( $stmt ); ++$i) > { > $line = mssql_fetch_row($stmt); > $company += > (strcmp($previous,$line[0]) ? 1 : 0); > $color = (($company%2) ? "FF" : > "00"); > $cname = (strcmp($previous,$line[0]) > ? $line[0] : " "); > print (" >BGCOLOR=#$color>\n\t$line[2]\n\t$cname\n\t$line[3]\n\n"); > $previous = $line[0]; > } > print (""); > > This not only takes care of the table row colors, but also removes > duplicate company names so it looks MUCH neater this way : YIKES! I thought there was a SQL keyword for returning a unique rowset. Cheers, Rob. -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Alternating table rows...
Just another version. the call ... $RowColor = useColor(); ... echo ''; - Jp At 15:46 10/05/02, you wrote: >[snip] >$rowcount = 0; >while($dbrowa = mysql_fetch_object($dbseta)){ > $dbrowb = mysql_fetch_object($dbsetb); >//change the color of alternating rows >$rowcount ++; >if ($rowcount == 1){too many lines of code >[/snip] > >I get humbled by this list every day. Could I have Single PHP Lines for a >thousand Alex? Dammit, still thinking like an old Basic programmer... > >Jay > > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php Juan Pablo Aqueveque <[EMAIL PROTECTED]> Ingeniero de Sistemas Departamento de Redes y Comunicaciones http://www.drc.uct.cl Universidad Católica de Temuco. Tel:(5645) 205 630 Fax:(5645) 205 628 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Alternating table rows...
[snip] http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Alternating table rows...
Yet even more ways to do it: $i = 0; while ($row = mysql_fetch_assoc($result)) { $bgcolor = ($i++ & 1) ? '#ff' : '#00'; } Or more then two: $colors = array('black','green','blue','yellow'); $i = 0; while ($row = mysql_fetch_assoc($result)) { $bgcolor = $colors[$i++ % 4]; } The first used &, which is a bitwise operator. See also the following faqt: How can I show rows from a database in a table with alternating colors? http://www.faqts.com/knowledge_base/view.phtml/aid/783 Regards, Philip Olson On Fri, 10 May 2002, Miguel Cruz wrote: > While everyone's kicking in ideas... > > I don't think modulus (as someone else suggested) is that expensive, but I > think this is the cheapest: > > $evenodd = 0; > $color[0] = '#cc'; > $color[1] = '#99'; > while ($row = mysql_fetch_row($st)) > { > print "{$row['whatever']}"; > $evenodd = 1 - $evenodd; > } > > miguel > > On Fri, 10 May 2002, Jay Blanchard wrote: > > > [snip] > > Is there a way for me to do one of the following: > > > > 1) Test to see if $i is an even or odd number? > > 2) Grab more than one line from the database at a time, and just put in two > > table rows at once? > > [/snip] > > > > > $rowcount = 0; > > while($dbrowa = mysql_fetch_object($dbseta)){ > > $dbrowb = mysql_fetch_object($dbsetb); > > //change the color of alternating rows > > $rowcount ++; > > if ($rowcount == 1){ > > print("\n"); > > } > > elseif ($rowcount <> 1) { > > print("\n"); > > $rowcount = 0; > > } > > > > > > This way you don't have to know even or odd. > > $rowcount is 0 > > $rowcount is set to one and printed in a different color > > $rowcount is set to two, which is not equal to one, so does elseif and is > > set to zero > > > > HTH! > > > > Jay > > > > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating table rows...
Robert Cummings wrote: > > The more generic approach: > > function getNextColour( &$lastIndex, $colourList ) > { > return $colourList[(++$lastIndex % count( $colourList ))]; U... $lastIndex++, don't want to start on the second entry :) Cheers, Rob. -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating table rows...
The more generic approach: function getNextColour( &$lastIndex, $colourList ) { return $colourList[(++$lastIndex % count( $colourList ))]; } $colours = array ( '#ff', '#00ff00', '#ff' ); $index = 0; foreach( $rows as $aRow ) { echo '' .$aRow .''."\n"; } Cheers, Rob. - Jay Blanchard wrote: > > [snip] > Is there a way for me to do one of the following: > > 1) Test to see if $i is an even or odd number? > 2) Grab more than one line from the database at a time, and just put in two > table rows at once? > [/snip] > > $rowcount = 0; > while($dbrowa = mysql_fetch_object($dbseta)){ > $dbrowb = mysql_fetch_object($dbsetb); > //change the color of alternating rows > $rowcount ++; > if ($rowcount == 1){ > print("\n"); > } > elseif ($rowcount <> 1) { > print("\n"); > $rowcount = 0; > } > > This way you don't have to know even or odd. > $rowcount is 0 > $rowcount is set to one and printed in a different color > $rowcount is set to two, which is not equal to one, so does elseif and is > set to zero > > HTH! > > Jay > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- .-. | Robert Cummings | :-`. | Webdeployer - Chief PHP and Java Programmer | :--: | Mail : mailto:[EMAIL PROTECTED] | | Phone : (613) 731-4046 x.109 | :--: | Website : http://www.webmotion.com | | Fax : (613) 260-9545 | `--' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Alternating table rows...
While everyone's kicking in ideas... I don't think modulus (as someone else suggested) is that expensive, but I think this is the cheapest: $evenodd = 0; $color[0] = '#cc'; $color[1] = '#99'; while ($row = mysql_fetch_row($st)) { print "{$row['whatever']}"; $evenodd = 1 - $evenodd; } miguel On Fri, 10 May 2002, Jay Blanchard wrote: > [snip] > Is there a way for me to do one of the following: > > 1) Test to see if $i is an even or odd number? > 2) Grab more than one line from the database at a time, and just put in two > table rows at once? > [/snip] > > $rowcount = 0; > while($dbrowa = mysql_fetch_object($dbseta)){ > $dbrowb = mysql_fetch_object($dbsetb); > //change the color of alternating rows > $rowcount ++; > if ($rowcount == 1){ > print("\n"); > } > elseif ($rowcount <> 1) { > print("\n"); > $rowcount = 0; > } > > > This way you don't have to know even or odd. > $rowcount is 0 > $rowcount is set to one and printed in a different color > $rowcount is set to two, which is not equal to one, so does elseif and is > set to zero > > HTH! > > Jay > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Alternating table rows...
[snip] Is there a way for me to do one of the following: 1) Test to see if $i is an even or odd number? 2) Grab more than one line from the database at a time, and just put in two table rows at once? [/snip] \n"); } elseif ($rowcount <> 1) { print("\n"); $rowcount = 0; } This way you don't have to know even or odd. $rowcount is 0 $rowcount is set to one and printed in a different color $rowcount is set to two, which is not equal to one, so does elseif and is set to zero HTH! Jay -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating table rows...
On 02:47 AM 5/11/2002 +0800, Jason Wong wrote: > if ($i % 2) { odd; } else { even; } Great! Thanks, Jason :))) One thing I love about this list... you guys are more than helpful! :) Glenn --- Glenn E. Sieb, System Administrator Lumeta Corp. mailto:[EMAIL PROTECTED] +1 732 357-3514 (V) +1 732 564-0731 (Fax) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alternating table rows...
On Saturday 11 May 2002 02:45, Glenn Sieb wrote: > Hi everyone, > > I have a PHP script that reads data from an MS SQL server and outputs the > data into a table. I've been asked if I can alternate the colors of the > rows to make the report more legible. The relevant piece of code looks > like: > > for ($i = 0; $i < mssql_num_rows( $stmt ); ++$i) > { > $line = mssql_fetch_row($stmt); > print (" COLOR=#$color>$line[2]$line[0]$line[3]"); > } > > Is there a way for me to do one of the following: > > 1) Test to see if $i is an even or odd number? if ($i % 2) { odd; } else { even; } -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* "I once witnessed a long-winded, month-long flamewar over the use of mice vs. trackballs...It was very silly." (By Matt Welsh) */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php