mysql - Silm routing php -


i have created database in mysql , have page portfolio items. when try create link item of portfolio via id keep getting errors.

i have had @ slim docs

i have had @ various questions here on stackoverflow, including;

this question

there few more viewed group routing, still getting confused. have read on internet using pdo it, have not covered yet. wandering if there way route portfolio item when clicked on template, url use id number of row in mysql table?

here index file, if need see more please let me know. many thanks

<?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(), );  $dbc = mysqli_connect('localhost','root','pass','tom_db') or die('error connecting mysql server');  $query = "select id  documentaries"; $result = mysqli_query($dbc,$query);  while ($row = mysqli_fetch_assoc($result)){         $data[] = $row["id"]; }    //what happens here run our object $app->get('/', function() use($app){   $app->render('about.twig'); });   //portfolio page     $app->get('/portfolio', function() use ($app) {         $app->render('portfolio.twig');     });      $app->get('portfolio/{id}', function ($id) {         $app->render('portfolioitem.twig', array(             'data' => $data         ));     });  $app->get('/contact', function() use($app){   $app->render('contact.twig'); })->name('contact');  $app->run();   ?> 

edit/update:

the error having when clicking on portfolio link not render url portfolio/1 if manually enter url in browser renders html no styles. rendering twig file, not seem working

here updated index file;

<?php  require __dir__ . '/vendor/autoload.php'; date_default_timezone_set ( "europe/london" );  \slim\slim::registerautoloader(); $app = new \slim\slim();  $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 run our object $app->get('/', function() use($app){   $app->render('about.twig'); });  //portfolio page $app->get('/portfolio', function() use($app){   $app->render('portfolio.twig'); })->name('portfolio');  //this function problem is, when in portfolio.twig file not link $app->get('/portfolio/:id', function ($id) use ($app) {     // include out mysql connection code , make connection     $db = mysqli_connect('localhost', 'root', 'pass', 'tom_db');       // query database     $rs = mysqli_query($db, "select id documentaries id=$id" );      // convert record set associative array can work     $data = mysqli_fetch_assoc($rs);       $app->render('portfolioitem.twig', array(             'data' => $data         )     ); });   $app->get('/contact', function() use($app){   $app->render('contact.twig'); })->name('contact');  $app->run();   ?> 

edit: additional twig files added

this portfolioitem.twig

{% extends 'main.twig' %}  {% block nav %}      <ul>         <li><a href="{{ baseurl() }}" >about</a></li>         <li><a href="{{ siteurl('/portfolio') }}" class="selected">portfolio</a></li>         <li><a href="{{ siteurl('/clients') }}">clients</a></li>         <li><a href="{{ siteurl('/teaching') }}">teaching</a></li>         <li><a href="{{ siteurl('/contact') }}">contact</a></li>      </ul>  {% endblock nav %} {% block content %}    <section>     <h2>this test</h2>   </section>  {% endblock content %} 

this portfolio.twig

{% extends 'main.twig' %}  {% block nav %}      <ul>         <li><a href="{{ baseurl() }}" >about</a></li>         <li><a href="{{ siteurl('/portfolio') }}" class="selected">portfolio</a></li>         <li><a href="{{ siteurl('/clients') }}">clients</a></li>         <li><a href="{{ siteurl('/teaching') }}">teaching</a></li>         <li><a href="{{ siteurl('/contact') }}">contact</a></li>      </ul>  {% endblock nav %}  {% block content %}        <section>         <div class="showreel-container">          <iframe src="https://player.vimeo.com/video/148640837?title=0&byline=0&portrait=0" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>         </div>       </section>        <section>          <div id="controls" id="controls">              <button class="filter" data-filter="all">all</button>             <button class="filter" data-filter=".documentaries">documentary</button>             <button class="filter" data-filter=".commercial">commercial</button>             <button class="filter" data-filter=".charity">charity/ngo/commisions</button>             <button class="filter" data-filter=".music">music</button>             <button class="filter" data-filter=".drama">drama</button>          </div>            <div id="container" class="container">            <div class="mix documentaries" data-myorder="1"><a href="portfolio/{{ data.id }}"><img src="img/numbers-01" alt="image one"/></a></div>           <div class="mix commercial" data-myorder="2"><a href=""><img src="img/numbers-02" alt="image two"/></a></div>           <div class="mix commerical" data-myorder="3"><a href=""><img src="img/numbers-06" alt="image six"/></a></div>           <div class="mix charity" data-myorder="4"><a href=""><img src="img/numbers-09" alt="image nine"/></a></div>           <div class="mix music" data-myorder="5"><a href=""><img src="img/numbers-12" alt="image twelve"/></a></div>           <div class="mix drama" data-myorder="6"><a href=""><img src="img/numbers-01" alt="image one"/></a></div>           <div class="mix music" data-myorder="5"><a href=""><img src="img/numbers-12" alt="image twelve"/></a></div>           <div class="mix charity" data-myorder="5"><a href=""><img src="img/numbers-12" alt="image twelve"/></a></div>            <div class="gap"></div>           <div class="gap"></div>          </div>         </section>  {% endblock content %} 

