Jump to content

add background color if Value is an odd number


kat35601
Go to solution Solved by kat35601,

Recommended Posts

I want to change the background to a light gray if the value of the $UOMPDROPSEQUENCE field is an odd number.

What would be the best way to do this please keep in mind I am very new to programming and PHP Thanks

Below is a snippet of my code.

$result =odbc_exec($connect,$sql);
if(!$result){
exit("Error in SQL");
}

echo "<table><tr>";
echo "<th>Drop</th>";
	echo "<th>Name</th>";
	echo "<th>Note</th>";
	echo "<th>PO</th>";
	echo "<th>Box_count</th>";
	echo "<th>Cubes</th>";
	echo "<th>ADDRESSLINE</th>";
	echo "<th>ADDRESS2</th>";
	echo "<th>CITY</th>";
	echo "<th>STATE</th>";
	echo "<th>ZIP</th>";
	echo "<th>Order_ID</th>";
	echo "<th> Phone </th>";
	

while (odbc_fetch_row($result)) {
	$UOMPDROPSEQUENCE=odbc_result($result,"UOMPDROPSEQUENCE");
	$cmlName=odbc_result($result,"cmlName");
	$UCMLSTOREHOURSTXT=odbc_result($result,"UCMLSTOREHOURSTXT");
	$ompCustomerPO=odbc_result($result,"ompCustomerPO");
	$UOMPTOTALBOXCOUNT=odbc_result($result,"UOMPTOTALBOXCOUNT");
	$UOMPVOLUMETOTAL=odbc_result($result,"UOMPVOLUMETOTAL");
	$CMLADDRESSLINE1=odbc_result($result,"CMLADDRESSLINE1");
	$CMLADDRESSLINE2=odbc_result($result,"CMLADDRESSLINE2");
	$CMLCITY=odbc_result($result,"CMLCITY");
	$CMLSTATE=odbc_result($result,"CMLSTATE");
	$CMLPOSTCODE=odbc_result($result,"CMLPOSTCODE");
	$ompSalesOrderID=odbc_result($result,"ompSalesOrderID");
	$cmlPhoneNumber=odbc_result($result,"cmlPhoneNumber");

  echo "<tr><td>$UOMPDROPSEQUENCE </td>";
  echo "<td align='center'>$cmlPhoneNumber</td>";
   echo "<td align='center'>$UCMLSTOREHOURSTXT</td>";
  echo "<td align='center'>$ompCustomerPO</td>";
  echo "<td align='center'>$UOMPTOTALBOXCOUNT</td>";
  echo "<td align='center'>$UOMPVOLUMETOTAL</td>";
  echo "<td align='center'>$CMLADDRESSLINE1</td>";
  echo "<td align='center'>$CMLADDRESSLINE2</td>";
  echo "<td align='center'>$CMLCITY</td>";
  echo "<td align='center'>$CMLSTATE</td>";
  echo "<td align='center'>$CMLPOSTCODE</td>";
  echo "<td align='center'>$ompSalesOrderID</td>";
echo "<td align='center'> $cmlPhoneNumber</td></tr>";
  }

odbc_close($connect);
Link to comment
Share on other sites

Well, you do have some errors in your code, but the usual solution would be to set the row's background color. 

 

Add this to your tr tag

 

<tr class='$bkgrnd'>

 

Then in your style section of your html code add two classes:

 

.color1 {background-color:white;}
.color2 {background-color:lightgray;}

 

In your loop now check the value of your data and set $bkgrnd to either 'color1' or 'color2' depending upon what you want.

 

Note: you are missing the closing row tag on your headings.  Also - you should look at the odbc_fetch_array function instead of using the odbc_result one that you are using.  Your method is very resource intensive (ie, inefficient).  Using the fetch_array function will get all your values in an array and you can use those elements in your output loop.  Check the manual for examples.

Link to comment
Share on other sites

  • Solution

This is how I colored the background of my rows based on odd or even. Not sure if it is the right way but it works great.

	{	
	{ if($UOMPDROPSEQUENCE % 2 == 0 )
               {$style = '#ffa500';}
          else {$style =  '#fffafa';}}


// Then echo 


"<tr  bgcolor=$style><th>$UOMPDROPSEQUENCE</th>";


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.