0

I have two tables:

Clubs

    |---------------------|------------------|
    |          id         |       name       |
    |---------------------|------------------|
    |           1         |      Arsenal     |
    |---------------------|------------------|
    |           2         |      Chelsea     |
    |---------------------|------------------|
    |           3         |      Fulham      |
    |---------------------|------------------|
    |           4         |      Leeds       |
    |---------------------|------------------|

Matches

    |---------------------|------------------|------------------|
    |          id         |       home       |        away      |
    |---------------------|------------------|------------------|
    |           1         |        1         |         3        |
    |---------------------|------------------|------------------|
    |           2         |        2         |         4        |
    |---------------------|------------------|------------------|

Explanation: the numbers in the columns "home" and "away" in the Matches table linking to the the id in de Clubs table.

Now I want to echo (with php) the following:

|---------------------|------------------|
|          home       |       away       |
|---------------------|------------------|
|         Arsenal     |      Fulham      |
|---------------------|------------------|
|         Chelsea     |       Leeds      |
|---------------------|------------------|

Explanation: when clicking on the teamname I want to open the club.php page with the teaminfo.

I've tried the following:

<?php
$sql = "SELECT * FROM matches JOIN clubs ON matches.home AND matches.away = clubs.id"
$rs_result = $conn->query($sql);
?> 

<div style="overflow-x:auto">
<table>
<tr><th><strong>Home</strong></th><th><strong>Away</strong></th><tr>

<?php     
while($row = $rs_result->fetch_assoc()) {
?> 

<tr>
<td><a href="club.php?id=<?php echo $row['home'];?>"><? echo $row['home']; ?></a></td>
<td><a href="club.php?id=<?php echo $row['away'];?>"><? echo $row['away']; ?></a></td>
</tr>

<?php } ?>  
</table></div>

It is echoing the following:

|---------------------|------------------|
|          home       |       away       |
|---------------------|------------------|
|           1         |         3        |
|---------------------|------------------|
|           2         |         4        |
|---------------------|------------------|

My question: How can I show the clubnames in the table but passing the club id in the "a href" link? I've tried $row[1] and so on but it doesnt work. Which SQL statement do I need? JOIN? INNER JOIN? LEFT JOIN?

Many thanks in advance!

1
  • So many tags there... Are you having problems with php, html, sql or echo? Commented Apr 16, 2020 at 12:36

1 Answer 1

1

I believe this should work - I made an adjustment to your query. You can use left joins when you are interested in the left side of the query (matches) and do not care if the clubs have not been populated. F ex if you have a home - away of 1 and 5. We know that 1 is Arsenal but there is no entry for 5 in clubs. Left join will display it, inner join will not.

<?php
$sql = "SELECT home,away,homeclub.name as homename,awayclub.name  as awayname FROM matches 
JOIN clubs as homeclub ON matches.home = homeclub.id
JOIN clubs as awayclub ON matches.away = awayclub.id"
$rs_result = $conn->query($sql);
?> 

<div style="overflow-x:auto">
<table>
<tr><th><strong>Home</strong></th><th><strong>Away</strong></th><tr>

<?php     
while($row = $rs_result->fetch_assoc()) {
?> 

<tr>
<td><a href="club.php?id=<?php echo $row['home'];?>"><? echo $row['homename']; ?></a></td>
<td><a href="club.php?id=<?php echo $row['away'];?>"><? echo $row['awayname']; ?></a></td>
</tr>

<?php } ?>  
</table>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.