finally main.twig file;

<!doctype html> <html>   <head>     {% block head %}       <meta charset="utf-8">       <title>tom turner - director of photography</title>       <link rel="stylesheet" href="css/normalize.css">       <link href='https://fonts.googleapis.com/css?family=changa+one|open+sans:400,400italic,700,700italic,800' rel='stylesheet' type='text/css'>       <link rel="stylesheet" href="css/main.css">       <meta name="viewport" content="width=device-width, initial-scale=1.0">       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>       <script type="text/javascript" src="js/jquery.mixitup.min.js"></script>       <script type="text/javascript" src="js/main.js"></script>       <script type="text/javascript" src="js/responsivemenu.js"></script>      {% endblock head %}   </head>    <body>     <header>       <a href="index.html" id="logo">         <h1>tom turner</h1>         <h2>director of photography</h2>       </a>       <nav>         {% block nav %}           <ul>             <li><a href="{{ baseurl() }}" class="selected">about</a></li>             <li><a href="{{ siteurl('/portfolio') }}">portfolio</a></li>             <li><a href="{{ siteurl('/clients') }}">clients</a></li>             <li><a href="{{ siteurl('/teaching') }}">teaching</a></li>             <li><a href="{{ siteurl('/contact') }}">contact</a></li>           </ul>         {% endblock nav %}       </nav>     </header>      <div id="wrapper">        {% block content %}       {% endblock content %}      </div>       <footer class="main-footer">        {% block footer %}           <div id="footer-notes">           <p>tom turner - director of photography</p>           <p>&copy; tom turner - rights reserved</p>         </div>        <div id="mayur">           <p>&copy; 2015 website <a href="https//:www.mayurpande.com">mayur pande</a></p>          </div>          <div class="social-media">           <ul>                <li><a href="mailto:tom@allritesreversed.co.uk"><img src="img/mail_circle.png" alt="email logo" /></a></li>               <li><a href="https://www.facebook.com/tom.turner.397501?fref=ts"><img src="img/fbcircle.png" alt="facebook logo" /></a></li>               <li><a href="https://vimeo.com/user6107855"><img src="img/vimeo_circle.png" alt="vimeo logo" /></a></li>               <li><a href="https://twitter.com/intent/tweet?screen_name=mayurpandeuk"><img src="img/twitter_circle.png" alt="twitter logo" /></a></li>              </ul>           </div>        {% endblock footer %}       </footer>        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>       <script type="text/javascript" src="js/jquery.mixitup.min.js"></script>       <script type="text/javascript" src="js/main.js"></script>       <script type="text/javascript" src="js/responsivemenu.js"></script>    </body> </html> 

my htaccess file;

rewriteengine on  # hosts may require use `rewritebase` directive. # if need use `rewritebase` directive, should # absolute physical path directory contains htaccess file. # # rewritebase /  rewritecond %{request_filename} !-d rewritecond %{request_filename} !-f rewriterule ^ index.php [qsa,l] 

your stylesheets relative current url path, when @

/portfolio, /portfolio/main.css. update link elements include site url, or make them relative root, using /main.css (note / @ beginning).

the same applies links in templates, portfolio/{{ data.id }} try point /portfolio/portfolio/{{ data.id }}, again, add site url beginning, or add slash.

if using slim v3, consider using https://github.com/slimphp/twig-view
alternatively v2, use https://github.com/slimphp/slim-views, edit: noticed using this.

it allows name routes, e.g:

$app->get('/portfolio/:id', function ($id) use ($app) {     ...      $app->render('portfolioitem.twig', array(             'data' => $data         )     ); })->name('portfolio.id'); 

and reference urls in template

{{ urlfor('portfolio.id', {"id": 17 }) }} 

this means if ever urls change, links referencing automatically updated @ same time.

you need make sure .htaccess file configured correctly. shown in slim documentation @ http://docs.slimframework.com/routing/rewrite/, should contain

rewriteengine on rewritecond %{request_filename} !-f rewriterule ^ index.php [qsa,l] 

issue 2

you running query results array of results in $data, trying access first result using {{ data.id }}, first, need loop on data using

{% item in data %}, able use {{ item.id }}. alternatively, first item php $data = $data[0], , pass template.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -