Jump to content

Display records in a table format dynamically


Go to solution Solved by Barand,

Recommended Posts

Not able to display the result to end-user as requested format in table form.

Here are my tables

maintab table contains,


dataset contains,


Now , user needs a display like this




Here Result and Method name(m1,m2...) are static.

I tried like this

$sel = $con->prepare("SELECT m.id,
                      FROM maintab m
                      INNER JOIN dataset d
                      ON m.id=d.mid");

<table class="table">
           <?php foreach($sel as $r1)
          <td><?php echo date('d-m-Y', strtotime($r1['edate'])); ?></td>
          <td><?php echo $r1['result']; ?></td>
          <td><?php echo $r1['nos']; ?></td>
           <?php } ?>

Couldn't achieve the desired display. I got something like this



Not getting where exactly i need to break <td> or i need to change my query. 

Can somebody please suggest?

Link to comment
Share on other sites

  • Solution

Do you mean something like this?

// get the "name" headings that you need for the columns
// and also use them as keys in a "template" array
$res = $db->query("SELECT DISTINCT name
                   FROM dataset
                   ORDER BY name
$names = $res->fetchAll();
$heads = array_column($names, 'name');
$temp = array_fill_keys($heads, '');
$table_header  = "<tr><td></td><td class='thead'>Result</td><td class='thead'>" . join("</td><td class='thead'>", $heads) . "</td></tr>\n";

//  now get the data
//  store in an array by "id"
//  witd subarrays for each name

$res = $db->query("SELECT 
                         , edate
                         , result
                         , name
                         , nos
                    FROM maintab m 
                         JOIN dataset d ON m.id = d.mid
                    ORDER BY id       
$data = [];
foreach ($res as $r) {
    if (!isset($data[$r['id']])) {
        $data[$r['id']] = [ 'edate' => $r['edate'],
                            'result' => $r['result'],
                            'names' => $temp                 // the template array from earlier
    $data[$r['id']]['names'][$r['name']] = $r['nos'];         // put value in tempate array

// now we simply output data array into html table rows

$tdata = '';
foreach ($data as $row) {
    $tdata .= "<tr><td>{$row['edate']}</td><td>{$row['result']}</td><td>" . join('</td><td>', $row['names']) . "</td></tr>\n";
<style type='text/css'>
    td {
        padding: 4px 10px;
    .thead {
        font-weight: 600;
        border-top: 1px solid gray;
        border-bottom: 1px solid gray;
        <?= $table_header ?>
        <?= $tdata ?>



[edit] PS Sorry about the data typo. That's what happens when people post pictures instead of copyable text.

Edited by Barand
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

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

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.