Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

slim routing php mysql

Here is my index.php file that I have written, using the slim framework. I have a portfolio page that is being rendered, but I also have the items linking to another page by using an id selected from mysql. However this is where I have the problem, that file is not rendering. Does anyone know what I am doing wrong, I have spent so much time on this, my head is going bonkers!

<?php

require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set ( "Europe/London" );


$app = new \Slim\Slim( array(
  'view' => new \Slim\Views\Twig()
));


$view = $app->view();
$view->parserOptions = array(
    'debug' => true,

);

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);


//what happens here is what will run with our object
$app->get('/', function() use($app){
  $app->render('about.twig');
});


//portfolio page

    $app->get('portfolio/{id}', function($id) use ($app) {
        $item = $app['item']->getRequest($id);
        $app['twig']->render('portfolioitem.twig', array(
            'item' => $item
        ));
    });
    $app->get('/portfolio', function() use ($app) {
        $app->render('portfolio.twig');
    });



$app->get('/contact', function() use($app){
  $app->render('contact.twig');
})->name('contact');

$app->run();

function getRequest($id) {
    $db = getConnection();
    $sql = "SELECT * FROM documentaries WHERE id=$id";
    if ($item = mysqli_fetch_assoc($result)) {
            $result = mysqli_query($db,$sql);
            $item['id'] = array();
            while ($row = mysqli_fetch_assoc($result)) {
                $item['id'][] = $row['id'];
            }
            return $item;
        } else {
            return null;
        }
}

function getConnection() {
  $dbhost="localhost";
  $dbuser="root";
  $dbpass="i";
  $dbname="tom_db";
  $dbh =  mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
  if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
  }
}
?>