Solved

How can I display MySQL query results with the fields vertical instead of horizontal?

Posted on 2008-10-26
5
2,624 Views
Last Modified: 2013-12-13
I have a MySQL Table with many fields that will not fit on my page when I display query results in a browser, I need to flip the table so that the Fields are displayed in the vertical axis instead of the horizontal.

Google is no use, and I've search through the archive here and can't find a solution.

My query and my Table code is posted below.

Thanks in advance for your help!
$maxRows_hostdetail = 10;
$pageNum_hostdetail = 0;
if (isset($_GET['pageNum_hostdetail'])) {
  $pageNum_hostdetail = $_GET['pageNum_hostdetail'];
}
$startRow_hostdetail = $pageNum_hostdetail * $maxRows_hostdetail;
 
mysql_select_db($database_webhosting, $webhosting);
$query_hostdetail = "SELECT `Host`, Package, Storage, `Data Transfer`, Bandwidth, `Email Accounts`, `IP Addresses`, `Included Domains`, `Sub Domains`, `Hostable Domains`, `FTP Accounts`, `Domain Price`, `MySQL Databases`, `Shared SSL`, `Dedicated SSL`, UpTime, `Money Back Guarantee`, Support, Setup, Price FROM hostfeatures";
$query_limit_hostdetail = sprintf("%s LIMIT %d, %d", $query_hostdetail, $startRow_hostdetail, $maxRows_hostdetail);
$hostdetail = mysql_query($query_limit_hostdetail, $webhosting) or die(mysql_error());
$row_hostdetail = mysql_fetch_assoc($hostdetail);
 
