alexcmm Posted November 18, 2006 Share Posted November 18, 2006 What's the easiest way to get alternating row colors? My code is very simple and I'm not sure if I can throw it in with ease or if I'll have some major overhauling to do... here's my code now. Thanks for your help.[b]<?include 'dbinfo.php';MYSQL_CONNECT(localhost, $username, $password) OR DIE("DB connection unavailable");@mysql_select_db( "$database") or die( "Unable to select database"); $query="SELECT * FROM $usertable ".stripslashes($mainreport)." ".stripslashes($sortby)."";$result=mysql_query($query);$num=mysql_numrows($result);$i=0;while ($i < $num) {$id=mysql_result($result,$i,"id");$UserName=mysql_result($result,$i,"UserName");$AccessLevel=mysql_result($result,$i,"AccessLevel");$status=mysql_result($result,$i,"status");$attending=mysql_result($result,$i,"attending");$prefix=mysql_result($result,$i,"prefix");$firstname=mysql_result($result,$i,"firstname");$initial=mysql_result($result,$i,"initial");$lastname=mysql_result($result,$i,"lastname");echo ?>[/b] Link to comment https://forums.phpfreaks.com/topic/27706-resolved-alternating-row-colors/ Share on other sites More sharing options...
rotwyla98 Posted November 18, 2006 Share Posted November 18, 2006 You can check if the ID is odd/even with % (mod)if($id%2==0){//display this color}else{//display this color} Link to comment https://forums.phpfreaks.com/topic/27706-resolved-alternating-row-colors/#findComment-126729 Share on other sites More sharing options...
taith Posted November 18, 2006 Share Posted November 18, 2006 aah... i remember making my pages like that... untill i found the wonder of mysql_fetch_array();[code]<?include 'dbinfo.php';MYSQL_CONNECT(localhost, $username, $password) OR DIE("DB connection unavailable");@mysql_select_db( "$database") or die( "Unable to select database"); $query="SELECT * FROM $usertable ".stripslashes($mainreport)." ".stripslashes($sortby)."";$result=mysql_query($query);while($row=mysql_fetch_array){ echo $row[id]; echo $row[UserName]; echo $row[AccessLevel]; echo $row[status]; echo $row[attending]; echo $row[prefix]; echo $row[firstname]; echo $row[initial]; echo $row[lastname];} ?>[/code]thats much faster, and easier on your database... also causes less problems in the long run :-) Link to comment https://forums.phpfreaks.com/topic/27706-resolved-alternating-row-colors/#findComment-126735 Share on other sites More sharing options...
roopurt18 Posted November 18, 2006 Share Posted November 18, 2006 [code]<?php$Colors = Array( 'cccccc', 'cdcdcd' );$Count = 0;while( $Count < 100 ){ echo $Colors[$Count++ % 2]; // Alternates colors}?>[/code] Link to comment https://forums.phpfreaks.com/topic/27706-resolved-alternating-row-colors/#findComment-126742 Share on other sites More sharing options...
Nicklas Posted November 18, 2006 Share Posted November 18, 2006 Or[code=php:0]$color = ($color == "#cccccc") ? "#eeeeee" : "#cccccc"; // Alternates colors[/code] Link to comment https://forums.phpfreaks.com/topic/27706-resolved-alternating-row-colors/#findComment-126795 Share on other sites More sharing options...
roopurt18 Posted November 18, 2006 Share Posted November 18, 2006 Even better! Link to comment https://forums.phpfreaks.com/topic/27706-resolved-alternating-row-colors/#findComment-126814 Share on other sites More sharing options...
alexcmm Posted November 19, 2006 Author Share Posted November 19, 2006 Thanks all! Link to comment https://forums.phpfreaks.com/topic/27706-resolved-alternating-row-colors/#findComment-126987 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.