Jump to content

Displaying Data from MySQL in a table on HTML page


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi all, I was wondering if you can help me with an issue I am having with my website. I am very new to php/html/css and mysql so I know very little. I have an order form on my website which when filled in send data to my database. I then have an admin page on my site which shows the data in the database. However, I need the data to be stored in a table with a row containing the column name and then each row showing the actual data underneath. Would like it in a grid sort of format. My current code is:

<!doctype html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title></title>
	<link href="css/style.css" rel="stylesheet" type="text/css" />
	<link href="css/reset.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="container2">
<div id="adminpanel">
Admin Page 

<div id="showorders"><u>Orders</u></div>
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
while($row = mysql_fetch_array($result))
  {
  echo "<span style='color: #333; width: 200px; height: 200px; padding: 1px; border: 1px solid #ff9900;'>" . $row['product'] . " " . $row['productcomments'] . " " . $row['name'] . " " .       $row['address'] . " " . $row['age'] . " " . $row['delivery'] . "</span>" ;
echo "<br>";
  }
?>


<div id="showreviews"><u>Reviews</u></div>
<?php
$result = mysql_query("SELECT * FROM reviewform");
while($row = mysql_fetch_array($result))
  {
  echo "<span style='color: #333; width: 200px; height: 200px; padding: 5px; border: 1px solid #ff9900;'>" . $row['name'] . " " . $row['product'] . " " . $row['comment']  . "</span>" ;
echo "<br>";
  
  echo $row['name'] . " " . $row['product'] . " " . $row['comment']  ;
  echo "<br>";
  }
?>

</div>
</div>
</body>

This code puts a border around each row but doesnt seperate each column and doesnt show a header. Ultimately, I am going to want to sort each order by different criteria e.g alphabetical, date ordered, product type.

All help is really appreciated as I am really pushed for time and a complete beginner.

Thanks Jonathan

  • Solution

Sounds like what you want is a HTML table. The basic code for a table is

<table>
  <tr>
    <th>Heading 1</th>
    <th>Heading 2</th>
    etc...
  </tr>
  <tr>
     <td>Column 1</td>
     <td>Column 2</td>
     etc...
  <tr>
  <tr>
     etc. 
  </tr>
</tablel>

So the column headings go between <th> and </th>, each row is echoed between <tr> and </tr> and each column value is echo'd between <td> and </td>.

 

The following with output the results from your orderform query into a HTML table

<div id="showorders"><u>Orders</u></div>
<table border="1" cellpadding="4"> <!-- start the table, defining the table column headings -->
    <tr>
        <th>Product</th>
        <th>Comments</th>
        <th>Name</th>
        <th>Address</th>
        <th>Age</th>
        <th>Delivery</th>
    </tr>   
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
while($row = mysql_fetch_array($result))
{
    // each row returned from the query echo into individual columns for the table
    echo '      <tr>
        <td>'.$row['product'].'</td>
        <td>'.$row['productcomments'].'</td>
        <td>'.$row['name'].'</td>
        <td>'.$row['address'].'</td>
        <td>'.$row['age'].'</td>
        <td>'.$row['delivery'].'</td>
    </tr>';
}
?>
</table> <!-- end table -->
Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.