if (isset($_GET['totalRows_hostdetail'])) {
  $totalRows_hostdetail = $_GET['totalRows_hostdetail'];
} else {
  $all_hostdetail = mysql_query($query_hostdetail);
  $totalRows_hostdetail = mysql_num_rows($all_hostdetail);
}
$totalPages_hostdetail = ceil($totalRows_hostdetail/$maxRows_hostdetail)-1;
 
 
```````````````````````````
 
</table>
 
 
 
 
<table width="1000" border="0" align="center">
  <tr>
    <td>Host</td>
    <td>Package</td>
    <td>Storage</td>
    <td>Data Transfer</td>
    <td>Bandwidth</td>
    <td>Email Accounts</td>
    <td>IP Addresses</td>
    <td>Included Domains</td>
    <td>Sub Domains</td>
    <td>Hostable Domains</td>
    <td>FTP Accounts</td>
    <td>Domain Price</td>
    <td>MySQL Databases</td>
    <td>Shared SSL</td>
    <td>Dedicated SSL</td>
    <td>UpTime</td>
    <td>Money Back Guarantee</td>
    <td>Support</td>
    <td>Setup</td>
    <td>Price</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_hostdetail['Host']; ?></td>
      <td><?php echo $row_hostdetail['Package']; ?></td>
      <td><?php echo $row_hostdetail['Storage']; ?></td>
      <td><?php echo $row_hostdetail['Data Transfer']; ?></td>
      <td><?php echo $row_hostdetail['Bandwidth']; ?></td>
      <td><?php echo $row_hostdetail['Email Accounts']; ?></td>
      <td><?php echo $row_hostdetail['IP Addresses']; ?></td>
      <td><?php echo $row_hostdetail['Included Domains']; ?></td>
      <td><?php echo $row_hostdetail['Sub Domains']; ?></td>
      <td><?php echo $row_hostdetail['Hostable Domains']; ?></td>
      <td><?php echo $row_hostdetail['FTP Accounts']; ?></td>
      <td><?php echo $row_hostdetail['Domain Price']; ?></td>
      <td><?php echo $row_hostdetail['MySQL Databases']; ?></td>
      <td><?php echo $row_hostdetail['Shared SSL']; ?></td>
      <td><?php echo $row_hostdetail['Dedicated SSL']; ?></td>
      <td><?php echo $row_hostdetail['UpTime']; ?></td>
      <td><?php echo $row_hostdetail['Money Back Guarantee']; ?></td>
      <td><?php echo $row_hostdetail['Support']; ?></td>
      <td><?php echo $row_hostdetail['Setup']; ?></td>
      <td><?php echo $row_hostdetail['Price']; ?></td>
    </tr>
    <?php } while ($row_hostdetail = mysql_fetch_assoc($hostdetail)); ?>
</table>

Open in new window

0
Comment
Question by:betch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22806879
You can do it in several ways, you could load the result set into an array, transpose the array and iterate through the new array and print it out.

Or you could use a nested table to display the results, but i'd think the above option is faster. I found this transpose function online: http://www.weberdev.com/get_example-4718.html
0
 
LVL 8

Accepted Solution

by:
fcardinaux earned 500 total points
ID: 22806932
Here is how you could transpose the array:

<?php
$maxRows_hostdetail = 10;
$pageNum_hostdetail = 0;
if (isset($_GET['pageNum_hostdetail'])) {
  $pageNum_hostdetail = $_GET['pageNum_hostdetail'];
}
$startRow_hostdetail = $pageNum_hostdetail * $maxRows_hostdetail;
 
mysql_select_db($database_webhosting, $webhosting);
$query_hostdetail = "SELECT `Host`, Package, Storage, `Data Transfer`, Bandwidth, `Email Accounts`, `IP Addresses`, `Included Domains`, `Sub Domains`, `Hostable Domains`, `FTP Accounts`, `Domain Price`, `MySQL Databases`, `Shared SSL`, `Dedicated SSL`, UpTime, `Money Back Guarantee`, Support, Setup, Price FROM hostfeatures";
$query_limit_hostdetail = sprintf("%s LIMIT %d, %d", $query_hostdetail, $startRow_hostdetail, $maxRows_hostdetail);
$hostdetail = mysql_query($query_limit_hostdetail, $webhosting) or die(mysql_error());
$row_hostdetail = mysql_fetch_assoc($hostdetail);
 
if (isset($_GET['totalRows_hostdetail'])) {
  $totalRows_hostdetail = $_GET['totalRows_hostdetail'];
} else {
  $all_hostdetail = mysql_query($query_hostdetail);
  $totalRows_hostdetail = mysql_num_rows($all_hostdetail);
}
$totalPages_hostdetail = ceil($totalRows_hostdetail/$maxRows_hostdetail)-1;
 
$values = array();
while ($row_hostdetail = mysql_fetch_assoc($hostdetail)) {
    if ( ! $values) {
        foreach ($row_hostdetail as $key => $value) $values[$key] = array($value);
    } else {
        foreach ($row_hostdetail as $key => $value) $values[$key][] = $value;
    }    
}
?> 
 
<table width="1000" border="0" align="center">
 
<?php
 
foreach ($values as $label => $row) {
    echo "<tr><th>$label</th>";
    foreach ($row as $value) echo "<td>$value</td>";
    echo '</tr>';
}
 
?>
 
</table>

Open in new window

0
 

Author Closing Comment

by:betch
ID: 31618371
Thats awesome, I was getting a bit stuck trying to work out ho to put my query into an array.
Cheers fcardinaux
0
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22807478
You're welcome.

I'd recommend you modify the solution I suggested you in order to avoid any problem with HTML special characters/tags. You should escape any variable you pass to HTML, for instance using htmlentities():
<?php
 
foreach ($values as $label => $row) {
    echo '<tr><th>' . htmlentities($label) . '</th>';
    foreach ($row as $value) echo '<td>' . htmlentities($value) . '</td>';
    echo '</tr>';
}
 
?>

Open in new window

0
 

Author Comment

by:betch
ID: 22807669
Thanks fcardinaux noted, but I'm having trouble formatting the data
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question