简体   繁体   中英

Display column value as table header rather than its original one

I got a database in mySQL, let says "Storage" with a table in it called "Storage_Det". Here is the content of "Storage_Det":

valueid | formid |  submissionid |  fieldname  |  fieldvalue
--------+--------+---------------+-------------+-------------
1       | 1      |  1            |  name       |  Alex
2       | 1      |  1            |  position   |  Manager
3       | 1      |  1            |  room       |  3-10
4       | 1      |  2            |  name       |  Ben
5       | 1      |  2            |  position   |  Accountant
6       | 1      |  2            |  room       |  2-05
7       | 1      |  3            |  name       |  Denny
8       | 1      |  3            |  position   |  Marketing
9       | 1      |  3            |  room       |  1-03

There's no problem for me to display it with php. My problem is: I want to display "name", "position", and "room" for my new table view so that it will show like this:

submissionid | name   | position   | room |
-------------+--------+------------+------+
1            | Alex   | Manager    | 3-10 |
2            | Ben    | Accountant | 2-05 |
3            | Denny  | Marketing  | 1-03 |

Since I am a newbie here, I need your help. Please let me know. Thanks.

Okay, I think I must add a few details before to make my problem clear enough.

The "table number 2" is NOT A REAL TABLE. It is GENERATED from data stored in table number 1. As you can see there in "table number 1" data that 'name', 'position', and 'room' will be "table header" in "table number 2". I hope that was clear enough.

Now that I better understand your question here is one way:

CREATE TEMPORARY TABLE  `table2` (
`submissionid` INT NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL ,
`position` VARCHAR( 50 ) NULL ,
`room` VARCHAR( 50 ) NULL
) ENGINE = MYISAM ;

ALTER TABLE  `table2` 
ADD UNIQUE (
`submissionid` ,
`name` ,
`position` ,
`room`
);

insert table2 (submissionid, name) select submissionid, fieldvalue from table1 where fieldname='name'; 
update table2 set position = (select fieldvalue from table1 where fieldname='position' and table1.submissionid = table2.submissionid); 
update  table2 set room = (select fieldvalue from table1 where fieldname='room' and table1.submissionid = table2.submissionid);   


select * from table2;

First, group all of your data for a certain submissionid like so (assuming the very basic mysql API):

$sIdGroup = array();
while ( $row = mysql_fetch_assoc( $result ) )
{
    if ( !isset( $sIdGroup[ $row[ "submissionid" ] ] ) )
    {
        $sIdGroup[ $row[ "submissionid" ] ] = array();
    }

    $sIdGroup[ $row[ "submissionid" ] ][ $row[ "fieldname" ] ] = $row[ "fieldvalue" ];  // Save the value for the current field
}

Now display, first the column names:

echo( '<table><tr><td>submissionid</td>' );

// Get the first row in $sIdGroup (i'm assuming 1 is not always the first index)

reset( $sIdGroup );
$firstSubmissionId = key( $sIdGroup );

foreach ( $sIdGroup[ $firstSubmissionId ] as $key => $dummy )
{
    echo( "<td>$key</td>" );
}

Now the data:

echo( '</tr>' );

foreach ( $sIdGroup as $submissionId => $data )
{
    echo( "<tr><td>$submissionId</td>" );
    foreach ( $data as $key => $value )
    {
        echo( "<td>$value</td>" );
    }
    echo( '</tr>' );
}

echo( '</table>' );

If you want a query to give you the data displayed how you've requested, you could use the following:

SELECT DISTINCT sd.SubmissionId
   , sdName.FieldValue AS Name
   , sdPosition.FieldValue AS Position
   , sdRoom.FieldValue AS Room
FROM Storage_Det AS sd
LEFT JOIN Storage_Det AS sdName ON sd.SubmissionId = sdName.SubmissionId
   AND sdName.FieldName = 'Name'
LEFT JOIN Storage_Det AS sdPosition ON sd.SubmissionId = sdPosition.SubmissionId
   AND sdPosition.FieldName = 'Position'
LEFT JOIN Storage_Det AS sdRoom ON sd.SubmissionId = sdRoom.SubmissionId
   AND sdRoom.FieldName = 'Room'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM