Brian Ramsey Brian Ramsey - 21 days ago 6
PHP Question

PHP add column to end of foreach generation

What seemed like something really easy and basic tonight has taken so long and I still have no result.. All I am trying to do is append a

<td>
to the end of a foreach generation and everything I try isnt working (google seems on strike for me also).

Any help would be appreciated, I am just trying to add a delete button calling my JS function at the end of my array data...

The Code:

<?php
if(!empty($JSONdata)){

foreach($JSONdata AS $k => $data){
echo '<tr id="' . $k . '">';
foreach($data AS $key => $value){
$len = count($data);
if($key < $len){
//normal lines (hide counter column)
$key == 'Counter' ? $hidden = 'display: none;' : $hidden = '';
echo '<td style="' . $hidden. '"><input id="' . $key .'" value="' . $value . '"/></td>';
}

elseif($key == $len){
//delete button
echo '<td><input type="button" value="Del" onclick="deleteRow(this)"></td>"';
}
}
echo '</tr>';
}
}
?>

Answer
<?php
if (!empty($JSONdata)) {

    foreach ($JSONdata AS $k => $data) {
        echo '<tr id="' . $k . '">';
        foreach ($data AS $key => $value) {
            echo '<td style="' . $hidden . '"><input id="' . $key . '" value="' . $value . '"/></td>';

        }
        echo '<td><input type="button" value="Del" onclick="deleteRow(this)"></td>"';
        echo '</tr>';
    }
}
?>

Should do the trick. This will add a button at the end of each row for you.