Wah is quite long too huh title of this article above ... For those of you who are still confused with the above title mean, just look at the picture below alone ... for details.

In the picture above you can see that each row of the table alternating color. Alternating colors can be easier for people to look at the data, particularly for distinguishing between lines. Well ... have understood it to mean?
OK ... this article will discuss how to make the display as above, with the data read from the MySQL database. Do not worry ... how to make it pretty easy anyway.
The basic idea creation data table rows with alternating colors is sufficient to use the concept of even and odd numbers. In the example above, the background color to be white lines on the odd lines, namely 1, 3, 5, ... and so on. While the gray lines given in even-numbered lines (2, 4, 6, ... etc.).
In PHP, to determine an even number is to use a modulo operation (%), if the number in modulo 2 result 0, then he is even, while if it is not equal to 0, then the odd.
For example, in this case, suppose we have a table structure
CREATE TABLE mhs ( nim varchar(10), namaMhs varchar(30), alamat varchar(30), PRIMARY KEY (nim) )
data OF Mahasiswa Table
INSERT INTO mhs VALUES ('M0197001', 'ROSIHAN ARI YUANA', 'Solo');
INSERT INTO mhs VALUES ('M0197002', 'DWI AMALIA FITRIANI', 'Kudus');
INSERT INTO mhs VALUES ('M0197003', 'FAZA FAUZAN KH.', 'Solo');
INSERT INTO mhs VALUES ('M0197004', 'NADA HASANAH', 'Solo');
INSERT INTO mhs VALUES ('M0197005', 'MUH. AHSANI TAQWIM', 'Solo');
Look this PHP script below:
<?php
mysql_connect("namaHost","namaUser","password");
mysql_select_db("namaDB");
$warnaGenap = "#CCCCCC"; // warna abu-abu
$warnaGanjil = "#FFFFFF"; // warna putih
$warnaHeading = "#FF0000"; // warna merah untuk heading tabel
$query = "SELECT * FROM mhs";
$hasil = mysql_query($query);
echo "<table border='1'>";
echo "<tr bgcolor='".$warnaHeading."'>
<td>NIM</td>
<td>Nama Mahasiswa</td>
<td>Alamat</td>
</tr>";
$counter = 1;
while($data = mysql_fetch_array($hasil))
{
// cek apakah counternya ganjil atau genap
if ($counter % 2 == 0) $warna = $warnaGenap;
else $warna = $warnaGanjil;
echo "<tr bgcolor='".$warna."'>";
echo "<td>".$data['nim']."</td>";
echo "<td>".$data['namaMhs']."</td>";
echo "<td>".$data['alamat']."</td>";
echo "</tr>";
$counter++; // menambah counter
}
echo "</table>";
?>
No comments:
Post a Comment