I’ve started working on quite big project.
Concept: Some1 inserts data to mysql. Other people gets data live on their screen(table)
I had my code working, but I was filling the tables in php and as I understand it is not good for bandwidth, so I’m trying to catch data from mysql with php code, send it to js, which would then format/fill the table (setinterval is running). But my table isn’t filling and I’m out of ideas.
I’m new to js, so if You see anything wrong let me know! Thank You!
PHP:
<?php
$rows = array();
$con = mysql_connect("where","who","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT * FROM Brokers");
$rows = array();
while($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}
mysql_close();
print json-encode($rows);
?>
JS:
function fetchData()
{
$.ajax({
url: 'brokers.php',
dataType: 'json',
success: createTable(rows)
})
}
function createTable(rows)
{
var flowTable = '<table id="resultTable">'
for (var i=0; i< rows.length; i++){
flowTable += '<tr class="filterthis height20">',
flowTable += '<td class="companyname width120">',
flowTable += rows[i].company_name + '</td>',
flowTable += '<td class="width180">' ,
flowTable += rows[i].address + '</td>',
flowTable += '<td class="width70">' ,
.........SOME MORE...........
flowTable += '</tr>';
}
flowTable += '</table>';
$("#here").innerHTML = flowTable;
}
function starttimer(){
interval = setInterval("fetchData()",1000);
}
starttimer();
Well I found a solution.
Corrections to script
JS:
brokers.php: