简体   繁体   English

GROUP_CONCAT php MySQL 后如何分割图片链接

[英]How to split links of image after GROUP_CONCAT php MySQL

I make GROUP_CONCAT between two tables to collect similar data with LEFT JOIN.我在两个表之间创建 GROUP_CONCAT,以使用 LEFT JOIN 收集相似的数据。

This is old data before GROUP_CONCAT:这是 GROUP_CONCAT 之前的旧数据:

Output:
+-----------+--------+----------+-----------+
|id| name   | Cmpany | POSTID   |ImageTopic |
|1 | John   |  Js    |  1       |1png       |
|1 | John   |  Js    |  1       |2png       |
|1 | John   |  Js    |  1       |3png       |
|1 | John   |  Js    |  1       |4png       |
+-----------+--------+----------+-----------+

New data with GROUP_CONCAT: GROUP_CONCAT 的新数据:

Output:
+---+------+-----------+--------+------------------------+
|id | name |    Cmpany | POSTID |   ImageTopic           |
|1  | John |      Js   |  1     |   1png,2png,3png,4png  |
+---+------+-----------+--------+------------------------+

Now my problem is with Column of Image Topic in new data.现在我的问题是新数据中的图像主题列。 I get link Images in this Column but it's coming without sprit so I can't click it or use it in other place.我在此专栏中获得了链接图像,但它没有精神,所以我无法单击它或在其他地方使用它。

Example image coming like that:示例图像是这样的:

ImageTopic: "https://png.pngtree.com/element_our/20200703/ourlarge/pngtree-butterfly-purple-red-wings-ink-transparent-png-bright-image_2300442.jpg,https://www.picng.com/upload/butterfly/png_butterfly_61701.png,https://www.picng.com/upload/butterfly/png_butterfly_61701.pnghttps://www.picng.com/upload/butterfly/png_butterfly_61700.png"

As you can see above this is not correct format.正如您在上面看到的,这不是正确的格式。

在此处输入图像描述

And If I try to click it I get this:如果我尝试点击它,我会得到这个:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>JF8C24VHE7Z9NKAC</RequestId>
<HostId>+yKClkshmm8kCqYydoRsaPDwjkkM+anF0vQI6nt0eTY6TvCuQ5QDv7hfq//eTxUJ9ApNMNWpPfc=</HostId>
</Error>

My code:我的代码:


<?php
require_once 'con.php';

$id=$_GET['id'];


$sql= "SELECT * FROM topics

LEFT JOIN (SELECT POSTID, GROUP_CONCAT(DISTINCT ImageTopic  ) ImageTopic 
FROM ImagePost GROUP BY POSTID
) ImageTopic ON topics.id = ImageTopic.POSTID


where topics.id=? "
;

$stmt = $con->prepare($sql); 

$stmt->bind_param("s",$id);

$stmt->execute();

$result = $stmt->get_result();

if ($result->num_rows >0) {
 
 
     while($row[] = $result->fetch_assoc()) {
     
     $item = $row;
     
     $json = json_encode($item, JSON_NUMERIC_CHECK);
     
     }
 
} else {

    $json = json_encode(["result" => "No Data Foun"]);
}
 echo $json;

 
$con->close();
 



?>




How I can solve this problem?我该如何解决这个问题?

You can try it here: https://onecompiler.com/mysql/3yhc5dnkh你可以在这里试试: https://onecompiler.com/mysql/3yhc5dnkh

This URL of images I will use it also later in my app.这 URL 张图片我稍后也会在我的应用程序中使用它。

If you further process your data in PHP you can explode them into an array using explode function.如果您进一步处理 PHP 中的数据,您可以使用explode function 将它们分解成一个数组。

$imageTopicArray = explode(',', $imageTopic);
foreach ($imageTopicItem as $imageTopicArray) {
    echo '<img src="'. $imageTopicItem .'" />';
}

This gives you an array of strings and you can print them one by one.这给你一个字符串数组,你可以一个一个地打印它们。

Another option is to use JSON_ARRAYAGG function which is available since MySQL 5.7.22.另一种选择是使用自JSON_ARRAYAGG 5.7.22 起可用的 JSON_ARRAYAGG function。 Instead of GROUP_CONCAT .而不是GROUP_CONCAT This returns you JSON array instead of comma separated values which might be more comfortable in some cases.这将返回 JSON 数组而不是逗号分隔值,这在某些情况下可能更舒服。

Then use json_decode to get the array of items and process them as mentioned above.然后使用json_decode获取项目数组并按上述方法处理它们。

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

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