HTML/Javascript

SES by business degree online promotion team.

Philippine Airlines International Flight schedules

Philippine Airlines International Flight schedules
Philippine Airlines International Flight schedules

Search This Blog

Tuesday, September 21, 2010

Display/Query data from myql database and create alternate background color using FOR LOOP

code to paste:

$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("personalinfo", $con);

// Alternate background color of the table rows//
function row_color($x){
$background_a = "#4169E1";
$background_b = "#A0522D";

if ( $x%2 ) {
return $background_a;
} else {
return $background_b;
}
}

//connection to the database
$connection = mysql_connect("localhost","root","htciton");
$db = mysql_select_db("personalinfo");

$sql = "select * from table1"; // locate table
$result = mysql_query("$sql"); // display the data output from mysql database table1

// create table
echo "<table bgcolor=#FFFFFF border=0 cellpadding=3 cellspacing=3 align=center>\n";
echo "<tr bgcolor='#4169E1'>";
echo "<td align='center'><b>IDNO</b></td>";
echo "<td align='center'><b>LASTNAME</b></td>";
echo "<td align='center'><b>FIRSTNAME</b></td>";
echo "<td align='center'><b>AGE</b></td>";
echo "<td align='center'><b>SEX</b></td>";
echo "<td align='center'><b>CIVIL STATUS</b></td>";
echo "<td align='center'><b>ADDRESS</b></td>";
echo "</tr>";
// call the fields
for( $x = 0; $x < $row = mysql_fetch_array($result); $x ){
echo "<TR>\n"
."<TD bgcolor=".row_color($x).">".$row["idno"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["lastname"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["firstname"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["age"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["sex"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["civilstatus"]."</TD>\n"
."<TD bgcolor=".row_color($x).">".$row["address"]."</TD>\n"
."</TR>";
}
echo "</TABLE>";
mysql_close($con);
?>

output:

No comments:

Post a Comment