How to output a row from Table B based on a value in a row for Table A for every value of table A (SQL in PHP) [duplicate]

3 weeks ago 25
ARTICLE AD BOX

So I have a table with 2 columns, parent ID and course ID. there is also a table with several columns, the course table, which has course ID as a column. I can isolate the current parent ID based on the log in data. for every row in the first table matching the current parent ID, I want to output the relevant course data via the course ID associated. a parent ID can have many course IDs linked to it. I am programming in PHP linked to a HTML site, and my tables are in SQL.

<?php include 'pHeader.php'?> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>University</title> <style> .university-grid { display: table-column; flex-wrap: wrap; gap: 0px; justify-content: space-between; padding: 0px; } .university-item { background-color: #009EBA; border: 10px solid #01435C; border-radius: 8px; padding: 15px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); flex: 1 1 calc(25% - 20px); box-sizing: content-box; display: flex; flex-direction: column; text-align: center; } .university-item img { max-width: 100%; height: auto; border-radius: 8px; } .university-item h2{ font-size: 1.5cm; margin: 10px 10px; } .university-item p{ font-size: 1cm; margin:10px 10px; } .university-item .grade-button-container{ margin-top:auto; } .university-item .price{ font-size: 1.2cm; color: #333; margin:10px 0; } .university-item button { background-color: #e74c3c; color: #fff; border: none; border-radius: 5px; padding: 10px 15px; font-size: len; cursor: pointer; } .university-item button:hover{ background-color: #c0392b; } </style> </head> <body> <div class="container"> <div class = "university-grid"> <?php $sql = "SELECT Variable FROM variables WHERE Code = 'ID'"; $result = $connection->query($sql); $row = $result->fetch_assoc(); $ParentID = $row['Variable']; $sql = "SELECT CourseID FROM parent_university_courses WHERE ParentID = '$ParentID'"; $result = $connection->query($sql); //$row = $result->fetch_assoc(); //$CourseID = $row['CourseID']; //Fetch course data from the database //$sql = "SELECT SubjectName, Description, Images, GradeRequirement, UCASRequirement FROM university_courses WHERE CourseID='$CourseID'"; //$result = $connection->query($sql); if($result->num_rows > 0) { //Output data of each row while($row = $result->fetch_assoc()) { $CourseID = $row['CourseID']; echo '<div class="university-item">'; $sql = "SELECT SubjectName, Description, Images, GradeRequirement, UCASRequirement FROM university_courses WHERE CourseID='$CourseID'"; $result = $connection->query($sql); echo '<img src="../Images/PNG/' . $row["Images"] . 'jpg" alt-"' . $row["SubjectName"] . '">'; echo '<h2>' . $row["SubjectName"] . '</h2>'; echo '<p>' . $row["Description"] . '</p>'; echo '<p>' . $row["CourseID"] . '</p>'; echo '<p>Grade requirement: ' . $row["GradeRequirement"] . '</p>'; echo '<p>UCAS requirement: ' . $row["UCASRequirement"] . '</p>'; echo '</div>'; echo '</div>'; } } else { echo "0 results"; } $connection->close(); ?> </div> </div> </body> </html> <?php include 'footer.php'?>
Read Entire Article