Jump to content

[SOLVED] PHP Table With CSS Style Help


stormx

Recommended Posts

Let's get started, I want to be able to put a style on my PHP style but have no knowledge of what to do when it comes to every second <tr>

 

If you can't understand me, what I want to do is for every first <tr> it will display <tr class="first"> and for every second <tr> I want it to display <tr class="second">

 

Here is my code so far:

 

<?php
$sql1 = mysql_query("SELECT * FROM `history` WHERE `user` = '$user1'") or die("Cannot select usage at this time.");
$sql1_rows = mysql_num_rows($sql1);
if($sql1_rows == "0") {
echo 'No Usage Found';
} else {
echo '<table width="100%" border="0" cellpadding="5" cellspacing="1" class="shared">
<tbody>
<tr class="titles">
<th>Date</th>
<th>Disk Usage</th>
<th>Disk Limit</th>
<th>Bandwidth Usage</th>
<th>Bandwidth Limit</th>
</tr>';
while($invoices = mysql_fetch_array($sql1)) {
echo '<tr class="first">
<td align="left">'.$invoices['date'].'</td>
<td align="left">'.$invoices['disk_usage'].'</td>
<td align="left">'.$invoices['disk_limit'].'</td>
<td align="left">'.$invoices['bandwidth_usage'].'</td>
<td align="left">'.$invoices['bandwidth_limit'].'</td>

</tr>';
}
echo '</table>';
}
?>

 

So every second time this is echo'd I need it to be something like:

 

<tr class="second">
<td align="left">'.$invoices['date'].'</td>
<td align="left">'.$invoices['disk_usage'].'</td>
<td align="left">'.$invoices['disk_limit'].'</td>
<td align="left">'.$invoices['bandwidth_usage'].'</td>
<td align="left">'.$invoices['bandwidth_limit'].'</td>

</tr>

 

Thanksyou :)

Link to comment
Share on other sites

Like this

 

<?php
$sql1 = mysql_query("SELECT * FROM `history` WHERE `user` = '$user1'") or die("Cannot select usage at this time.");
$sql1_rows = mysql_num_rows($sql1);
if($sql1_rows == "0") {
echo 'No Usage Found';
} else {
echo '<table width="100%" border="0" cellpadding="5" cellspacing="1" class="shared">
<tbody>
<tr class="titles">
<th>Date</th>
<th>Disk Usage</th>
<th>Disk Limit</th>
<th>Bandwidth Usage</th>
<th>Bandwidth Limit</th>
</tr>';

// Define first befor loop
$row = "1";
while($invoices = mysql_fetch_array($sql1)) {
echo '<tr ';

//in your loop
if($row==1){echo "class=\"first\"";$row="2";}else{echo "class=\"second\"";$row="1";}

echo '>
<td align="left">'.$invoices['date'].'</td>
<td align="left">'.$invoices['disk_usage'].'</td>
<td align="left">'.$invoices['disk_limit'].'</td>
<td align="left">'.$invoices['bandwidth_usage'].'</td>
<td align="left">'.$invoices['bandwidth_limit'].'</td>

</tr>';
}
echo '</table>';
}
?>

Link to comment
Share on other sites

Like this

 

<?php
$sql1 = mysql_query("SELECT * FROM `history` WHERE `user` = '$user1'") or die("Cannot select usage at this time.");
$sql1_rows = mysql_num_rows($sql1);
if($sql1_rows == "0") {
echo 'No Usage Found';
} else {
echo '<table width="100%" border="0" cellpadding="5" cellspacing="1" class="shared">
<tbody>
<tr class="titles">
<th>Date</th>
<th>Disk Usage</th>
<th>Disk Limit</th>
<th>Bandwidth Usage</th>
<th>Bandwidth Limit</th>
</tr>';

// Define first befor loop
$row = "1";
while($invoices = mysql_fetch_array($sql1)) {
echo '<tr ';

//in your loop
if($row==1){echo "class=\"first\"";$row="2";}else{echo "class=\"second\"";$row="1";}

echo '>
<td align="left">'.$invoices['date'].'</td>
<td align="left">'.$invoices['disk_usage'].'</td>
<td align="left">'.$invoices['disk_limit'].'</td>
<td align="left">'.$invoices['bandwidth_usage'].'</td>
<td align="left">'.$invoices['bandwidth_limit'].'</td>

</tr>';
}
echo '</table>';
}
?>

 

Thankyou so much, works like a treat.

Link to comment
Share on other sites

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.