I have a program that gets data from a database, and then graphs it using d3 and javascript. I have a couple of methods that update and insert data into the local arrays in javascript, but I need to update and insert in the database too. How would I do this? I am using PSQL for database.
Here's my php code:
<?php
// attempt a connection
$dbh = pg_connect("host=localhost dbname=data user=postgres");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
// execute query
$sql = "SELECT * FROM dataset";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
$arr = pg_fetch_all_columns($result, 1);
// free memory
pg_free_result($result);
// close connection
pg_close($dbh);
?>
And here's the methods I am talking about, the clickEvent prompts for input when a bar is clicked, and updates the length of the bar.
function clickEvent(d, i) {
var op = prompt("Please enter the value", d);
data[i] = parseInt(op, 10);
render();
};
This method prompts for input when a "Add Data" is clicked, and adds a new bar.
d3.select("button").on("click", function(d, i) {
var op = prompt("Please enter the value", "");
var newData = parseInt(op, 10);
if (!isNaN(newData)) {
data.push(newData);
render();
}
});
Here's a jfiddle for the javascript code to see what it does.