<html>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<div id='liclass'>
</div>
<?php
require('db.php');
$dbh = get_db();
$person_list =
$dbh->query('select fullname, birthdate, country_code from person')
->fetch_all(MYSQLI_ASSOC);
// echo format_html_table($person_list, 'persontable');
?>
/*
<!-- uebergabe der Daten mittels einer "umwandlung" von php in JavaScript
auf diese art eher unueblich
-->
<script>
var person_list = <?php echo json_encode($person_list); ?>;
$(document).ready(function() {
$.each( person_list, function( key, person ) {
$('#liclass').append("<li>"+ person['fullname'] +"</li" );
});
} );
</script>
*/
// direkte Formatierung der Daten mit php in html
<div id="person_list">
<?php
foreach ($person_list as $person) {
printf ("<li> %s, %s, %s </li>\n",
$person['fullname'],
$person['birthdate'],
$person['country_code']
);
}
?>
</div>
<html>
Oder die Trennung von Präsentation und Datengenerierung
php, liefert JSON list-persons-ajax.php
<?php
require('db.php');
$dbh = get_db();
$person_list =
$dbh->query('select fullname, birthdate, country_code from person')
->fetch_all(MYSQLI_ASSOC);
header('Content-type: application/json');
echo json_encode($person_list);
welches dann mit jquery get abgefragt und danach formatiert angezeigt wird
<html>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<div id='liclass'>
</div>
<script>
$(document).ready(function() {
var ret = $.get("/kurs/list-persons-ajax.php")
.done(function( person_list) {
$.each( person_list, function( key, person ) {
$('#liclass').append("<li>"+ person['fullname'] +"</li" );
});
});
});
</script>
</html>