I've been trying for quite a while, and I don't quite understand how and why everything works with slim. Without Slim everything works fine and I kinda know how everything works (this is a small project I'm doing to learn ajax, object-oriented php and Slim, and never did this, so I'm REALLY lost).
What I have right now is an html with a form, which sends the data to a .php file via ajax. That file takes the data, runs a query, puts the results in a .json string, and the html then prints the results as a charts.js canvas.
This is my class with php (Select.php):
<?php require_once 'Connection.php';
$id = $_POST['id'];
$from = $_POST['from'];
$to = $_POST['to'];
$date = new Select($dbh, $id, $from, $to);
return $dates->select();
class Select {
private $dbh;
public function __construct($dbh, $id, $from, $to) {
$this->dbh = $dbh;
$this->id = $id;
$this->from = $from;
$this->to = $to;
}
public function select() {
$id = $this->id;
$from = $this->from;
$to = $this->to;
$query = ***Ignoring it because it's quite long***
$results = [];
while ($arr = $query->fetch(PDO::FETCH_ASSOC)) {
$results[] = $arr;
}
echo json_encode($results);
}
}
My ajax script (generateChart.js):
$.ajax({
type: 'post',
url: 'classes/Select.php',
data: $('form').serialize(),
success: function (data) {
var results = JSON.parse(data);
var chartjsTemp = [];
var chartjsDate = [];
for (var i = 0; i < results.length; i++) {
chartjsTemp.push(results[i].probeTemp);
chartjsDate.push(results[i].dateProbe);
}
var ctx = document.getElementById('myChart').getContext('2d');
var button = $("#submitButton");
submitButton.addEventListener("click", function(){
myChart.destroy();
});
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: chartjsDate,
datasets: [{
label: 'temp',
data: chartjsTemp,
backgroundColor: "rgba(240,240,240,0.5)"
}]
}
});
}
});
And this is where I'm trying to implement 'Select.php' (called slimSelect.php). I know this file is a total trainwreck, but I'm pretty much going by trial and error, but I'm definitely not advancing at all. I don't understand the documentation, and other posts here in stackoverflow don't quite explain how it works:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require 'Autoloader.php';
$app = new \Slim\App;
$app->post('/', function(Request $request) use ($app) {
$allPostPutVars = $request->getParsedBody();
$id = $allPostPutVars['id'];
$from = $allPostPutVars['from'];
$to = $allPostPutVars['to'];
include 'Select.php';
});
$app->run();
Right now, it gives me Fatal error: Class 'Select' not found
, although it DOES exist. It just goes over my head how to make slim and my class work together. If someone could explain me how I could make it work, or at least point me in the right direction I would really appreciate it.
Edit: Modified slimSelect.php. I now get "Fatal error: Call to undefined method Slim\Http\Response::getAttribute() on line 11"
Edit2: GOT SOMETHING WORKING!!!! One of my main problems was that I completely messed up the function in $app->post
, which gave me errors with $request and such. The magic happens in lines 9 and 10. It now sends the .json back and prints the chart! Now I want to find out how to call the class Select without including it.