Khalid Khalid - 1 month ago 6x
PHP Question

Check <th> value and update in <td> based on condition

I have a result and a json , where $response['json'] contain table header columns and table data comes for the $response['results']. i need to check if the header contain column name as "NN ID" then respective table column data should be in a anchor

<a href="">Data</a>

PHP code:

$json = $response['JSON'];
$result = $response['RESULT'];

echo '<strong>Search Results</strong>
<h4 class="bg-default"><strong>Total Records ('.count($result).')</strong></h4>';

if (count($result) > 0) {

echo '<table class="table table-striped table-hover table-bordered">
<thead class="bg-primary">
foreach (array_values($json) as $column) {
echo '<th>'.$column.'</th>';
echo '</tr>
foreach ($result as $row) {
echo '<tr>';
foreach (array_keys($json) as $field) {
echo '<td>'.$row[$field].'</td>';
echo '</tr>';
echo '</tbody>


Need to add href for

<a onclick="parent.LoadIframe(\'/view.php?nnid='.$row[field].'\')">

Current View:

enter image description here

Need View as a link for NN ID Column data

enter image description here


A simple if statement is all you need:

foreach ($result as $row) {
            echo '<tr>';
            foreach ( array_keys($json) as $field) {
                if ( $field == 'NN ID' ) {
                    echo '<td><a onclick="parent.LoadIframe(\'/view.php?nnid='.$row[$field].'\')">
                } else {
                    echo '<td>'.$row[$field].'</td>';
            echo '</tr>';