Highlighting data in a loop

Associate
Joined
4 Feb 2014
Posts
12
I have a while loop setup that grabs data from my database and inserts into tables. What im stuck on and want to do is check the table column name and any ending in the letter 'P' I want to be able to highlight or bold all of the data in that row.

Can someone help me with this code please??

Here is the loop I have:
Code:
// Prints the columns
		echo "<table class='searchTable' border='1';  <tr>"; 
		for($j = 0; $j < mysql_num_fields($result); $j++) {     
		$field_info = mysql_fetch_field($result, $j);     
		echo "<th>{$field_info->name}</th>"; }  
		
		// Print the data 
		
		while($row = mysql_fetch_row($result)) {     
		echo "<tr>";     
			foreach($row as $_column) {   
			      
				echo "<td>{$_column}</td>";     }     
				echo "</tr>"; 
				}  
				
				echo "</table>";
 
Do you mean row or column, I ask because what happens if you get to column 5 of 10 and it's the first column that ends with a 'P'. the formatting for previous columns will have already been done?
 
Do you mean row or column, I ask because what happens if you get to column 5 of 10 and it's the first column that ends with a 'P'. the formatting for previous columns will have already been done?

Sorry I will rephrase that, as that was incorrect what I said.

I want all the data in columns that end in a 'P' to be highlighted, not rows.
 
Code:
$isP = strtolower‎ (substr($_column, -1)) == 'p';
echo '<td style="' . ($isP ? 'background:green') . '">' . $_column . '</td>';

Check code as i'm writing this while in middle of conf call so minds not 100% on it :D
 
Code:
$isP = strtolower‎ (substr($_column, -1)) == 'p';
echo '<td style="' . ($isP ? 'background:green') . '">' . $_column . '</td>';

Im am getting an undefined error for the strtolower function. Not sure what the "?" in $isP ? 'background:green' does, or was that a typo?

Thanks
 
It's erroring due to the space before the brace.

The ? is a ternary operator, think of it as a short hand IF/ELSE

Code:
<?php echo true ? 'True' : 'False'; ?>

Getting an error now on the second line. Unexpected ')'!! Not sure why.
 
Sorry if that confused you more, like I said I wasn't really focusing when I gave that example.

Take this next example, should explain it more and this time i've actually made sure it will work for you ;)

Code:
<?php
  $columns = array_merge(
    array_fill(-1, 10, 'Bob'),
    array_fill(-1, 10, 'Barry'),
    array_fill(-1, 10, 'Pop'),
    array_fill(-1, 10, 'Foo'),
    array_fill(-1, 10, 'bar'),
    array_fill(-1, 10, 'Silly Pop')
  );

  function endsWith($needle, $haystack){
    return strtolower(substr($haystack, -1)) == $needle;
  }
?>

<style>
  .highlight {
    background: #99cc00;
    color: #fff;
    font-weight: bolder;
  }
</style>

<table border="1">
  <?php foreach( $columns as $column ): ?>
    <tr>
      <td class="<?php echo endsWith('p', $column) ? 'highlight' : '';?>"><? echo $column; ?></td>
    </tr>
  <?php endforeach; ?>
</table>

Nothing special, you're just checking the last character of the string and then adding a class to the <td/>. I always forget that PHP doesn't like truthy only ternaries.
 
Back
Top Bottom