Jump to content

table row colour


witham

Recommended Posts

I am hoping someone can point me in the right direction, I have a php script that adds a row to an sql database but I would like to change the background colour of the added row:

 

<?

//check the $ProdName variable to see if it has data if not send the user back to the entry page
// this has to be done before any output

$ProdName = $_GET['ProdName'];
$ProdEquiv = $_GET['ProdEquiv'];
$Manufacturer = $_GET['Manufacturer'];
$ProdNar = $_GET['ProdNar'];
$Category = $_GET['Category'];


if (empty($ProdName)){  
header("Location: addprod.php"); 
die ("opps");  
}

if (empty($ProdEquiv)){  
header("Location: addprod.php"); 
die ("opps");  
}


$dbuser  = 'user';		// your database server user name
$dbhost  = 'localhost';		// name of the sql server
$dbpass  = '';		// your database server password
$dbname  = 'ace;		// the name of the database to connect to 

// use a compare statement to check for duplicates

$sqlCOMPARE	= "SELECT * from prodname where '$ProdName' = proddesc;";


// the sql query to insert data into the prodname table


$sqlINSERT	= "INSERT into prodname values
	  (null, '$Manufacturer', '$Category', '$ProdName', '$ProdNar', '$ProdEquiv');";

$sqlINSERT	= strtoupper($sqlINSERT);


//use to display the desired data $sqlSELECT

$sqlSELECT	= "SELECT MANNAME, prodtype, proddesc, prodnar, prodequiv 
	FROM man, produse, prodname where manno = manid
	and manid = '$Manufacturer'
	and useid = '$Category' 		   
	and prodid = useid
                		and manno = MANID
	and useid = USEID
	order by MANNAME;";

// mysql_connect connects to the database server and returns a link to the the resource
$dblink = @mysql_connect("$dbhost","$dbuser","$dbpass") 
or die("<p><b>Could not connect to database server: ($dbhost)</b></p>\n");

// mysql_select_db selects a database to use on the database server pointers to by $dblink
// the @ sign before the command supresses any error messages
@mysql_select_db ($dbname , $dblink)
or die ("<p><b>Could not connect to database ($dbname)</b></p>\n");


// now execute the next query to determine if a duplicate has been entered and use
// an if statement to return the user to the entry page if this is true


$result  = mysql_query($sqlCOMPARE, $dblink)
or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");


if (  mysql_num_rows($result) > 0  ){  
header("Location: alreadyin.php");  
die ("opps");  

}

// now execute the next query to update the database table


$result  = mysql_query($sqlINSERT, $dblink)
or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");



// now execute the next query to display these results

$result  = mysql_query($sqlSELECT, $dblink)
or die("<p>Error Processing Query</p><hr /><p>".mysql_error()."</p>\n");

?>



<style type="text/css">
tr {color: white; font-family: arial; font-weight: bold;}
body {
background: navy;
background-image: url();
background-color: #333333;
}
.style2 {font-weight: bold; font-family: "arial", "Tempus Sans ITC", "Trebuchet MS";}
.style3 {font-family: Arial, Helvetica, sans-serif}
body,td,th {
color: #999999;
font-family: arial, Tempus Sans ITC, Trebuchet MS;
}
a:link {
color: #666666;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: white;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: none;
}
a {
font-family: arial, Times New Roman, Times, serif;
color: #FFFFFF;
}
h1,h2,h3,h4,h5,h6 {
font-family: arial;
}
.style5 {font-family: arial, "Times New Roman", Times, serif}
.style7 {font-family: "arial", "Tempus Sans ITC", "Trebuchet MS"}
</style>


<head>
<title>W.E.L.D.</title>
</head>


<!-- set the css format for the page-->


<body>

<?

// output the table and the first row headings

echo'<table align = "center" border=0>' . "\n";
echo'<tr><td colspan = 3 align = "center"><table width="100" border="0" cellspacing="10" cellpadding="10">
  <tr>
    <th scope="row"><img src="car.jpg" width="150" height="150"></th>
    <td><img src="gears.jpg" width="150" height="150"></td>
    <td><img src="wheel.jpg" width="150" height="150"></td>
    <td><img src="metalworking.jpg" width="150" height="150"></td>
    </tr>
