Second question - Variables in styles based upon database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brik
Forum Newbie
Posts: 15
Joined: Tue Jan 30, 2007 12:48 pm

Second question - Variables in styles based upon database

Post by Brik »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Below is my code so far. What I am attempting to do is to draw a table and populate it with the contents of a mySQL query from a view. Several columns of the view will drive formatting of rows in the table and ultimately how the table itself draws. Lets just start with shading one row based upon the value of one column.

My query contains a column called $sedonly its populated with yes and no (1,0). I want to shade the corresponding row in the output blue or red based upon the 1 or 0 in the retrieved row.

I learned how to color a table thanks to a response to my post http://forums.devnetwork.net/viewtopic. ... 428#415428

I'm not a programmer and am learning so please go easy on me. Any references to examples or documentation of what I am trying to do would be great!

Code: Select all

<?php
include 'config.php';
include 'opendb.php';

$query  = "select *	from output_all";
$result = mysql_query($query);

$result = mysql_query($query) or die('Error, query failed');

// print info in table
echo '<table border="1"><tr>
					<td><b>Top Level</td>
					<td><b>Category</td>
					<td><b>Offering Name</td>
					<td><b>Resource(s) Name(s)</td></tr>';		
			//		<td><b>sedonly</td>
			//		<td><b>classsmb</td>
			//		<td><b>classdepartmental</td>
			//		<td><b>classenterprise</td>
			//		<td><b>underevaluation</td>
			//		<td><b>dropped</td></tr>';

while(list( $toplevel,
						$category,
						$offeringsname, 
						$resnames, 
						$sedonly, 
						$classsmb, 
						$classdepartmental, 
						$classenterprise, 
						$underevaluation, 
						$dropped ) = mysql_fetch_array($result))
{
echo "<tr>  <td style=\"background-color:blue\" >$toplevel</td> 
             <td>$category</td>                    
             <td>$offeringsname</td>              
             <td>$resnames</td></tr>"; 
}
echo '</table>';
echo '<br>';    	
include 'closedb.php';
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

//Snip
{
$color = ($sedonly==1)? 'red' : 'blue'; // If $sedonly is 1 set to red, otherwise blue
echo "<tr>  <td style=\"background-color:$color\" >$toplevel</td>
             <td>$category</td>                   
             <td>$offeringsname</td>             
             <td>$resnames</td></tr>";
} 
Brik
Forum Newbie
Posts: 15
Joined: Tue Jan 30, 2007 12:48 pm

Post by Brik »

WAY COOL, easy fast!!! Fast reply! Many thanks!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Brik wrote:WAY COOL, easy fast!!! Fast reply! Many thanks!
Isn't PHP just so AWESOME?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply