Jump to content

Recommended Posts

Notice how the rowheading has its own class and so does row1 and row2 well what I want to do if I have like 10 or so rows. I want it to have the first result returned with row2 as stated in the code and then the second returned result to have row1 class and then after that it'll alternate the next would be row2 and then 4th would be row1 and so on. How do I tell it to alternate.

 

function directory() {
$query = "SELECT * FROM users";
$result = @mysql_query ($query);// Run The Query
if ($result) {
print'<h1 class=backstage>Active Handler Directory</h1><br />';
print'<table width="100%" class="table1">';
print'<tr class="rowheading">';
print'<td>Name</td>';
print'<td>Forum Name</td>';
print'<td>Characters</td>';
print'</tr>';
// Fetch and print all records.
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
print'<tr class=row2>';
print'<td valign=top>Admin 1 (site admin)</td>';
print'<td valign=top>Forum Name </td>';
print'<td valign=top><ul class=characters><li>Character Name</li>';
print'</ul></td>';
print'</tr>';
print'<tr class=row1>';
print'<td valign=top>User 1</td>';
print'<td valign=top>Forum Name </td>';
print'<td valign=top><ul class=characters><li>Character Name</li>';
print'</ul></td>';
print'</tr>';
print'</table><br />';
print'<h2 class=backstage><form method=POST><input type=hidden name=action value=mainmenu><input type=submit value="Return to Main Menu" class=button200></form></h2>';
}  

Link to comment
https://forums.phpfreaks.com/topic/138659-solved-alternating-classes/
Share on other sites

You want to use modulus.

 

$i=0;
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$class = ($i%2==0)?'row1':'row2';

print'<tr class=' . $class . '>';
print'<td valign=top>Admin 1 (site admin)</td>';
print'<td valign=top>Forum Name </td>';
print'<td valign=top><ul class=characters><li>Character Name</li>';
print'</ul></td>';
print'</tr>';
print'<tr class=' . $class . '>';
print'<td valign=top>User 1</td>';
print'<td valign=top>Forum Name </td>';
print'<td valign=top><ul class=characters><li>Character Name</li>';
print'</ul></td>';
print'</tr>';
print'</table><br />';
print'<h2 class=backstage><form method=POST><input type=hidden name=action value=mainmenu><input type=submit value="Return to Main Menu" class=button200></form></h2>';
$i++;
}  

 

I am unsure why you displaying a table each loop but that is the idea behind it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.