how do i FORMAT OUTPUT from php-mysql database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mervdsz
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2014 12:29 am

how do i FORMAT OUTPUT from php-mysql database

Post by mervdsz »

Hi

i got a php code on internet that works fine. It retrieves details of suppliers from a mysql table "suppliers". All column headings are displayed horizontally.
It retrieves data and displays as shown below:

ID | NAME | ADDRESS | PLACE | PHONE | EMAIL
----------------------------------------------------------------
1 | TOM | XYZ | ABC | 123 | FDS@FDSF.COM


However i would like to format the output to appear as shown below in image file "desired-output.png":
DESIRED OUTPUT.PNG
DESIRED OUTPUT.PNG (4.46 KiB) Viewed 3630 times

I want all my column headings to appear in one column (vertically) as shown above and subsequent columns will contain matching related data about suppliers. Being a newbie im unable to do it.

my table.php file is below:

Code: Select all

<?php
$con=mysqli_connect("127.0.0.1","root","","supplier");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM supplier");

echo "<table class='search' >
<tr>
<th>ID</th>
<th>NAME</th>
<th>ADDRESS</th>
<th>PLACE</th>
<th>PHONE</th>
<th>EMAIL</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['NAME'] . "</td>";
  echo "<td>" . $row['ADDRESS'] . "</td>";
  echo "<td>" . $row['PLACE'] . "</td>";
  echo "<td>" . $row['PHONE'] . "</td>";
  echo "<td>" . $row['EMAIL'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Form Submission</title>
<!-- This is the CSS file for this tutorial -->
<link href="Articlestyles.css" rel="stylesheet" type="text/css">
</head>
</html>

can someone help me and point me in the right direction ?
thanks in advance
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: how do i FORMAT OUTPUT from php-mysql database

Post by requinix »

What if you have 50 suppliers? 100? That's going to be one very wide table...

Because of the structure of an HTML table, you'll need to be able to show all the IDs for all the suppliers before you move on to the other fields. That means having all the data at once - you won't be able to output each row inside the loop as you have now.

Instead of outputting, put the data into a set of arrays, one for each field. If you put all those arrays inside yet another array then you only need one variable for it.

Code: Select all

$data = array();
while($row = mysqli_fetch_array($result))
  {
  foreach($row as $column => $value)
    {
    $data[$column][] = $value;
    }
  }
Now you can loop over $data.

Code: Select all

foreach($data as $column => $rows)
  {
  echo "<tr>";
  echo "<th>" . $column . "</th>";
  foreach($rows as $row)
    {
    echo "<td>" . $row . "</td>";
    }
  echo "</tr>";
  }
Before that you'll want to output the header row of the table; you can know how many suppliers there are by counting the number of items in any of the subarrays of $data.
Post Reply