Jump to content

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


Recommended Posts

Dear Team,

I am having Mysql databse table in the name of fieldtable. My table looks bellow image

image.png.0c809797c592040ad24e1ef8e6966a16.png

I want to fetch the data from this table where the value is 1 and display on my PHP table row wise

image.png.f681245711638f2eea92f9b09906a99b.png

How cani display the data like above picture. Can any one help me this

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

 

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";
        }
    }
}

 

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.