Im building a control panel on my website to cover a bunch of stuff for my website. It makes viewing each member and video, website etc alot easier. But im building a table for my members. And i have it layed out pretty good. but there's one issue. When you look across the coloum you can end up blocking the wrong user our changing the wrong password.

So i want to change each row with a diffrent color. (only 2 colors) that switch back and forth every time it gets echo'ed out. how would i do this??

$useryquery = "SELECT * FROM  `members` ORDER BY id DESC ";
$useryquery = mysql_query($useryquery);
	while($row = mysql_fetch_assoc($useryquery)){
	$id = $row['id'];
	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$verified = $row['eamailactivated'];
	$userinfo = $row['Blocked'];
	$usertype = $row['Usertype'];
	if($usertype == '1'){
	$usertype = "<td colspan='2' align='center'>Unavailable.</td>";
	}else{
	$usertype = "<td width='50'><center><a href='index.php?block=$id'>Block</a></center></td><td width='60'><center><a href='index.php?unblock=$id'>Unblock</center></td>";
	}
	if ($userinfo == '0'){
	$userinfo = "<font color='green'>Active</font>";
	}else{
	$userinfo = "<font color='red'>Blocked</font>";
	}
	if($verified == '0'){
	$verified = "<font color='red'>Unverified</font>";
	}else{
	$verified = "<font color='green'>Verified</font>";
	}
	$name = "$firstname $lastname";
	echo "<tr><td><a href='http://www.daparadise.com/profile.php?id=$id'>$name</a></td>
	<td width='80' align='center'>$userinfo</td>
	<td width='80' align='center'>$verified</td>
	<td width='130'><center><a href='index.php?cp=$id'>Change Password</a></center></td>$usertype</tr>";
	
}

try this

<?php

$useryquery = "SELECT * FROM  `members` ORDER BY id DESC ";
$useryquery = mysql_query($useryquery);
$i=0; // added start count
	while($row = mysql_fetch_assoc($useryquery)){
	$id = $row['id'];
	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$verified = $row['eamailactivated'];
	$userinfo = $row['Blocked'];
	$usertype = $row['Usertype'];
	if($usertype == '1'){
	$usertype = "<td colspan='2' align='center'>Unavailable.</td>";
	}else{
	$usertype = "<td width='50'><center><a href='index.php?block=$id'>Block</a></center></td><td width='60'><center><a href='index.php?unblock=$id'>Unblock</center></td>";
	}
	if ($userinfo == '0'){
	$userinfo = "<font color='green'>Active</font>";
	}else{
	$userinfo = "<font color='red'>Blocked</font>";
	}
	if($verified == '0'){
	$verified = "<font color='red'>Unverified</font>";
	}else{
	$verified = "<font color='green'>Verified</font>";
	}
	$name = "$firstname $lastname";
	
	if($i%2==0){
		echo "<tr style='background-color:red;'>"; // sample color red, replace it with the color of your choice, you use hex number
	}else{
		echo "<tr style='background-color:blue;'>"; // sample color blue, replace it with the color of your choice, you use hex number
	}
	echo "<td><a href='http://www.daparadise.com/profile.php?id=$id'>$name</a></td>
	<td width='80' align='center'>$userinfo</td>
	<td width='80' align='center'>$verified</td>
	<td width='130'><center><a href='index.php?cp=$id'>Change Password</a></center></td>$usertype</tr>";
	
	$i++; // increment by 1
	
}


?>
commented: Useful Post +7

Use CSS - for example set the table rows to white with black text and change the odd rows to a balck background with white text (modify to your needs, desired colours)

tbody tr { background: white; color: black;
tbody tr:nth-of-type(odd), .odd { background: black; color: white; }

This doesn't work in IE 8 or below.

Thanks for your help. Means a lot. Sorry i couldn't replay quicker been busy with school lol.

try this

<?php

$useryquery = "SELECT * FROM  `members` ORDER BY id DESC ";
$useryquery = mysql_query($useryquery);
$i=0; // added start count
	while($row = mysql_fetch_assoc($useryquery)){
	$id = $row['id'];
	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$verified = $row['eamailactivated'];
	$userinfo = $row['Blocked'];
	$usertype = $row['Usertype'];
	if($usertype == '1'){
	$usertype = "<td colspan='2' align='center'>Unavailable.</td>";
	}else{
	$usertype = "<td width='50'><center><a href='index.php?block=$id'>Block</a></center></td><td width='60'><center><a href='index.php?unblock=$id'>Unblock</center></td>";
	}
	if ($userinfo == '0'){
	$userinfo = "<font color='green'>Active</font>";
	}else{
	$userinfo = "<font color='red'>Blocked</font>";
	}
	if($verified == '0'){
	$verified = "<font color='red'>Unverified</font>";
	}else{
	$verified = "<font color='green'>Verified</font>";
	}
	$name = "$firstname $lastname";
	
	if($i%2==0){
		echo "<tr style='background-color:red;'>"; // sample color red, replace it with the color of your choice, you use hex number
	}else{
		echo "<tr style='background-color:blue;'>"; // sample color blue, replace it with the color of your choice, you use hex number
	}
	echo "<td><a href='http://www.daparadise.com/profile.php?id=$id'>$name</a></td>
	<td width='80' align='center'>$userinfo</td>
	<td width='80' align='center'>$verified</td>
	<td width='130'><center><a href='index.php?cp=$id'>Change Password</a></center></td>$usertype</tr>";
	
	$i++; // increment by 1
	
}


?>

The PHP example works but I find that

if ($i%2) {}

works and you do not have to test if it equals zero. Not that it really matters as it adds only 3 characters to the code so there's really no bloat. But for anyone else that looks into how to do this I want to present other possibilities. Although it's technically the same possibility mostly hah

I, personally, would enter a class parameter in the TR and define it by style sheet rather than explicitly assign the style within the TR. Although it doesn't matter so much as if you plan to make a change you have to make the same amount of changes either way you go because you're technically defining the same number of possibilities. But I like to keep my code consistent so I always rely on keeping everything controlled by my stylesheets when possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.