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
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++; 
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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