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=''>$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>";
	
}

Dani AI

Generated

A quick summary and a few practical improvements that build on the thread:

noted the problem (mistaking which row’s action is being clicked). ’s server-side counter and ’s CSS nth-child are both valid approaches; ’s suggestion to prefer stylesheet classes over inline styles is the best maintainable practice. Use server-side class assignment for maximum browser compatibility (IE8 and older need it), and keep all visual rules in CSS so colors can be changed without touching PHP.

Server-side class toggle (keeps markup simple and works everywhere):

$rowIndex = 0;
while ($row = mysqli_fetch_assoc($result)) {
  $rowClass = ($rowIndex++ & 1) ? 'row-odd' : 'row-even';
  echo '<tr class="'.$rowClass.'"> ... </tr>';
}

Example stylesheet (pick accessible colors and test contrast):

.row-even { background:#ffffff; color:#111111; }
.row-odd  { background:#f4f4f8; color:#111111; }
tr:focus-within { outline:2px solid #6ca0ff; } /* keyboard focus */

Prevent wrong-user actions and hard-to-reverse clicks:

  • Put actions in a POST form per row with a hidden id and action name so the server receives an explicit intent. Example pattern:
    <form method="post" action="admin-actions.php">
    <input type="hidden" name="member_id" value="<?php echo htmlspecialchars($id); ?>">
    <button name="action" value="block">Block</button>
    <button name="action" value="unblock">Unblock</button>
    </form>
  • Always validate on the server (check permissions, confirm the id matches a real user) and use CSRF tokens.
  • Escape output with htmlspecialchars and use mysqli or PDO with prepared statements instead of deprecated mysql_* functions.

Troubleshooting: if styles don’t apply, check selector specificity, ensure the stylesheet loads after any theme CSS, and verify table rows actually have the class (view source).

Recommended Answers

All 4 Replies

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=''>$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=''>$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.