Check column name contains certain character

Associate
Joined
6 Mar 2009
Posts
495
Hi guys, im trying to write some code that checks if a column heading ends with a specific letter and then highlight everything in that column.

I have a loop that prints out all data from an array of table names and would like it to highlight certain columns.

Here what I have been trying to do. Probably way off but im not sure how to go about it.

Here is what I have to print the column headings.

Code:
echo "<div style='font-weight:bold; color:red'>" .$tables[$i]."</div>";
		// Prints the columns
		echo "<table class='searchTable' border='1';  <tr>"; 
		for($j = 0; $j < mysql_num_fields($result); $j++) {  
		$isP = mysql_query("SELECT * FROM ($tables[$j]) WHERE column LIKE '%P'");   
		$field_info = mysql_fetch_field($result, $j);
		if($isP)
		     echo "<div style='font-weight:bold; color:red'>" .$field_info."</div>";
			 
			 else
		echo "<th>{$field_info->name}</th>"; }

Have tried to add a query in there that checks if the last letter of a column ends in 'P' and if so then print it in red.

Any help would be great, thanks
 
I would advise you get it to print out the table first without any highlighting, you can work on that after the table is printing out correctly.
 
I would advise you get it to print out the table first without any highlighting, you can work on that after the table is printing out correctly.

The tables are printing out correctly as I want them its just this additional highlight feature I want to add to it now.
 
Well there's a couple ways of doing it. You can either colour each td individually if it's a match, or you can use the following and add styles to the <col> tags.
PHP:
<table>
 <colgroup>
 <col style="font-weight:bold; color:red"></col>
 <col></col>
 <!-- For each column in the table -->
</colgroup>
<tr>
  <th>Column 1</th>
  <th>Column 2</th>
</tr>
<tr>
  <td>data</td>
  <td>data</td>
</tr>
</table>
 
Last edited:
Well there's a couple ways of doing it. You can either colour each td individually if it's a match, or you can use the following and add styles to the <col> tags.

PHP Code:
<table>
<colgroup>
<col style="font-weight:bold; color:red"></col>
<col></col>
<!-- For each column in the table -->
</colgroup>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
<tr>
<td>data</td>
<td>data</td>
</tr>
</table>

Thanks xbenjiiman.

Its more so the php/MySQL that I need help with as to how to check the column name in the loop.
 
Assuming I get what you mean, something like this? But obviously make it relevant to your example as I did it quickly on my phone.
PHP:
$value_to_check_against="p";
$array =str_split($table_column_name,1); //splits string into individual characters
if(end($array)==strtolower($value_to_check_against)) //checks last letter of array agains't your value
  {
      /* Set your colour output if match here*/
  }
else
  {
     /*if no match do this*/
   }
 
Last edited:
Assuming I get what you mean, something like this? But obviously make it relevant to your example as I did it quickly on my phone.
PHP:
$value_to_check_against="p";
$array =str_split($table_column_name,1); //splits string into individual characters
if(end($array)==strtolower($value_to_check_against)) //checks last letter of array agains't your value
  {
      /* Set your colour output if match here*/
  }
else
  {
     /*if no match do this*/
   }

PHP:
if(preg_match('/p$/', $stringToCheck)){

}

Or...

PHP:
if(substr($stringToCheck, -1) == 'p'){

}
 
You sure you want to do a mysql query per iteration of the for loop?

Why not do the column highlighting with a bit of jQuery?
 
Back
Top Bottom