Jump to content

sql distanct and group by in table


hyster

Recommended Posts

 I have a database with 2 columns, player - file

PLAYER    -  FILE
hyster - ussr-Object263.png 
hyster - ussr-Object268.png 
merc   - germany-E-100.png 
merc   - germany-JagdPz_E100.png 

what I want to do is create a horizontal table like this

<table>
<tr>
            <td>hyster</td><td> ussr-Object263.png 
                                ussr-Object268.png </td>
  </tr>
  <tr> 
<td>merc </td><td> germany-E-100.png germany-JagdPz_E100.png</td> 
</tr> 
</table>

the file part is going to be an image,

I can do it vertical by running 2 query's but I will be getting up to 30 names so to many for a vertical system.

 

its the method im stuck on so any pointers in the way to do this please ??

 

all the code I have so far.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
 
 // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
 } 

$sql = "SELECT * FROM players GROUP BY player,file";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '' . $row["player"]. '<IMG src="test_files/' . $row["file"]. '"><br>';
    }
} else {
    echo "0 results";
}
 $conn->close();
?>
Link to comment
Share on other sites

What does this output?

$sql = "SELECT * FROM players GROUP BY player";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_array()) {
echo '<pre>' . print_r($row, true) . '</pre>';
}
} else {
echo "0 results";
}
$conn->close();

Link to comment
Share on other sites

output is below

Array
(
    [0] => fdg
    [player] => fdg
    [1] => ussr-Object263.png
    [file] => ussr-Object263.png
)

Array
(
    [0] => hyster
    [player] => hyster
    [1] => china-Ch19_121.png
    [file] => china-Ch19_121.png
)

Array
(
    [0] => merc
    [player] => merc
    [1] => germany-E-100.png
    [file] => germany-E-100.png
)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.