Context: I've established a connection with a MySQL database that has Stock Ticker, Open, High, Low, Close prices, interval (1d, 1wk and 1mo) and date. Now, I'd like to make it possible for a user to select the "ticker" name and "interval" in HTML and send this input as a MySQL search query with PHP (I'm trying to do this using the action and method="post"). This data would then be saved or pushed into the "data" variable in a javascript file (which will create a candlestick graph using google charts).
TL;DR - User selects ticker and interval, this is sent to MySQL with PHP to collect the data and this data is saved in a javascript variable called "data" to create a candlestick graph.
Problem: I'm not sure how I could create this interface between MySQL outcome from PHP and then push this data to the javascript variable. I was trying to do $data = array()
and then somehow push this data in the data variable on my js file, but not sure how to do this (I'm new to PHP). I was also trying to make it so that on the PHP file ticker='$ticker'
to grab the HTML user input (since it's id of the input is ticker and Interval), but not sure if this is the correct way of doing this
Any help is very welcome! HTML
<section class="no-padding-top no-padding-bottom">
<div class="container-fluid">
<div class="statistic-block block">
<form name="query" method="post" action="connection.php">
<div class="ticker">
<label for="name">Ticker:</label>
<input style="background: rgba(255,255,255,0.5);border-radius: 10px; border: none; text-align: center;"
type="text" id="ticker" name="ticker" required minlength="4" maxlength="8" size="10"
placeholder="TSLA"></input>
</div>
<div class="Interval">
<label for="name">Period:</label>
<select
style="background: rgba(255,255,255,0.5);border-radius: 10px; border: none; text-align: center;"
name="Interval" id="Interval">
<option value="1d">1 day</option>
<option value="1wk">1 week</option>
<option value="1mo">1 month</option>
</select>
</div>
</form>
Javascript
google.charts
.load("current", {
packages: ["corechart"],
})
.then(function drawChart() {
var rows = [
["2021-01-01", 2722124.01, 2715937.44, 2724963.79, 2715937.44], //these are just dummy values to test if the chart was functional or not. The goal is to push the PHP data in here to create the graph
];
var data = google.visualization.arrayToDataTable(
rows.map(function (row) {
var date = row[0];
var low = row[3];
var open = row[1];
var close = row[4];
var high = row[2];
return [date, low, open, close, high];
}),
true
);
var options = {
legend: "none",
candlestick: {
fallingColor: { strokeWidth: 0, fill: "#a52714" }, // red
risingColor: { strokeWidth: 0, fill: "#0f9d58" }, // green
},
};
var chart = new google.visualization.CandlestickChart(
document.getElementById("candlestick")
);
chart.draw(data, options);
});
PHP
<?php
$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT Date, Open, High, Low, Close FROM symbols WHERE ticker='$ticker' AND interval='$Interval'";
$result = mysqli_query($conn, $sql);
// $data = array(); possibly create a data array to store all the data and then push this data onto the javascript?
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "[" . $row["Date"]. "," . $row["Open"]. "," . $row["High"]. "," . $row["Low"]. "," . $row["Close"]. "],";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>