简体   繁体   English

GROUP_Concat 超链接

[英]GROUP_Concat Hyperlink <a>

So I have in my database a table called "Clients" and an other one called "Pets" .所以我的数据库中有一个名为"Clients"的表和另一个名为"Pets" They are linked by an associative table which means that a "Clients" can have two "Pets".它们由关联表链接,这意味着"Clients"可以有两个“宠物”。

I am using this SQL Query to display many pets for one client :我正在使用此 SQL 查询为一个客户显示许多宠物:

SELECT *, 
GROUP_CONCAT(DISTINCT petName ORDER BY petName) AS petsNames
FROM clients
LEFT JOIN owners ON owners.idClients = clients.idClients
LEFT JOIN pets ON pets.idPets = owners.idPets
LEFT JOIN clientsdocs ON clientsdocs.idClients = clients.idClients
WHERE clients.idClients='$id'

And I am using this foreach loop :我正在使用这个 foreach 循环:

foreach ($files as $file) :
echo $file['address'];
endforeach

My issue is the following :我的问题如下:

<strong>Pets : </strong> <a href="?profilePet=<?= $file['idPets']; ?>"> <strong><?= $file['petsNames']; ?> </strong> </a>

I am using this line of code to print the client's pets but I want them to be hyperlinked, Which means that I would be able to Click on the differents pets.我正在使用这行代码来打印客户的宠物,但我希望它们被超链接,这意味着我可以点击不同的宠物。 The problem is that GROUP_Concat linked all of the pets which mean that when I click on one pet I always go to the first written.问题是 GROUP_Concat 链接了所有的宠物,这意味着当我点击一个宠物时,我总是转到第一个被写入的宠物。

在此处输入图片说明

As you can see, the pet "Penelope" and "W" are linked by " GROUP_COncat" .如您所见,宠物“Penelope”和“W”由“ GROUP_COncat"链接。 So when I click on it, it sends me to the first pet (Penelope).所以当我点击它时,它会把我送到第一只宠物(佩内洛普)。

You can split your petnames about comma and add a tags to each of it :您可以分割你的petnames约逗号,并添加a标签给每个一下:

$pets = explode(',',$file['petsNames']);
foreach($pets as $pet){
echo '<a>'.$pet.'</a>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM