Search code examples
phphyperlinkgroup-concat

create separate hyperlinks for group_concat and display the results into one table cell


I have the following code, which successfully creates separate link for each of the group_concat objects, but instead of placing them into one table cell, it placing them to separate cells. Can anyone help me? Here is the full code:

<?php
if (isset($search))
{
    $count=mysqli_num_rows($query) ;
    if($count>=1){
        $search_output .= "<hr  color=red />$count results for <strong>$search</strong><hr color=red />";


        while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
             $Title = $row['Title'];
             $Year = $row['Year'];
             $Authors = $row['Authors'];
             $Journal=$row['Journal'];
             $Genes=explode(',', $row['Genes']);
             $Data_Type=$row['Data_Type'];

             echo"<tr>";
             echo "<td>".htmlspecialchars($row['Title'])."</td>";
             echo "<td>".htmlspecialchars($row['Year'])."</td>";
             echo "<td>".htmlspecialchars($row['Authors'])."</td>";
             echo "<td>".htmlspecialchars($row['Journal'])."</td>";

            foreach($Genes as $aGenes){
            echo "<td><a target=\"_blank\" href='http://www.ncbi.nlm.nih.gov/gene/?term=" .$aGenes."'> ".$aGenes." </a> </td>";

            }

             echo "<td>".htmlspecialchars($row['Data_Type'])."</td>";
             echo"</tr>";

        }


    }else{
         $search_output = "<hr />0 results for <strong>$search</strong><hr />";
    }
    echo "$search_output";      

}
?>

Solution

  • I guess the problem is that you are creating separate cells for each link here

    foreach($Genes as $aGenes){
            echo "<td><a target=\"_blank\" href='http://www.ncbi.nlm.nih.gov/gene/?term=" .$aGenes."'> ".$aGenes." </a> </td>";
    
            }
    

    instead of that do this

        echo '<td>';
    foreach($Genes as $aGenes){
                echo "<a target=\"_blank\" href='http://www.ncbi.nlm.nih.gov/gene/?term=" .$aGenes."'> ".$aGenes." </a> ";
    
                }
    echo '</td>';
    

    That is move the td tag out of foreach loop so that they are echoed only once for every element in while loop