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
 
Associate
Joined
24 May 2011
Posts
262
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.
 
Associate
OP
Joined
6 Mar 2009
Posts
495
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.
 
Associate
Joined
4 Feb 2011
Posts
580
Location
Halifax
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:
Associate
OP
Joined
6 Mar 2009
Posts
495
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.
 
Soldato
Joined
26 Aug 2012
Posts
4,399
Location
North West
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:
Associate
Joined
4 Feb 2011
Posts
580
Location
Halifax
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'){

}
 
Back
Top Bottom