Khalid Khalid - 2 months ago 7
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>
tag.

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">
<tr>';
foreach (array_values($json) as $column) {
echo '<th>'.$column.'</th>';
}
echo '</tr>
</thead>
<tbody>';
foreach ($result as $row) {
echo '<tr>';
foreach (array_keys($json) as $field) {
echo '<td>'.$row[$field].'</td>';
}
echo '</tr>';
}
echo '</tbody>
</table>';

}


Need to add href for

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


Current View:

enter image description here

Need View as a link for NN ID Column data

enter image description here

Answer

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].'\')">
    '.$row[$field].'
</a></td>';
                } else {
                    echo '<td>'.$row[$field].'</td>';
                }
            }
            echo '</tr>';
        }
Comments