Jump to content

Columns


Andrej_Petras

Recommended Posts

Hi,

 

I need help for printing table from MySQL in PHP.

i need this:        1 2 3
                          4 5 6
                          7 8 9

no this:    1
                2
                3
                4

 

If someone has code for that i would appreciate it.

:D 

Edited by Andrej_Petras
Link to comment
Share on other sites

There are many ways to accomplish this.  What approach have you attempted so far?

  • HTML tables
  • Flexbox
  • Grid

One fundamental technique for procedural programming, when looping through a set of data, is to utilize the modulus operator.   

By checking the remainder of division by the number of items you want per row, you can determine whether or not to start a new line, or whether a new line was just started.

Consider this code that has a simulated result set of rows:

<?php

define('PER_ROW', 3);

$resultset[] = array('id' => 3, 'name' => 'Bob');
$resultset[] = array('id' => 5, 'name' => 'Mark');
$resultset[] = array('id' => 9, 'name' => 'Sam');
$resultset[] = array('id' => 10, 'name' => 'Linda');
$resultset[] = array('id' => 11, 'name' => 'Amy');
$resultset[] = array('id' => 75, 'name' => 'John');
$resultset[] = array('id' => 20, 'name' => 'Aaron');
$resultset[] = array('id' => 19, 'name' => 'Ringo');
$resultset[] = array('id' => 2, 'name' => 'Paul');
$resultset[] = array('id' => 99, 'name' => 'George');



foreach ($resultset as $key => $row) {
    $key++;
    
    if ($key % PER_ROW === 1) {
        // First item in row
        echo "| ";
    }
    echo "{$row['id']}: {$row['name']} | ";
    
    if ($key % PER_ROW === 0) {
        // Last item in row
        echo PHP_EOL;
    }
}

echo PHP_EOL;

 

You can play with this example here: https://3v4l.org/lD83M

Changing the PER_ROW constant to another number like 4 for example, should help you see how this common technique works.

Link to comment
Share on other sites

And, for good measure, I will provide another option: array_chunk()

$columns = 3;

//Put results into an array
$results = $pdo->fetchAll();

echo "<table>";
foreach(array_chunk($results, $columns) as $rowData)
{
    echo "<tr>";
    foreach($rowData as $record) { echo "<td>{$record['name']}</td>";
    echo "</tr>";
}
echo "</table>";

EDIT: Fixed inner TD tags

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.