I need some Help. I'm new to PHP and the last couple of days trying to solve this issue. I am trying to parse data from my database to a styled HTML Table and I am not able to find any tutorials on this. I did do the tutorial for parsing to a table that is created with PHP. I would like to use the tables I did include in this file. If anyone would be so kind to show me how to do this and also explain it I would be very happy.
This is the PHP file I did try to work with. Only one close I could find from tutorials.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Navn</th> <th>Årgang</th> <th>NR</th> <th>Navn</th> <th>Navn</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</table>";
?>
This is the stylesheet I would like to use:
/* ------------------
styling for the tables
------------------ */
body
{
line-height: 1.6em;
}
#hor-zebra
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 60px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
#hor-zebra th
{
font-size: 14px;
font-weight: normal;
padding: 10px 8px;
color: #039;
}
#hor-zebra td
{
padding: 8px;
color: #669;
}
#hor-zebra .odd
{
background: #e8edff;
And this is the HTML file where I would like the data from my database to show:
<!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>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th> //Name off table in DB
<th scope="col">age</th> //Name off table in DB
<th scope="col">issue</th> //Name off table in DB
<th scope="col">Description</th> //Name off table in DB
<th scope="col">quality</th> //Name off table in DB
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
</body>
</html>
After adding the code my PHP do return the result. The only problem is it it not showing my styled tables from my style.css and I also get the error "
Notice: Undefined variable: i in C:\Program Files (x86)\EasyPHP-5.3.9\www\Tables\Datamodtagelse.php on line 25
And under that it returns my output: (This is the php page.)
name age issue Description quality
Anders And & Co. 1949 1 Dette er en beskrivelse af en tegneserie. Very Fine.
When I open my html file it doesn't display anything at all.
I will add my file :
Datamodtagelse.php
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo '<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">age</th>
<th scope="col">issue</th>
<th scope="col">Description</th>
<th scope="col">quality</th>
</tr>
</thead>
<tbody>';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if( $i++ % 2 == 0 ) {
$class = " class='odd'";
} else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
Showcomic.html:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>
Style.css
body
{
line-height: 1.6em;
}
#hor-zebra
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 60px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
#hor-zebra th
{
font-size: 14px;
font-weight: normal;
padding: 10px 8px;
color: #039;
}
#hor-zebra td
{
padding: 8px;
color: #669;
}
#hor-zebra .odd
{
background: #e8edff;
}
My database name is: tegneserier
My table in the database is: årgang
My attributes in the table is:
id int(11) AUTO_INCREMENT
name varchar(255) utf8_danish_ci
age int(11)
issue int(11)
Description text utf8_danish_ci
When looking at the code I think the problem is the HTML file nor importing the stylesheet and the data from the .php file?
The .php file and the .css file and the .html file is located in the same folder.
Any help is welcome.
And sorry this is probably just a easy beginner mistake. (We all need to start somewhere.)
Try something like this:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo '<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th> //Name off table in DB
<th scope="col">age</th> //Name off table in DB
<th scope="col">issue</th> //Name off table in DB
<th scope="col">Description</th> //Name off table in DB
<th scope="col">quality</th> //Name off table in DB
</tr>
</thead>
<tbody>';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if( $i % 2 == 0 ) {
$class = " class='odd'";
} else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
And in your HTML file:
<!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>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>
you are almost there (if the code you showed us runs).
in your php file, you need to give your table the id "hor-zebra", so that the style will be applied to it and the th's and td's within it.
you also want to add a counter to get the .odd thing right:
var $i = 0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr";
if( $i++ % 2 == 0 ) echo(" class='odd'");
echo "><td>";
...
you also might want to set the scope="col" in your th's to match the original table as closely as possible
AND last but not least: Don't forget the elements thead and tbody in your echo-output.
if you do all this, you should see the same table twice in your final html (check the source, ctrl+U)
Remember you can echo your data in div tags too, use
<li></li>
to allow row after row of sql data to get displayed. I mention this because I find the div elements easier to work with than tables.
Related
I try to make a dynamic table. The data comes from a database.
It works so far, but I want to make a table with seperate fields, not like my way.
My code so far:
<?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result);
echo("Buy Date"."|Expire Date");
echo "<br />";
echo "<br />";
while ($row = mysql_fetch_array($result)) {
echo '<th>'.$row['buyTime'].'</th>'."|".'<th>'.$row['untilTime'].'</th>';
echo "<br />";
}
?>
The result:
Click here
So how can I make a correct table, not a pseudo one?
Thank you :)
Regards
Use a table element on html and put your php loop code inside the tbody element. How to properly create a table;
<table>
<thead>
<tr>
<th>Buy Date</th>
<th>Expire Date</th>
</tr>
</thead>
<tbody>
<?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$row['buyTime'].'</td><td>'.$row['untilTime'].'</td>';
echo '</tr>'
}
?>
</tbody>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<h2>Colored Table Header</h2>
<table>
<tr>
<th>Buy Date</th>
<th>Expire Date</th>
</tr>
<?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row['buyTime']; ?></td>
<td><?php echo $row['untilTime']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
NOTE: An HTML table is defined with the <table>tag.
Each table row is defined with the <tr> tag. A table header is defined with the <th> tag. By default, table headings are bold and centered. A table data/cell is defined with the <td> tag. Moreover Mysql is outmoded please try learn new approach. You can learn it on w3schools php
Good Luck!!!
<?php
error_reporting(E_ALL);
ini_set('display_errors' ,1);
require "connection.php";
$query= "SELECT client_id, array_agg(insurance) AS insurance from vouchers WHERE parsing_date=CURRENT_DATE GROUP BY client_id ";
$result = pg_query($conn,$query);
?>
<!DOCTYPE html>
<html>
<head>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href = "http://fonts.googleapis.com/css?family=Roboto:400">
<style>
.responstable {
margin: 1em 0;
width: 100%;
overflow: hidden;
background: #FFF;
color: #024457;
border-radius: 10px;
border: 1px solid #167F92;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container-fluid">
<div>
<h1>Clients</h1>
</div>
<table class="responstable" rules='all' style='border-collapse: collapse;'>
<thead>
<tr>
<th>Client id</th>
<th>Insurance</th>
<th>Number of rows</th>
</tr>
</thead>
<?php
while($row = pg_fetch_array($result))
{
?>
<tbody>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo $row['insurance']; ?></td>
<td><?php echo $row['rows'];?></td>
</tr>
<?php }
?> </tbody>
</table>
</div>
</body>
</html>
I have the code above to make my output from:
Client id Insurance
------------ ---------------
123 {"AA","EE","U"}
125 {"AA","UE"}
126 {"CU"}
124 {"UE"}
I want this:
Client id Number of rows Insurance
------------ ----------------------- --------------
123 3 rows AA,EE,U
125 2 rows AA,UE
126 1 rows CU
124 1 rows UE
Im not sure how to fix it, i tried add this line to my query:
concat(count(*), ' rows') AS rows
But it just gave me an error, any ideas on what I could do for the rows to come out and the "{" and '"' goes?
u can use explode function.
try this code
<?php while ( $row = pg_fetch_array ( $result ) ) { ?>
<tr>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo implode(',',json_decode($row['insurance'])); ?></td>
<td><?php
$sRows = '';
if($row['iCnt'] > 0) {
$sRows = $row['iCnt']==1?' row':' rows';
}
echo ''.$row['iCnt'].$sRows;
?></td>
</tr>
JSON Parsing and Query issues are solved
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );
require "connection.php";
$query = "SELECT client_id,
count(*) AS iCnt,
array_agg(insurance) AS insurance from vouchers
WHERE parsing_date=CURRENT_DATE GROUP BY client_id ";
$result = pg_query ( $conn, $query );
?>
<!DOCTYPE html>
<html>
<head>
<link
href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"
rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Roboto:400">
<style>
.responstable {
margin: 1em 0;
width: 100%;
overflow: hidden;
background: #FFF;
color: #024457;
border-radius: 10px;
border: 1px solid #167F92;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container-fluid">
<div>
<h1>Clients</h1>
</div>
<table class="responstable" rules='all' style='border-collapse: collapse;'>
<thead>
<tr>
<th>Client id</th>
<th>Insurance</th>
<th>Number of rows</th>
</tr>
</thead>
<tbody>
<?php while ( $row = pg_fetch_array ( $result ) ) { ?>
<tr>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo implode(',',json_decode($row['insurance'])); ?></td>
<td><?php
$sRows = '';
if($row['iCnt'] > 0) {
$sRows = $row['iCnt']==1?' row':' rows';
}
echo ''.$row['iCnt'].$sRows;
?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
Change you query like this
SELECT client_id, concat(cast(count(*) AS char),' rows')
This works good in mysql. You have to type cast integer to string for concatenation
http://www.mysqltutorial.org/mysql-cast/
For PostgreSQL it's should be like this,
SELECT client_id,
count(*) AS cnt,
''||count(*)||' rows' AS concatString
https://www.postgresql.org/docs/8.3/static/functions-string.html
You can add If and else like this on query
SELECT client_id,
CASE WHEN count(*)>0 THEN
CASE WHEN count(*)==1 THEN ''||count(*)||' row'
ELSE ''||count(*)||' rows' END
ELSE ''
END AS concatenatedString
I don't know about the performance
Postgres nested if in case query
You want to use array_to_string:
SELECT
client_id,
concat(count(*), ' rows'),
array_to_string(array_agg(insurance),',') AS insurance
FROM vouchers
WHERE parsing_date=CURRENT_DATE
GROUP BY client_id
I have already referred this post How to get row ID in button click? but still can't make it load values and I do not know why.I'm pretty sure the id has been properly captured at profile.php. Below is my code:
hanj.php
style type="text/css">
.buttonize {
text-decoration: none;
border: 1px solid #ccc;
background-color: #efefef;
padding: 10px 15px;
-moz-border-radius: 11px;
-webkit-border-radius: 11px;
border-radius: 11px;
text-shadow: 0 1px 0 #FFFFFF;
}
<?php require_once('Connections/conn.php');?>
</style>
<table border="1" cellpadding="5" cellspacing="2" width="600">
<tr>
<th>Vendor Id</th>
<th>Kategori</th>
<th>Nama Vendor</th>
<th>Alamat</th>
<th>Poskod</th>
<th>Bandar</th>
<th>Negeri</th>
<th>No</th>
<th>Email</th>
</tr>
<?php
session_start();
$_SESSION['profile']= $row ['v_id'];
mysql_select_db ($database_conn,$conn);
$query="SELECT v_id,type,companyName,address,code,city,state,contact,email FROM vendor";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "</td><td>";
echo $row['v_id'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>";
echo $row['companyName'];
echo "</td><td>";
echo $row['address'];
echo "</td><td>";
echo $row['code'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
echo $row['state'];
echo "</td><td>";
echo $row['contact'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
print '<center>View</center>';
echo "</td></tr>";
}
?>
profile.php
<?php require_once('Connections/conn.php'); ?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Casado</title>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="http://pingendo.github.io/pingendo-bootstrap/themes/default/bootstrap.css" rel="stylesheet" type="text/css">
<div class="container">
<div class="col-md-12">
<div class="col-md-2"></div>
<div class="col-md-8">
<table class="table table-bordered">
<thead>
<tr>
<th>Pakej</th>
<th>Image</th>
<th>Harga</th>
<th>Vendor Id</th>
</tr>
</thead>
<tbody>
<?php
session_start();
mysql_select_db ($database_conn,$conn);
$vid = $_SESSION['profile'];
$sql = mysql_query("SELECT * from item where v_id = '$vid' ") or die (mysql_error());
while($res = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $res['pakej'] ?></td>
<td><?php echo '<img src="data:image;base64,'.$res['image'].'" class="img-thumbnail">' ?></td>
<td><?php echo $res['harga'] ?></td>
<td><?php echo $res['v_id'] ?></td>
</tr>
<?php }
?>
</tbody>
</table>
</div>
<div class="col-md-2"></div>
</div>
</div>
</body>
</html>
btw there is no syntax error
Instead of $_SESSION['profile'] use $_REQUEST['id'] in your profile.php and it should work.
In profile.php use $_GET to reference the passed in 'id'. i.e
Replace the following line
$vid = $_SESSION['profile'];
with this one.
$vid = $_GET['id'];
Hope this helps.
this question is answered by Bikash Paul above where he suggest to change $_SESSION['profile'] to $_REQUEST['id']
Instead of $vid = $_SESSION['profile']; try $vid = $_GET['profile'];, if not works, then print_r($_GET) or print_r($_REQUEST) & show us the details
I don't understand why this isn't working, I have been stuck on this for ages and tried lots of different alternatives, but it just doesn't print the data from the database.
At the moment I am just trying to get the id to print, but eventually I want to print most of the data in the database (not including the hash).
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Staroids Leaderboard</title>
</head>
<body>
<table border=1px>
<thead>
<tr>
<td>name</td>
<td>score</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "password");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("staroids");
$results = mysql_query("SELECT id FROM scores");
while($row = mysql_fetch_array($results)) {
$name = $row['id']
?>
<tr>
<td><?php echo '$name'?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
The image below shows what it looks like in html:
This image shows the database in local host and as you can see there is lots of data, but none of the names seem to print?!
Correct your syntax where it could be
$name = $row['id']; //Put ; here
<?php echo $name;?> //Remove quotes and put ;
Select name from DB and you can get name then.It should be
$results = mysql_query("SELECT id,name FROM scores");
while($row = mysql_fetch_array($results)) {
$name = $row['name'];
?>
<td><?php echo $name;?></td>
And dont use mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
And as #Nedstark said use try die(mysql_error()); for the errors regarding the mysql errors.
<td><?php echo $name;?></td>
or use
<td><?php echo "$name";?></td> <!--(Bad idea but works)->
Variables work in double quotes("") not in single quotes('')
<?php
session_start();
if (!(isset($_SESSION['UserName'])))
{
echo "<script type=\"text/javascript\">alert('Unauthorize user are redirected to Login page');".
header('Location:http://localhost/campus');
}
include_once "connect.php";
$find = mysql_query("YOUR SELECT STATEMENT ") or die('error');
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ST. MICHAEL's COLLEGE ILIGAN CITY</title>
<style>
AlignJst {
text-align:justify;
text-justify:inter-word;
}
pTable {
margin:2cm 4cm 3cm 4cm;
}
body {color: black; font-size: 10px; font-family: Helvetica, Arial, non-serif;}
a:link {color: #FF8C00;}
a:visited {color: #FF8C00;}
a:hover {color: #FF8C00; background: #ADD8E6; text-decoration:none;}
a:active {color: #FF0000;}
p {line-height: 2em;
font-size:85%;
color:black;
letter-spacing: 0.3em
}
h1 {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
font-size: 12pt;
color: navy;
padding-top: 12px;
padding-bottom: 3px;
}
</style>
</head>
<body>
<?php
echo "<CENTER>"."<H1>TABLE TITLE</H>" . "<BR />";
echo "<H1>SUBTITLE</H>"."</CENTER>"."<BR/>";
echo "<CENTER>"."<p>"."<b>" . "PAST MORNING PRAYER SCHEDULE" ."</b>"."</p>"."</CENTER>"."<BR/>";
echo "<table border='1' width='100%' align ='center'>";
echo "<tr>";
echo "<th>SPONSOR NAME</th>";
echo "<th>VENUE </th>";
echo "<th>DATE EVENT</th>";
echo "<th>TIME </th>";
while($row = mysql_fetch_array($find)){
echo "<tr>";
echo "<td>".$row['sponsor_name']."</td>";
echo "<td>".$row['Venue']."</td>";
echo "<td>".$row['Date_Event']."</td>";
echo "<td>".$row['Time_Event']."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br />". "<br />" ."<br />";
echo "<p align = 'right'>"."Prepared By:" . $_SESSION['UserName'] ."</p>";
?>
</body>
Other than changing mysql ==> msqli, I'd recommend a couple of debug strategies, in this case I would:
fix the echo, you need to pick one of those two options:
<?php echo $variable; ?>
<?php echo "this is my variable {$variable}"; ?>
If you put a single quote PHP don't parse the content of what is about to print, it just print it as text, so what you have should print $name in the HTML...but, since I don't see any $name text in the black screenshot I think you might even not get into that loop...
a good debug strategy would be to query for something broader, i.e. "SELECT * FROM `scores`", then you can do a
<?php print_r($row); ?>
right after while ($row = mysql_fetch_array($results)) {
Overview
I have some data stored in MySql database related to Products. I am trying to retrieve this data and display it on a page using HTML table.
The PHP and MySql has gone well and all the data is retrieved but it is displayed in a very messy manner.
Here is what I have as a layout:
What I am aiming to achieve is to further divide the results table add more columns rows to make the data more readable
Something like this;
The code: PHP, MySQL & HTML:
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products");
echo "<table id='display'>";
while($rows = mysql_fetch_array($sql))
{
echo"<br>";
echo"<tr><td>";
echo"$rows[$product_name]<br></td>";
echo"<td><img src=$rows[$product_image] height='200px' width='200px'><br></td>";
echo"<td>Avalible: $rows[$product_qua]<br></td>";
echo"<td>Price: $rows[$product_price]<br></td>";
echo"<td>Description: $rows[$product_des]<br></td>";
echo"</tr>";
}
echo "</table>";
?>
CSS responsible for this part:
#display{
float:left;
border: 5px solid black;
margin-left:100px;
}
just add some padding or a border to the table cells:
table#display td{
border: 1px solid black;
padding:0 8px;
}
Edit: What you could do as well:
<table id='display'>
<?php while($rows = mysql_fetch_array($sql)): ?>
<!-- <br> <- why a break? it's not in the correct spot anyway -->
<tr><td>
<?php echo $rows[$product_name]; ?><br>
</td>
<td> - </td>
<td><img src="<?php echo $rows[$product_image]; ?>" height='200px' width='200px'><br></td>
<td> - </td>
<td>Avalible: <?php echo $rows[$product_qua]; ?><br></td>
<td> - </td>
<td>Price: <?php echo $rows[$product_price]; ?><br></td>
<td> - </td>
<td>Description: <?php echo $rows[$product_des]; ?><br></td>
</tr>
<?php endwhile; ?>
</table>
Tip: I prefer to use the while/endwhile; approach rather than using brackets when displaying data to the user.
First of all don't echo so much HTML using PHP, instead do it like this
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products"); ?>
<table id='display'>
<?php
while($rows = mysql_fetch_array($sql)) {
?>
<tr><td><?php echo $rows[$product_name]; ?></td>
<!-- And so on-->
<?php
}
?>
</table>
Secondly by seeing your inmage, it seems like you need a border for your table so use
table, td {
border: 1px solid #000000;
}
add the following css to apply border on your table cells:
#display td{ border: 2px solid red; }
and optionally add to your #display { :
border-collapse: collapse;