user3065587 user3065587 - 1 month ago 6
PHP Question

Need help changing table array by moving a column in PHP

I have been handed the task of making some changes to a PHP webpage that was coded by someone who has left the company and my Php is exactly exeprt.
The page in question displays a database table in a SQL server that allows you to update values via an update page.
Currently the Update function sits under the 'Action' column at the end of the table and I need to relocate the 'Action' column to the start of the table before the 'Name' column.
When I try to make changes, I break the table array and the 'Update' function no longer works.

Current order of columns are;
Name,
Value,
Details,
Action

The new order of columns attempting to achieve
Action,
Name,
Value,
Details

I have also included the code in question.
Any assistance would be appreciated

Note** It is a Php website running on a Windows box and connecting to a MSSQL Server 2008

$query = sqlsrv_query($conn, 'SELECT * FROM Database_Values ORDER BY Name ASC');
// Did the query fail?
if (!$query) {
die( FormatErrors( sqlsrv_errors() ) );
}
// Dump all field names in result
$field_name = "";
foreach (sqlsrv_field_metadata($query) as $fieldMetadata) {
foreach ($fieldMetadata as $name => $value) {
if ($name == "Name") {
$field_name .= $value . '-';
}
}
}
$field_name = substr_replace($field_name, "", -1);
$names = explode("-", $field_name);
?>
<div style="max-height:610px; overflow:auto;">
<table border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" class="table" width="100%">
<tr>
<?php
foreach ($names as $name) {
?>
<th><?php echo $name; ?></th>
<?php
}
?>
<th>Action</th>
</tr>
<?php
// Fetch the row
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
//print_r($row);
?>
<tr>
<?php
foreach ($row as $key => $eachrow) {
?>
<td nowrap="nowrap">
<?php echo $eachrow; ?>
</td>
<?php
}
?>
<td nowrap="nowrap">&nbsp;
<?php $groupid = $_SESSION["gid"] ;
if($groupid!='1') {
?>
<a href="javascript:void(0);" title="Permission Restricted" >Update</a>
<?php } else { ?>
<a href="javascript:void(0);" onclick="update('<?php echo $row['CodeName'] ?>');">Update</a>

<?php } ?>
</td>
</tr>
<?php
}
?>
</table>
</div>

Answer

All you need to do is change the order of the th and td cells in the html

    $query = sqlsrv_query($conn, 'SELECT * FROM Database_Values ORDER BY Name ASC');
    // Did the query fail?
    if (!$query) {
        die( FormatErrors( sqlsrv_errors() ) );
    }
    // Dump all field names in result
    $field_name = "";
    foreach (sqlsrv_field_metadata($query) as $fieldMetadata) {
        foreach ($fieldMetadata as $name => $value) {
            if ($name == "Name") {
                $field_name .= $value . '-';
            }
        }
    }
    $field_name = substr_replace($field_name, "", -1);
    $names = explode("-", $field_name);
    ?>
    <div style="max-height:610px; overflow:auto;">
        <table border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" class="table" width="100%">
            <tr>
                <th>Action</th>
                <?php
                foreach ($names as $name) {
                    ?>
                    <th><?php echo $name; ?></th>
                    <?php
                }
                ?>

            </tr>
            <?php
            // Fetch the row
            while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
                //print_r($row);
                ?>
                <tr>
                    <td nowrap="nowrap">&nbsp;
                    <?php $groupid  =   $_SESSION["gid"] ; 
                    if($groupid!='1') { 
                    ?>
                    <a href="javascript:void(0);" title="Permission Restricted"  >Update</a>
                    <?php } else { ?>
                    <a href="javascript:void(0);"  onclick="update('<?php echo $row['CodeName'] ?>');">Update</a>

                      <?php } ?>  
                    </td>
                       <?php
                    foreach ($row as $key => $eachrow) {
                        ?>
                        <td nowrap="nowrap">
                            <?php echo $eachrow; ?>
                        </td>
                        <?php
                    }
                    ?>

                </tr>
                <?php
            }
            ?>
        </table>
    </div>