extremeshannon Posted February 22, 2015 Share Posted February 22, 2015 Hello everyone, I am trying to display my Columns from the top down. I want my Column names on the left and the data moving to the right. Column Record1 Redcord2 Record3 C1 Data Data Data C2 Data Data Data C3 Data Data Data C4 Data Data Data C5 Data Data Data This what I am using and it is traditional left to right output with each record below the next. if (!$result) { $message = 'ERROR:' . mysql_error(); return $message; } else { $i = 0; echo '<html><body><table><tr>'; while ($i < mysql_num_fields($result)) { $meta = mysql_fetch_field($result, $i); echo '<td>' . $meta->name . '</td>'; $i = $i + 1; } echo '</tr>'; $i = 0; while ($row = mysql_fetch_row($result)) { echo '<tr>'; $count = count($row); $y = 0; while ($y < $count) { $c_row = current($row); echo '<td>' . $c_row . '</td>'; next($row); $y = $y + 1; } echo '</tr>'; $i = $i + 1; } echo '</table></body></html>'; mysql_free_result($result); } mysql_close ($link); ?> Thanks for the help Shannon Quote Link to comment Share on other sites More sharing options...
ChristopherJCrandall Posted February 22, 2015 Share Posted February 22, 2015 to do that you would have to setup a different while statement for each column. you will be able to put the columns in any order you want. if you need an example let me know Christopher J. Crandall Quote Link to comment Share on other sites More sharing options...
extremeshannon Posted February 22, 2015 Author Share Posted February 22, 2015 Thanks for the reply. I have been trying to figure out how to accomplish this with the least amount of code. If you could post an example that would be awesome. Thanks Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 22, 2015 Share Posted February 22, 2015 you would 'pivot' the data, i.e. swap the rows and columns - // retrieve the data, swapping rows/columns $pivot = array(); while($row = a_fetch_assoc_statement()){ foreach($row as $key=>$value){ $pivot[$key][] = $value; } } // display the data echo "<table>"; foreach($pivot as $key=>$row){ echo "<tr><th>$key</th><td>"; // the row heading echo implode('</td><td>',$row); // the data in the row echo "</td></tr>\n"; } echo "</table>"; the a_fetch_assoc_statement() in the above would be replaced with the appropriate statement from the database library of functions you are using. however, the mysql_ functions are obsolete and you should be using either PDO or msyqli_ statements. Quote Link to comment Share on other sites More sharing options...
extremeshannon Posted February 22, 2015 Author Share Posted February 22, 2015 Thank you and yes I am looking to pivot Shannon Quote Link to comment 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.