Simplest way to display MySQL data on a website with a table? - php

I am trying to display data from from a database in a table, with one column for the names and another for the values, instead of the rudimentary layout it currently has:
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "{$row['date']} <br> ".
"--------------------------------<br>".
"Temperature :{$row['temperature']} <br> ".
"Luminosite : {$row['luminosite']} <br> ".
"Humidite : {$row['humidite']} <br> ".
"--------------------------------<br>";
}
I am very much a novice in all things php so any suggestions are welcome.

Try Using below code
echo "<table>
<tr>
<th>Date</th>
<th>Name</th>
<th>luminosite</th>
<th>humidite</th>
</tr>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<tr>
<td>".$row['date']."</td>
<td>".$row['temperature']."</td>
<td>".$row['luminosite']."</td>
<td>".$row['humidite']."</td>
</tr>";
}
echo "</table>";

Try to use this code
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div>
<div class="row">
<div class="table-responsive">
<table class="table table-striped table-hover" id="my_table">
<thead>
<tr>
<th>Temperature</th>
<th>Luminosite</th>
<th>Humidite</th>
</tr>
</thead>
<tbody>
<?php
$query = "select * from table";
$conn = mysqli_connect("localhost", "root", "password", "ecom");
$data = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($data))
{
$temperature = $row['temperature'];
$luminosite = $row['luminosite'];
$humidite = $row['humidite'];
echo "<tr><td>$temperature</td><td>$luminosite</td><td>$humidite</td></tr>";
}
; ?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>

Try this code
<table>
<tr>
<td>Date</td><td>Temperature</td><td>Luminosite</td><td>Humidite</td>
</tr>
<?php
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<tr>
<td>$row['date']</td>
<td>$row['temperature']</td>
<td>$row['luminosite']</td>
<td>$row['humidite']</td>
</tr>";
}
?>
</table>

Do not use MySQL.
<table>
<tr>
<?php
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
?>
<td>
<?php echo "{$row['date']};?>
<td>
<td>
<?php echo "{$row['temperature']};?>
<td>
<!-- other fiels -->
<?php
}
?>
</tr>
</table>

Related

convert rows to columns php is it possible?

I want to make 2 columns(openday, school_name) into 1 row and print it as a table.
I would like to print it out like a desired table.
Is it possible?
convert rows to columns php is it possible?
If possible, please explain in detail
my code is
<?php
$SQL= "SELECT Concat_ws('\n' openday,'', school_name) AS school_column, supply_name,score from result ";
$grouped = [];
$columns = [];
$resultObject = $result = mysqli_query($conn, $SQL);
foreach ($resultObject as $row) {
$grouped[$row['supply_name']][$row['school_column']] = $row['score'];
$columns[$row['school_column']] = $row['school_column'];
}
sort($columns);
$defaults = array_fill_keys($columns,'-');
array_unshift($columns, 'school_column');
?>
<body>
<table id="example" class="display" style="width:100%" align="center">
<thead>
<?php
While($result = mysqli_fetch_array($resultObject));
{
echo "<tr>
<td>supply_name</td>
<td> ".$row['school_column']." </td>
</tr>";
}
?>
</thead>
<tbody>
<?php
While($result = mysqli_fetch_array($resultObject));
{
echo "<tr>
<td> ".$row['supply_name']." </td>
<td> ".$row['score']." </td>
</tr>";
}
?>
</tbody>
</table>
</body>
<?php
$SQL= "SELECT Concat_ws('\n' openday,'', school_name) AS school_column, supply_name,score from result ";
$grouped = [];
$columns = [];
$resultObject = $result = mysqli_query($conn, $SQL);
foreach ($resultObject as $row) {
$grouped[$row['supply_name']][$row['school_column']] = $row['score'];
$columns[$row['school_column']] = $row['school_column'];
}
sort($columns);
$defaults = array_fill_keys($columns,'-');
array_unshift($columns, 'school_column');
?>
<body>
<table id="example" class="display" style="width:100%" align="center">
<thead>
<?php
while($result = mysqli_fetch_array($resultObject))
{
echo "<tr>
<td>openday</td>
<td> school_name </td>
</tr>";
}
?>
</thead>
<tbody>
<?php
while($result = mysqli_fetch_array($resultObject))
{
echo "<tr>
<td colspan='2'> ".$row['supply_name']." </td>
<td> ".$row['score']." </td>
</tr>";
}
?>
</tbody>
</table>
</body>

Image is uploaded but not displayed in php

I had this project wherein, I am supposed to insert and select pictures, as in upload and view them. Initially, when I tried a code, it worked but the image didn't display. After many trials, I just copied a code from a mate which worked for him but when I tried, it didn't. Why? I have posted everything below-
Image upload code
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap");
$query = "SELECT * FROM `blobclob`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Image View Code
<html>
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap_exp");
$query = "SELECT * FROM `images`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Output
Database
it should be something like this
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
it should be **src="data:image/jpeg;base64 ... ** instead of src="data:image..

Attach SQL to button

