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
https://forums.phpfreaks.com/topic/144850-solved-php-table-with-css-style-help/
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>';
}
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.