I need a script that can show results form my database dynamically. I found a method here: http://openenergymonitor.org/emon/node/107
My question is how to alter the code so that it will show all results instead of only one?
And is it possible to display all results on load and then add newly added lines afterwards?
I tried the following on the client.php:
<html>
<head>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<h2> Client example </h2>
<h3>Output: </h3>
<div id="output">Henter data.</div>
<script id="source" language="javascript" type="text/javascript">
$(function ()
{
setInterval(function() { $.ajax({
url: 'api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var id = data[0]; //get id
var vname = data[1]; //get name
$('#output').append("<b>id: </b>"+id+"<b> name: </b>"+vname+"<br />"); //Set output element html
//recommend reading up on jquery selectors they are awesome
// http://api.jquery.com/category/selectors/
}
}); }, 1000);
});
</script>
</body>
</html>
And this is what I tried on the api.php page:
$result = mysql_query("SELECT * FROM $tableName ORDER BY id DESC");
while($row = mysql_fetch_assoc($result)){
echo json_encode($row);
}
Now it won't show anything. What am I doing wrong?
Thank you!