0

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.

Community
  • 1
  • 1
Newwt
  • 491
  • 1
  • 5
  • 22
  • You may need to include your Select.php to use that class in slimSelect.php – Ahmed Ginani Apr 24 '17 at 09:38
  • @AhmedGinani Shouldn't the Autoloader deal with it when I do `$date = new Select()`? – Newwt Apr 24 '17 at 09:39
  • As per my knowledge it shouldn't – Ahmed Ginani Apr 24 '17 at 09:42
  • @AhmedGinani Okay. I now included it inside slimSelect.php, and changed the `function()` to `function(Request $id, $from, $to)`. Also added this to try: `$id = $id->getAttribute('id');` (same with from and to). I now get "Call to undefined method Slim\Http\Response::getAttribute()" on the second `getAttribute`. Why is that? – Newwt Apr 24 '17 at 09:46
  • Do one thing, place your Select.php in models folder and then try to get $date = new \models\Select(); but you class should extended e.g class 'Select extends \Slim\Middleware{' – Ahmed Ginani Apr 24 '17 at 09:48
  • @AhmedGinani Nothing. I get class not found... – Newwt Apr 24 '17 at 09:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142478/discussion-between-ahmed-ginani-and-newwt). – Ahmed Ginani Apr 24 '17 at 10:59

1 Answers1

1

your Select class is not auto-loaded that's why you are getting this error. have you seen this recommendation from the slim's website. https://www.slimframework.com/ "The easiest way to start working with Slim is to create a project using Slim-Skeleton as a base by running this bash command:

$ php composer.phar create-project slim/slim-skeleton [my-app-name] ". I do normally used this skeleton app to build my slim apps.

ojasseh
  • 11
  • 4
  • Yes. I did the "Hello, {name}" example to learn how it works and such, but as simple as that is, I don't see the way to apply that to my project :/ – Newwt Apr 24 '17 at 09:50