Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to display this data in table format, like

<table border='1'>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>City</th>
        <th>State</th>
        <th>Phone</th>
    </tr>

But my table, column-wise, is like

userdbelemnts_id     userdbelements_field_name      userdbelements_field_value  

180                  user_first_name                Demo
181                  user_last_name                 Agent
183                  City                           Mumbai
184                  zip                            400000
185                  state                          xyz
189                  phone                          123456

How do i flatten the normalized data for display in the above table structure?

share|improve this question
    
Is the second table a mysql table? is yes, do you want to retrieve the value from the table and display them in the browser? Please be more clear. –  kevin Jun 15 '13 at 12:40
    
What exactly do you mean by 'separating' it? –  Cthulhu Jun 15 '13 at 12:41
    
Do you have a user_id in that table? Is the table called userdbelements? –  Steven Moseley Jun 15 '13 at 12:41
    
Sorry, yes i am retrieving from the database. yes, table is userdbelements. and also column is there called userdb_id –  user2178637 Jun 15 '13 at 12:44
    
Ok, tweaked my answer... that should work for ya –  Steven Moseley Jun 15 '13 at 12:47
add comment

1 Answer

up vote 1 down vote accepted

You could denormalize the data on output in your query, like this (Grouping on userdb_id):

SELECT
    MAX(CASE WHEN userdbelements_field_name = 'user_first_name' 
        THEN userdbelements_field_value ELSE NULL END) AS first_name,
    MAX(CASE WHEN userdbelements_field_name = 'user_last_name' 
        THEN userdbelements_field_value ELSE NULL END) AS last_name,
    MAX(CASE WHEN userdbelements_field_name = 'City' 
        THEN userdbelements_field_value ELSE NULL END) AS city,
    MAX(CASE WHEN userdbelements_field_name = 'state' 
        THEN userdbelements_field_value ELSE NULL END) AS state,
    MAX(CASE WHEN userdbelements_field_name = 'zip' 
        THEN userdbelements_field_value ELSE NULL END) AS zip,
    MAX(CASE WHEN userdbelements_field_name = 'phone' 
        THEN userdbelements_field_value ELSE NULL END) AS phone,
FROM userdbelemnts
GROUP BY userdb_id

Then in your php, just loop through the results as you would in a flat table.

<thead>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>City</th>
        <th>State</th>
        <th>Phone</th>
    </tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
    <tr>
        <td><?=$row['first_name']?></td>
        <td><?=$row['last_name']?></td>
        <td><?=$row['city']?></td>
        <td><?=$row['state']?></td>
        <td><?=$row['phone']?></td>
    </tr>
<?php } ?>
</tbody>

Edit: given your new table schema per comments below:

SELECT
    orodha_en_userdb.*,
    MAX(CASE WHEN userdbelements_field_name = 'user_first_name' 
        THEN userdbelements_field_value ELSE NULL END) AS first_name,
    MAX(CASE WHEN userdbelements_field_name = 'user_last_name' 
        THEN userdbelements_field_value ELSE NULL END) AS last_name,
    MAX(CASE WHEN userdbelements_field_name = 'City' 
        THEN userdbelements_field_value ELSE NULL END) AS city,
    MAX(CASE WHEN userdbelements_field_name = 'state' 
        THEN userdbelements_field_value ELSE NULL END) AS state,
    MAX(CASE WHEN userdbelements_field_name = 'zip' 
        THEN userdbelements_field_value ELSE NULL END) AS zip,
    MAX(CASE WHEN userdbelements_field_name = 'phone' 
        THEN userdbelements_field_value ELSE NULL END) AS phone,
FROM orodha_en_userdbelements
    INNER JOIN orodha_en_userdb
    ON orodha_en_userdbelements.userdb_id = orodha_en_userdb.userdb_id
WHERE orodha_en_userdb.userdb_id = $id
GROUP BY orodha_en_userdb.userdb_id
share|improve this answer
    
actually i am getting the data from 2 tables , my query is like this $sql="SELECT * FROM orodha_en_userdbelements, orodha_en_userdb WHERE orodha_en_userdbelements.userdb_id = orodha_en_userdb.userdb_id AND userdb_id = '".$q."'";, so for that not getting how to add this –  user2178637 Jun 15 '13 at 12:53
    
sorry sorry, I got it. I made it!! Thanks a lot Steven Moseley –  user2178637 Jun 15 '13 at 12:57
    
Updated the query to show how to group it with a join on another table. In the future, try to post your question with your actual table structure and problem you're trying to solve. –  Steven Moseley Jun 15 '13 at 12:58
    
sure. I will take care of that in my next post. Thank you once again –  user2178637 Jun 15 '13 at 13:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.