skybeta Posted May 25, 2010 Share Posted May 25, 2010 I have a table from SQL that has 3 columns with following datas: A B C 1 2 10 1 3 12 2 1 20 2 4 30 3 1 20 4 6 22 4 5 14 I would want to create table to display the data like this: A is the row and B is the column, and C is the value of A match B B A 1 2 3 4 5 6 1 10 12 2 20 30 3 20 4 14 22 What will be the best way to do it? Should I create an array? IF so, grateful if you can teach me how to do it! Thx in advance! Quote Link to comment https://forums.phpfreaks.com/topic/202833-how-to-create-this-matrix-table/ Share on other sites More sharing options...
Daniel0 Posted May 25, 2010 Share Posted May 25, 2010 Untested: $matrix = array(); foreach ($rows as $row) { if (is_array($matrix[$row['A']])) $matrix[$row['A']] = array(); $matrix[$row['A']][$row['B']] = $row['C']; } Quote Link to comment https://forums.phpfreaks.com/topic/202833-how-to-create-this-matrix-table/#findComment-1063032 Share on other sites More sharing options...
skybeta Posted May 25, 2010 Author Share Posted May 25, 2010 thx for ur reply! but sorry im a newbie in this, could you explain a little detail on how this code work? Quote Link to comment https://forums.phpfreaks.com/topic/202833-how-to-create-this-matrix-table/#findComment-1063041 Share on other sites More sharing options...
Daniel0 Posted May 25, 2010 Share Posted May 25, 2010 It loops over the rows and uses the A and B colums for the matrix indices and C for the value. Matrices are usually implemented using 2D arrays. Quote Link to comment https://forums.phpfreaks.com/topic/202833-how-to-create-this-matrix-table/#findComment-1063058 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.