Hallo,

Ik hen een tabel1 deze kan ik ophalen en tonen op het scherm. Ik heb vervolgend ook een andere tabe2 waarin ik het id van tabel1 heb opgenomen.
ik wil nu de gegevens uit tabel1 tonen met het aantal waarvan het id voorkomt in tabel2.

dus zo

ditiseennaamintabel1 (2)

De 2 is omdat de id van ditiseennaamintabel1 2 keer voorkomt in tabel2.

Dit is wat ik heb. (let niet op de inline css... dat ga ik opruimen)

<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



$sql = "SELECT id, naam FROM producten";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row


    while($row = $result->fetch_assoc()) {

        if( $i >= 8 ){ $i = 0; echo '<div style="clear:both;margin-bottom:10px;"></div>'; }
        echo '
        <a style="color:#000000;text-decoration:none" href="icondetail.php?id='.$row["id"]. '">
        <div align="center" style="width:150px;height:100px;background-color:#f4f4f4;float:left;margin-left:10px;padding-top:20px;">
        '.$num_rows.'
         <img style="width:75px;" src="producten/' . $row["naam"]. '.svg" /><br>
         <small>' . $row["id"]. '' . $row["naam"]. '' . $row["omschrijving"]. '</small>
        </div></a>';
         $i++;
    }
} else {
    echo "0 results";
}
$conn->close();
?>

Komen alle producten uit tabel #1 voor in tabel #2 zou je een COUNT en JOIN i.c.m. een GROUP BY kunnen gebruiken.

Als niet gegarandeerd is dat alle producten uit tabel #1 voorkomen in tabel #2 is een LEFT JOIN wellicht veiliger, omdat je dan altijd alle producten te zien krijgt uit tabel #1, ook als deze dus niet voorkomen in tabel #2.

Abstract wordt het dus zoiets:
SELECT t1.*, COUNT(t2.id) AS aantal
FROM tabel1 t1
LEFT JOIN tabel2 t2 ON (t2.id_tabel1 = t1.id)
GROUP BY t1.id

Reageren