Michael
Michael

Reputation: 93

Need to filter out one row with PHP

I've browsed the website in search of an answer, but nothing that I tried seems to work.

Basically I got my index page with tiles showing a small preview of the items in my database. When I click on a tile, it opens the details page which, at this moment, shows every item from the database and not just the one I've just clicked on.

My question is: how do I adjust the query to only show the relevant explanation for the selected item?

This is my index page code

<?php

    $query="SELECT * FROM werkvorm";
    $result=mysqli_query($link, $query);

    while($row=mysqli_fetch_assoc($result))
    {
        echo "<h2>".$row['w_naam']."</h2>";
        echo "<p>".$row['w_omschrijving']."</p>";
        echo "<p><a href='details.php?id=".$row['w_id']."'>Meer weten?</a></p>";

    }

?>

This is the details page code

<?php

    $query="SELECT * FROM werkvorm";
    $result=mysqli_query($link, $query);

    while($row=mysqli_fetch_assoc($result))
    {
        echo "<h2>".$row['w_naam']."</h2>";
        echo "<p>".$row['w_omschrijving']."</p>";
        echo "<p>Uitvoeringstijd: ".$row['w_uitvoeringstijd']." minuten.</p>";
        $query="SELECT * FROM rol WHERE r_id=".$row['w_rolleerkracht'];
        $rolresult=mysqli_query($link, $query);
        $rolrow=mysqli_fetch_assoc($rolresult);
        echo "<p>Rol van de leerkracht: ".$rolrow['r_omschrijving']."</p>";
        echo "<p>Doelstellingen: ".$row['w_doelstellingen']."</p>";
        $query="SELECT * FROM onderwijsniveau WHERE o_id=".$row['w_onderwijsniveau'];
        $oresult=mysqli_query($link, $query);
        $orow=mysqli_fetch_assoc($oresult);
        echo "<p>Onderwijsniveau: ".$orow['o_omschrijving']."</p>";
        echo "<p>Klasgrootte: ".$row['w_klasgrootte']."</p>";
        $query="SELECT * FROM attribuut WHERE a_id=".$row['w_benodigdheden'];
        $aresult=mysqli_query($link, $query);
        $arow=mysqli_fetch_assoc($aresult);
        echo "<p>Benodigheden: ".$arow['a_omschrijving']."</p>";
        echo "<p>Voorbereidingstijd: ".$row['w_voorbereidingstijd']." minuten.</p>";
        echo "<p>Voorbereiding: ".$row['w_voorbereiding']."</p>";
        echo "<p>Visuele ondersteuning: ".$row['w_visueleuitleg']."</p>";                           
    }

?>

Upvotes: 2

Views: 160

Answers (1)

giorgio
giorgio

Reputation: 10202

On the details page, use this query:

$query = 'SELECT * FROM `werkvorm` WHERE `w_id`='. intval($_GET['id']);

But be aware, this is VERY unsafe as it is now. Use pdo prepared statements or some library to prevent sql injection. There is an excellent SO answer about the matter. Please look into this, and make it a habit to use the mysqli prepared statements or pdo prepared statements for ANY sql query you'd like to execute.

Upvotes: 1

Related Questions