首页 > 解决方案 > 如何在数据表中将 group_concat() 列显示为一列?

问题描述

我正在按数据表显示表格。所以group_concat()我的表中有一个来自数据库的列,其中包含多个图像文件。我想将其显示datatable为单列。当我给出正常语法时,它不起作用。有人帮忙吗?

我越来越像这样

我得到什么

但我想要这样的东西,图片栏中的所有多张图片

期望的

这是我为显示数据表提供的脚本代码

 <script type="text/javascript">

    $(document).ready(function() {
        $('#dis').dataTable( {
        "lengthMenu": [[2,5,8,10, 25, 50, -1], [2,5,8,10, 25, 50, "All"]]

    } );
    } );
</script>

这是 gettiing 表的 mysql 查询:

<?php
$conn =mysqli_connect("localhost", "root", "", "project");

$sql="SELECT product.*,images.*, group_concat(images.image) as imag FROM product INNER JOIN images on product.ppid=images.pid group by product.ppid";
?>

这是表的详细信息:

<table id="dis" class="display" style="width:100%">
    <thead>
    <tr>  
<th>ID</th>  
<th>Name</th>
<th>Product Description</th>  
<th>Category</th>  
<th>Subcategory</th>
<th>Images</th>
<th>Edit</th>
</tr>  
</thead>
<?php

$result = mysqli_query($conn, $sql);
    foreach($result as $row)
{
    ?>
    <tr>
        <td><?php echo $row['ppid'];?></td>
        <td><?php echo $row['pname'];?></td>
        <td><?php echo $row['prodes'];?></td>
        <td><?php echo $row['cat'];?></td>
        <td><?php echo $row['sucat'];?></td>

<?php
foreach(explode(',',$row['imag']) as $url)
 {?>

   <td>
    <?php  $imageURL = 'pictures/'.$url;?>
   <img src="<?php echo $imageURL; ?>" alt="" width="100" height="100" /><br>
      <?php $t= $row['id'];?>
    <a href="dele.php?id=<?php echo $t ; ?>">Delete </a> &nbsp;&nbsp;
</td>

我已经包含了所有的脚本,css链接......所以我怎么能把它作为一个数据

标签: phphtmlmysqlajaxdatatables

解决方案


请试试这个:在你的代码中你<td>foreach循环中重复。

您必须将<td>标签放在 foreach 循环之外

<td>  
    <?php
    $img_arr = explode(',',$row['imag']);
    if(!empty($img_arr)){
        foreach($img_arr as $url)
        {  
            $imageURL = 'pictures/'.$url;
            $t= $row['id'];?>
            <img src="<?php echo $imageURL; ?>" alt="" width="100" height="100" /><br>
            <a href="dele.php?id=<?php echo $t ; ?>">Delete </a> &nbsp;&nbsp;
            <?php 
        }   
    }
    ?>
</td>

推荐阅读