I want to get data from a MySQL Database;
EDIT:
I want to be able to modify a query in order to obtain different rows corresponding to a specific date, using a datapicker.
I need to generate a report (an Excel file) from a query, make it user-friendly, using HTML and PHP but I don't know how to do that.
EDIT: Sorry, I am going to edit this question:
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$sql = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $sql);
?>
<html>
<head>
<title>Attempt to get data</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h2 align="center">Export MySQL data to Excel in PHP</h2><br />
<table class="table table-bordered">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
?>
</table>
<br />
<form method="post" action="export.php">
<input type="submit" name="export" class="btn btn-success" value="Export" />
</form>
</div>
</div>
</body>
</html>
And
<?php
//export.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$output = '';
if(isset($_POST["export"]))
{
$query = "SELECT * FROM tbl_customer";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["CustomerName"].'</td>
<td>'.$row["Address"].'</td>
<td>'.$row["City"].'</td>
<td>'.$row["PostalCode"].'</td>
<td>'.$row["Country"].'</td>
</tr>
';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
?>
Take a look at this:
$sql = "SELECT * FROM tbl_customer";
there is a column called CREATED_AT in my table and I want the user to be able to dynamically change the value of that in the page.
I mean:
SELECT * FROM tbl_customer
where CREATED_AT >= (here, the user should be able to adjust this field in the view of the page, otherwise he Will get several rows from different dates). I don't know how to do that :(
Also, I would like to display a load bar when clicking on export to Excel.

Print all table data from mysql into a html table

I've been trying to print all users into a html table. The objective is that the site administrator can edit the information.
I've done the following code so far, but I can't understand how I will print User1_Name, User1_Email and password into a table row. I want this for every user on the database.
<body>
<div class="edituser">
<h1> Edit Users </h1>
<table>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php include("include/headerhomeadmin.php");?>
<?php while($row = mysqli_fetch_assoc($result)) {
/*$line[]=$row;*/
echo
$userusername =$row['username'];
$useremail =$row['email'];
$userpassword=$row['password'];
?>
<td><?php echo implode(" ", $line)?></td>
<?php } ?>
</table>
</div>
Can someone help me?
UPDATE:
My code is now printing the first username, the email from the second user and the password from de third user.
The code is the following:
<body>
<div class="edituser">
<h1> Editar Utilizadores </h1>
<table>
<tr>
<th>Nome de Utilizador</th>
<th>Email</th>
<th>Password</th>
</tr>
<tr>
<?php while($row = mysqli_fetch_assoc($result)) {
$userusername =$row['username'];
?>
<td> <?php echo $userusername ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$useremail =$row['email'];
?>
<td> <?php echo $useremail ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$userpassword=$row['password'];
?>
<td> <?php echo $userpassword ?> </td>
<?php }
}
}?>
How can I fix this?
if ($result->num_rows > 0) {?>
<table border=1>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
</tr>
<?php }?>
</table>
<?php
}
else{
echo "0 results";
}
?>

Outputting Data into an HTML table using php/html

I am trying to output data onto my webpage from a database. I am able to so successfully, but in a very untidy way. I have tried to put the data in a table on the website with no success.
Below, data is retrieved from the db, but just echoed out crudely. It looks untidy but it outputs successfully onto the webpage
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Address: " . $row["address"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
I try to put the data into an HTML table(on the same page, by combining PHP and HTML with no success. How can put this data into an organised table successfully?
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
</tbody>
</table>
</div>
Below is roughly how I would like the data to be structured, how can achieve this?
Name | Email | Address
Jo |J#o.com|12 Street
Ben |B#e.com|23 street
try this:
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Try below code :
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) { ?>
<td><?php $row["name"]?></td>
<td><?php $row["email"]?></td>
<td><?php $row["address"]?></td>
<?php }
} ?>
</tr>
</tbody>
</table>
</div>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
?>
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead> <tbody>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php }
} else {
echo "<tr><td colspan=3>0 results</td></tr>";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
You can always print the results from the select like this:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["name"]."</td>
<td>".$row["email"]."</td>
<td>".$row["address"] . "</td></tr>";
}
It's not a very clean way, but for each row should print another row, below the three table headers.
There isn't really a very neat way to do this beyond the other answers.
But what you can do is keep the HTML and PHP separate as much as possible in the same file.
The PHP at the top of the file can be as follows:
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
$htmlToDisplay = ''; //this variable will contain table rows
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
//create the HTML row - we'll use this later
$htmlToDisplay .= "<tr><td>".$row['name']."</td><td>". $row['email']."</td><td>".$row['address']."</td></tr>";
}
} else {
$htmlToDisplay = "<tr><td>0 results</td><tr>";
}
Then you can have your HTML after your closing PHP tag (?>) as follows:
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- echo your php row(s) here in a slightly neater way since it's one line -->
<?php echo $htmlToDisplay; ?>
</tbody>
</table>
This will make the the different parts of the file (the PHP and HTML) more readable.
You could also look at using a PHP template engine like smarty.

Categories