Jump to content

Fetch the data from database table column and display in PHP table row wise


Senthilkumar

Recommended Posts

If you had normalized your tables correctly you would already have the data in this format

SELECT id
     , name 
     , month
     , 'Field1' as field
FROM senthil
WHERE field1 = 1
  UNION
SELECT id
     , name 
     , month
     , 'Field2' as field
FROM senthil
WHERE field2 = 1
  UNION
SELECT id
     , name 
     , month
     , 'Field3' as field
FROM senthil
WHERE field3 = 1
  UNION
SELECT id
     , name 
     , month
     , 'Field4' as field
FROM senthil
WHERE field4 = 1
ORDER BY id, field

 

Link to comment
Share on other sites

Alternative php solution

$res = $pdo->query("SELECT id
                         , name 
                         , month
                         , field1
                         , field2
                         , field3
                         , field4
                    FROM senthil
                    ORDER BY id
                   ");
$tdata = '';
foreach ($res as $r)  {
    $flds = array_slice($r,3);
    foreach ($flds as $k => $v)  {
        if ($v)  {
            $tdata .= "<tr><td>{$r['name']}</td><td>{$r['month']}</td><td>$k</td></tr>\n";
        }
    }
}

 

Link to comment
Share on other sites

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.