0

PhpMyAdmin

Looking for output like this:

PHOTO 2

In Mysql/PHP, how can I get output like this, or similar (photo 2)?

$start = mysqli_query($conn, $sql);

if (mysqli_num_rows($start) > 0) {
    $row = mysqli_fetch_assoc($start);

    while($row = mysqli_fetch_assoc($start)) {
        echo '<p name='.$row['game'].'>'.$row['game'].'</p>' ;
    }
}
7
  • Can you please tell me what is inside $sql ?
    – Ashish
    Commented Jul 8 at 1:43
  • @Jan Klan This is the result of already publishing this question. It got downvoted, got a comment asking for the query, an answer that only explains how to generate a HTML table, and nothing about handling the database or any real problems the user may have. Since you created the HTML answer, I now understand why you published the question. Commented Jul 8 at 6:28
  • Are you struggling with the PHP part, the MySQL part, or the HTML part? How is your problem even related to phpMyAdmin?
    – Nico Haase
    Commented Jul 8 at 6:55
  • 1
    @JanKlan Yes, the downvote isn't nice. You can still upvote though. There is no real lesson here, other than that you could have been a bit more patient, since HIHAWA already indicated to be willing to improve the question. My main fear was that the question wasn't clear about what HIHAWA was really struggling with. Your answer, when if comes to the HTML part, is fine. As I say: I now understand your reason for publishing it. Maybe the "lesson" should be: Knowing the answer isn't a reason to publish a question, most of us know the answer. The reason should be that it is a good question. Commented Jul 8 at 8:25
  • 1
    Yes, asking good questions is hard, and we, as a community, aren't very good at guiding new users. Stack Overflow can be quite toxic when you first start using it. I think that's why the "staging ground" was created, to shield users from the worst of it, but even asking for corrections can put off new users. Let's see what HIHAWA does with your answer, and if there are still issues that are unresolved. Commented Jul 8 at 8:45

1 Answer 1

1

The key is that the result set must be ordered by the game you want to span across multiple columns.

Then, you need to figure out how many rows should each game cell span for.

Then you have to keep track of which game is the latest one found and either omit the table cell if you've already seen the game, or add a <td colspan="..."> if it's a game mentioned for the first time.

Take a look at the example code

$data = [
    ['id' => 1, 'nickname' => 'abc', 'game' => 'abc', 'checkbox' => 1],
    ['id' => 2, 'nickname' => 'xyz', 'game' => 'zyx', 'checkbox' => 0],
    ['id' => 5, 'nickname' => 'xyz', 'game' => 'abc', 'checkbox' => 1],
    ['id' => 6, 'nickname' => 'afg', 'game' => 'zyx', 'checkbox' => 1],
];

// The data must be sorted by the game, that's the key
$sortedData = [
    ['id' => 1, 'nickname' => 'abc', 'game' => 'abc', 'checkbox' => 1],
    ['id' => 5, 'nickname' => 'xyz', 'game' => 'abc', 'checkbox' => 1],
    ['id' => 2, 'nickname' => 'xyz', 'game' => 'zyx', 'checkbox' => 0],
    ['id' => 6, 'nickname' => 'afg', 'game' => 'zyx', 'checkbox' => 1],
];


// Figure out which game should span how many rows ahead of time
foreach ($sortedData as $row) {
    $gameCounts[$row['game']] ??= 0;
    $gameCounts[$row['game']]++;    
}

echo '<table border="1">';
echo '<thead>';
echo '<tr>';
foreach (array_keys($sortedData[0]) as $header) {
    echo '<th>'.$header.'</th>';
}
echo '</tr>';
echo '</thead>';

echo '<tbody>';
$previousGame = null;
foreach ($sortedData as $row) {
    echo '<tr>';
    foreach ($row as $key => $value) {
        if ($key === 'game') { // The Game column gets a special treatment
            if ($previousGame !== $value) { // The game has changed - span the cell over the correct number of rows
                echo '<td rowspan="'.$gameCounts[$value].'">'.$value.'</td>';
                $previousGame = $value;
            }
            
            continue;
        }
        
        echo '<td>'.$value.'</td>';
    }
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';

it will produce the HTML code you need, assuming your code holds the sorted result of the SQL query in the $sortedData array. See it live,https://3v4l.org/LITPO#v8.3.9

Not the answer you're looking for? Browse other questions tagged or ask your own question.