php array to load dataTables

php array to load dataTables

edcolonedcolon Posts: 1Questions: 1Answers: 0

Hello,

Need to execute a piece of php code, where I will generate an array, say:
/* php code begins*/

$DATA = array();

$DATA[] = array(
'Name' => 'Tiger Nixon',
'Position' => 'System Architect',
'Office' => 'Edinburgh',
'Extn.' => '5421',
'Start date' => '2011/04/25',
'Salary' => '$320,800'
);

@DATA = $DATA;

/* php code ends */

Then, want to load it into dataTables with javascript code:

var dataSet = JSON.parse(
decodeURIComponent(
@DATA));?>"
)
);

$(document).ready(function() {
$('#example').DataTable( {
data: dataSet,
columns: [
{ title: "Name" },
{ title: "Position" },
{ title: "Office" },
{ title: "Extn." },
{ title: "Start date" },
{ title: "Salary" }
]
} );
} );

What am I doing wrong?

Thanks.

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    edited January 2016

    I don't know anything about php, my world is purely html & js, but going to try and help.

    What does the array look like after executing this?

    $DATA[] = array( 'Name' => 'Tiger Nixon', 'Position' => 'System Architect', 'Office' => 'Edinburgh', 'Extn.' => '5421', 'Start date' => '2011/04/25', 'Salary' => '$320,800' );

    In order to add what you want to the DataTable config you have, the resulting array has to be.......

    [
    {'Name':'Tiger Nixon', 'Position':'System Architect', 'Office':'Edinburgh', 'Extn.':'5421', 'Start date':'2011/04/25', 'Salary':'$320,800'},
    {'Name':'Leeroy Jenkins', 'Position':'System Architect II', 'Office':'Edinburgh', 'Extn.':'5420', 'Start date':'2010/04/25', 'Salary':'$520,800'}
    ]

    Each index in your array must be an object containing (Name, Position, Office, Extn, Start data, Salary).

    I could be completely wrong about this.

This discussion has been closed.