Today's post is about use of datatables. Lot of examples are available on Internet. This example is just to explore it little more and explains how to read data from xml and display using datatables.
<?php
$file_url='blog.xml';
$file_content = file_get_contents($file_url);
$xml_string_arr=simplexml_load_string($file_content);
//Optional: Start of code if you want to add some attribute to xml
//code to add unique_id attribute
/*foreach( $xml_string_arr->xpath("entry") as $r ) {
$attrs = $r->attributes();
if(!$attrs['unique_id'])
$r->addAttribute('unique_id', $r->id);
}
$xml_string_arr->asXML($file_url);*/
//End of code if you want to add some attribute to xml
$json = json_encode($xml_string_arr);
$arr_new = json_decode($json,TRUE);
$required_content_arr = $arr_new['entry'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Task3</title>
<link rel="stylesheet" href="DataTables/media/css/jquery.dataTables.css">
<script src="DataTables/media/js/jquery.js"></script>
<script src="DataTables/media/js/jquery.dataTables.js"></script>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
</table>
</body>
</html>
<script language="javascript">
$(document).ready(function() {
var oTable = $('#example').dataTable({
data: <?php echo json_encode($required_content_arr); ?>,
columns: [
{ data: 'id' },
{ data: 'name' },
{ data: 'url' },
{ data: 'height' },
]
});
} );
</script>
Thanks!!!!!!!!!! Enjoy Programming :)
<?php
$file_url='blog.xml';
$file_content = file_get_contents($file_url);
$xml_string_arr=simplexml_load_string($file_content);
//Optional: Start of code if you want to add some attribute to xml
//code to add unique_id attribute
/*foreach( $xml_string_arr->xpath("entry") as $r ) {
$attrs = $r->attributes();
if(!$attrs['unique_id'])
$r->addAttribute('unique_id', $r->id);
}
$xml_string_arr->asXML($file_url);*/
//End of code if you want to add some attribute to xml
$json = json_encode($xml_string_arr);
$arr_new = json_decode($json,TRUE);
$required_content_arr = $arr_new['entry'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Task3</title>
<link rel="stylesheet" href="DataTables/media/css/jquery.dataTables.css">
<script src="DataTables/media/js/jquery.js"></script>
<script src="DataTables/media/js/jquery.dataTables.js"></script>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
</table>
</body>
</html>
<script language="javascript">
$(document).ready(function() {
var oTable = $('#example').dataTable({
data: <?php echo json_encode($required_content_arr); ?>,
columns: [
{ data: 'id' },
{ data: 'name' },
{ data: 'url' },
{ data: 'height' },
]
});
} );
</script>
Thanks!!!!!!!!!! Enjoy Programming :)
Comments
Post a Comment
Thanks for your valuable comments.