Helen Neely
Helen Neely

Reputation: 4740

Displaying MySQL table in CSS

I just started working with PHP and have managed to run a simple CRUD application. But displaying it seems a bit dull and looks quite ugly, so I was wondering if anyone could explain how I can select my data from a database table and make it look like this one on the W3School website?

I like the way the colours alternate and would be nice to reproduce it when my data is read and displayed on a table.

Upvotes: 0

Views: 1802

Answers (2)

Nicolae Albu
Nicolae Albu

Reputation: 1245

If your question is about alternating color rows, here it goes: you define two CSS classes for the two types of rows:

tr.row-even {
    background-color: blue;
}

tr.row-odd {
    background-color: red;
}

Then you assign the row class to each row by checking the evenness of each row's index:

<?php
$rowCount = ...; // Depends on your code
for ($i = 0; $i < $rowCount; $i++) {
    $rowClass = ($i % 2 == 0) ? 'row-even' : 'row-odd';
    echo '<tr class="' . $rowClass . '">';
    // ...
    echo '</tr>';
}
?>

Upvotes: 1

genesis
genesis

Reputation: 50976

That's not based on php but on html. PHP is server-side.

Wait, I think I did get you

$i = 0;
while($row = mysql_fetch_assoc($result))
{
    $i++;
    if ($i % 2)
    {
        echo "<tr class='yellow'><td>".$row['id']."</td></tr>";
    }
    else{
        echo "<tr class='green'><td>".$row['id']."</td></tr>";
    }
}

in CSS it will look like

.yellow{background-color:yellow;}
.green{background-color:green;}

Upvotes: 0

Related Questions