</table></td></tr>';



echo '<table align = "center" border="1" cellspacing="2" cellpadding="2">' . "\n";
echo "<tr bgcolor = #000000><td>MANUFACTURER</td><td>PRODUCT TYPE</td><td>PRODUCT NAME</td><td>PRODUCT DESCRIPTION</td><td>PRODUCT EQUIVALENT</td></tr>\n";


// mysql_fetch_array fetches the results from the query a row at a time each time it's called
// the result is returned as an array that can be referenced either by field name or by it's index


while ($row = mysql_fetch_array ($result)) 


// loop through the rows outputing them as html table rows

   {



// $row["fieldname"] returns the content for the field in the current row


echo "<tr><td>" . $row["MANNAME"]. 	"</td>";
   	echo "<td>" 	. $row["prodtype"]. 	"</td>";
   	echo "<td>" 	. $row["proddesc"]. 	"</td>";
echo "<td>"	. $row["prodnar"].	"</td>";
   	echo "<td>QUALUBE " 	. $row["prodequiv"]. 	"</td></tr>";


   }


// close html table tag
echo "</table>\n";

// the mysql_free_result command removes any resources relating to the query results
// this happens automatically at the end of the script but still better to free up now
mysql_free_result ($result);

// the mysql_close command severs the link to the database, with scripts that make multiple
// queries on the same database the command only needs to be done once after all queries are completed
@mysql_close ($dblink)     
or die( "<p><b>Error while closing connection to database server:" . 
        "($dbhost)</b></p>");

?>

 

It is a while since my php college course and I would really appreciate some help

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/92330-table-row-colour/
Share on other sites

You only have to edit the HTML to change the background colour, as you don't want/need to edit the database entry.

 


echo "<tr><td>" . $row["MANNAME"]. 	"</td>";
   	echo "<td>" 	. $row["prodtype"]. 	"</td>";
   	echo "<td>" 	. $row["proddesc"]. 	"</td>";
echo "<td>"	. $row["prodnar"].	"</td>";
   	echo "<td>QUALUBE " 	. $row["prodequiv"]. 	"</td></tr>";

 

I assume it's there you're wanting to change the background colour, so you could change it to this:

 


echo "<tr><td style=\"background-color: #C0C0C0\">" . $row["MANNAME"]. 	"</td>";
   	echo "<td style=\"background-color: #C0C0C0\">" 	. $row["prodtype"]. 	"</td>";
   	echo "<td style=\"background-color: #C0C0C0\">" 	. $row["proddesc"]. 	"</td>";
echo "<td style=\"background-color: #C0C0C0\">"	. $row["prodnar"].	"</td>";
   	echo "<td>QUALUBE " 	. $row["prodequiv"]. 	"</td></tr>";

Link to comment
https://forums.phpfreaks.com/topic/92330-table-row-colour/#findComment-473196
Share on other sites

Create a boolean variable before the loop, and in the while loop NOT it so it flips on every iteration.  Use that true/false value to change the content of a variable, say $colour, and then use that in the TR tag to set the background colour (using CSS).  Here's a quick example.

 

<?php
$rowState=false;
while ($row = mysql_fetch_array ($result))
// loop through the rows outputing them as html table rows
   {
$colour=($rowState=!$rowState) ? "#FF0000" : "#00FF00";
// $row["fieldname"] returns the content for the field in the current row
echo "<tr style=\"background-color: $colour;\"><td>" . $row["MANNAME"]. 	"</td>";
   	echo "<td>" 	. $row["prodtype"]. 	"</td>";
   	echo "<td>" 	. $row["proddesc"]. 	"</td>";
echo "<td>"	. $row["prodnar"].	"</td>";
   	echo "<td>QUALUBE " 	. $row["prodequiv"]. 	"</td></tr>";
   }
?>

 

I'd suggest using CSS classes instead of changing the background-color directly, just for ease of changing it later.

Link to comment
https://forums.phpfreaks.com/topic/92330-table-row-colour/#findComment-473812
Share on other sites

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.