Jump to content

Alternate my row colors?


chaddsuk

Recommended Posts

Hi i was wondering how i can alternate the row colors in my table?

 

ive copid and pasted my code below

 


<html><head><title>MySQL Table Viewer</title>



<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body>





<?php

if (isset($_POST["submit"])) {
//DB Variables
$user = "root"; // username
$pass = "****"; // password
$dbname = "test"; // database name
$tablename = "users"; // table name

mysql_connect ("localhost", $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname);

$query = "INSERT INTO $tablename (autoID, name, age, comments, email) VALUES ('0','".$_POST['name']."','".$_POST['age']."','".$_POST['Comments']."','".$_POST['Email']."')";

mysql_query ($query) or die(mysql_error());
// mysql_close ($dbh); You don't really need to close it as it closes automatically once the scripts finished running
}
?>

<center><form method="POST" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>">
Name:<input type="text" name="name">
Age:<input type="text" name="age">
Comments:<input type="text" name="Comments">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="submit">
</form> </center>



<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '****';

$database = 'test';
$table = 'users';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT name, age, Comments, Email FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);




echo "<center><table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>


 

 

Thanks ahead of time for your help

 

chris

 

Link to comment
https://forums.phpfreaks.com/topic/88042-alternate-my-row-colors/
Share on other sites

<?php
$color1 = "#CCFFCC"; 
$color2 = "#BFD8BC"; 
$row_count = 0;

$query = "...";
$result = mysql_query( $query );
while( $row = mysql_fetch_assoc( $result );
{
    $row_color = ( $row_count % 2 ) ? $color1 : $color2;
    echo( '<tr>' );
    echo( '<td style="background:' . $row_color . ';"></td>' );
    echo( '</tr> ');
    $row_count++; 
}
?>

thanks for that, im not sure where i should place this in my script? ive tried just placing at the top however it just gives me a blank page?

 

could you possibly edit my coe etc to show me what i would need for it to function correctly?

 

thanks again

 

chris

$bgColor1 = "#CCFFCC";
$bgColor2 = "#BFD8BC"; 
$rowCount = 0;
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    if($rowCount % 2 == 0) { $bgColor = $bgColor1 } else { $bgColor = $bgColor2; }

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td style=\"background-color: $bgColor\">$cell</td>";

    echo "</tr>\n";
    $rowCount++;
}
mysql_free_result($result);

 

There we are :)

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.