How to dynamically create table columns?

How to dynamically create table columns?

dajones12dajones12 Posts: 5Questions: 3Answers: 0

Hi all,
I have a stored procedure that returns data. Prior to executing the stored procedure, I do not know the number of columns or the title for each column. I'm trying to create a table to display the data from the stored procedure. In the past, I have just hard coded the header names and brought the data in with something like this:
HorizontalData.php

        $(document).ready(function() {
            $('#HorizontalData_ajax').dataTable( {
                "sAjaxSource": "content/HorizontalData_ajax.php?startdate=<?php echo $startdate;?>&enddate=<?php echo $enddate;?>&description=<?php echo $description;?>",
                "bPaginate": true,
                "processing": true,
                "bDeferRender": true, 
                "bAutoWidth": false,
                "bScrollCollapse": true,
                "sScrollY": "250px",
                "bStateSave": true,
                "sDom": '< TC > ftiS',
                "oTableTools": {
                    "sSwfPath": "js/copy_csv_xls.swf",
                    "aButtons":[
                        "copy",
                        "print",
                        {
                            "sExtends":    "collection",
                            "sButtonText": "Save",
                            "aButtons":    [ "xls" ]
                        }
                                ]
                                },
                                 } ).fnSetFilteringDelay();
        } );

    </script>

<div class="clear20"></div>
    <table id="HorizontalData_ajax" class="pretty" border="0">
        <thead>
            <tr>
                <th>Lot</th>
                <th>% +10 mesh</th>
                <th>% -50 mesh</th>
                <th>% Moisture</th>
                <th>Avg 24r Viscosity</th>
                <th>Avg u 10/u0</th>
                <th>Coating: %</th>
                <th>Color</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
        </tfoot>
    </table>


    <?php } ?>

HorizontalData_ajax
```
$resultsDescription = odbc_exec($connection, "EXEC CompanyDB.dbo.sp_stored_procedure '$startdate','$enddate','$description'");

$out = array();

while($data = odbc_fetch_array($resultsDescription))
{

$row = array(
    $data['Lot'],
    $data['% +10 mesh'],
    $data['% -50 mesh'],
    $data['% Moisture'],
    $data['Avg 24r Viscosity'],
    $data['Avg u10/u0'],
    $data['Coating: %'],
    $data['Color']);           


      $out[] = $row;

}

echo json_encode(array("aaData" =>$out));

<?php > ``` I can't do this, however, because I do not know ahead of time what the data is I'm looking for, the columns names or even the number of columns returned. Any help on how to dynamically create this table is greatly appreciated. ?>

Answers

This discussion has been closed.