Highligh HTML Table Header

Hi guys.

I have the following function, which extracts data from mysql and puts it in an html table.

[php]function getPosts(){
$query = mysql_query(“SELECT * FROM myPosts”) or die(mysql_error());

if(mysql_num_rows($query)==0){
	echo'<tr><td colspan="\3\">NO POSTS FOUND!</td></tr>';
	} else	{

	while($posts = mysql_fetch_assoc($query)){
		echo ';
		echo "<tr><td>".$posts['title']."</td> <td>".$posts['author'] . "</td>   <td>".$posts['linklabel'] . "</td>  <td> <a href=\"delete.php?id=".$posts['id']."\">Delete</a> &nbsp;&nbsp;  <a href=\"editPosts.php?id=".$posts['id']."\"> Edit </a></td>";
	}[/php]

I am trying to highlight to the first row of the table, i.e, the header, which is called “title”. Ideally, I would like to high like every other row with a different color as well, but at this point I am interested in highlighting the html header

. I have tried numerous ways but I always end up highlighting the entire column

I am trying to achieve the same effect as Tables # 1, and/or 2-3 in this link http://www.textfixer.com/resources/css-tables.php

this is the way I have done it before, obviously if you have a css file you can move the styles there, its just an example of how it can be done.

[php]

#blue { background-color: #659EC7; } #grey { background-color: #657383; } #header{ background-color: yellow; } <?php function getPosts(){ $query = mysql_query("SELECT * FROM myPosts") or die(mysql_error()); //start and first row of the table is here echo ""; if(mysql_num_rows($query)==0){ //only one row so class is always 'blue' echo''; } else { //$i is set as one $i = "1"; while($posts = mysql_fetch_assoc($query)){ //$i goes up with every iteration, this finds the remainder of $i divide by 2, therefore all odd numbers will return '1' and all even numbers '0' $x = $i % 2; //if statement sets odd numbers as blue and even numbers as grey if($x == "1") { $class = "blue"; } else { $class = "grey"; } //making the 'class' attribute a variable means that now all odd rows should be one color and all even rows another echo ""; $i++; } } echo "
Title Author Label Delete/Edit
NO POSTS FOUND!
".$posts['title']." ".$posts['author'] . " ".$posts['linklabel'] . " Delete    Edit
"; } ?>

[/php]

Thanks you again for your help Mass. Sorry for the late reply, but I was away for a few days.

I have an external stylesheet and I created three classes, .tableHL_1, tableHL_2, and tableHL-header reprsseting the three bg colors as your example.

I only have the header color working so far.

I not sure how to get it to work with the attached style sheet. Here’s my code:

[php]function getPosts(){
$query = mysql_query(“SELECT * FROM myPosts”) or die(mysql_error());

//start and first row of the table Highlight here
echo "<table><tr class='tableHL-header'> <td> Title</td> <td>Author</td> <td>Button</td> <td colspan='2'>Delte &nbsp;&nbsp; Edit</td>' </tr>";

if(mysql_num_rows($query)==0){
	echo'<tr class="tableHL-header"> <td colspan="\5\">NO POSTS FOUND!</td></tr>';
	} else	{

     //$i is set as One (1).  Hfor Highlighting table rows.
	$i="1";
	while($posts = mysql_fetch_assoc($query)){
		
	//$i goes up with every iteration, this finds the remainder of $i divide by 2, therefore all odd numbers will return '1' and all even numbers '0'
	$x=$i %2;
	
	//if statement sets odd numbers as white and even numbers as grey
	if($x == "1") {
	$class = "tableHL_1";
	} else {
	$class = "tableHL_2";
	}
	
	//making the 'class' attribute a variable means that now all odd rows should be one color and all even rows another
	
	echo "<tr class='$class'><td>".$posts['title']."</td> <td>".$posts['author'] . "</td>   <td>".$posts['linklabel'] . "</td>  <td> <a href='javascript:confirmDeletePost(\"delete_posts.php?id=".$posts['id']."\")'>Delete</a> &nbsp;&nbsp;  <a href=\"editPosts.php?id=".$posts['id']."\"> Edit </a></td></tr>";
	}

}

}[/php]

The stylesheet is already attached as include in a functions file. Once again, thanks for your help!

I finally got it working. Thanks Maas… you are a life saver!
Here’s my final code for those interested:

[php]function getPosts(){
$query = mysql_query(“SELECT * FROM myPosts”) or die(mysql_error());

//start and first row of the table Highlight here
echo "<table id='postTable'><tr class='tableHL-header'> <td> Title</td> <td>Author</td> <td>Button</td> <td colspan='2'>Delete &nbsp;&nbsp; Edit</td> </tr>";

if(mysql_num_rows($query)==0){
	echo'<tr class="tableHL-header"> <td colspan="\4\">NO POSTS FOUND!</td></tr>';
	} else	{

     //$i is set as One (1).  Hfor Highlighting table rows.
	$i=1;
	while($posts = mysql_fetch_assoc($query)){
		
	//$i goes up with every iteration, this finds the remainder of $i divide by 2, therefore all odd numbers will return '1' and all even numbers '0'
	$x=$i %2;
	
	//if statement sets odd numbers as white and even numbers as grey
	if($x == 1) {
	$class = "tableHL_1";
	} else {
	$class = "tableHL_2";
	}
	
	//making the 'class' attribute a variable means that now all odd rows should be one color and all even rows another
	
	echo "<tr class='$class'><td>".$posts['title']."</td> <td>".$posts['author'] . "</td>   <td>".$posts['linklabel'] . "</td>  
	<td> <a href='javascript:confirmDeletePost(\"delete_posts.php?id=".$posts['id']."\")'>Delete</a> &nbsp;&nbsp;  <a href=\"editPosts.php?id=".$posts['id']."\"> Edit </a></td></tr>";
	$i++;
	}

}

}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service