show specific database with session - php

uservieworder.php
<?php
include("connect.php");
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
</style>
</head>
<body>
<body bgcolor="#919191">
<?php
if(isset($_SESSION['ID'])) //Checking if they have the session cookie
{
$result = mysql_query("SELECT * FROM orders INNER JOIN register WHERE customer_id ='".$_SESSION["ID"]."' LIMIT 10");
echo "<table border='1'>
<tr>
<th>User ID</th>
<th>Username</th>
<th>Prices</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['customer_id'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['total_price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
else
{
echo "<title>Error!</title>";
//Doesn't have session cookie
echo "YOU ARE NOT LOGGED IN!";
}
?>
</body>
</html>
connect.php
<?php
$hostname="localhost"; //local server name default localhost
$username="root"; //mysql username default is root.
$password=""; //blank if no password is set for mysql.
$database="register"; //database name which you created
$con=mysql_connect($hostname,$username,$password);
if(! $con)
{
die('Connection Failed'.mysql_error());
}
mysql_select_db($database,$con);
?>
with the code above , it show every data on the database.
echo "<td>" . $row['customer_id'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['total_price'] . "</td>";
on the code above , ( customer_id, total_price data are from orders table, and Username are from register table )
I wanted that only show specific data based on session id only .

Related

Awkward white space above outputed Table

I outputed the data from my database in a HTML Table and styled it with CSS. Everything is fine, but there is an awkward White space above the outputed Table. (see picture). What's wrong with my code?
Result:
<html>
<head>
<title>Create new user</title>
<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>
<div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from employee";
$result = $conn->query($sql);
echo "<table>";
echo "<th>Identifier</th><th>Name</th><th>Lastname</th>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
echo "</br>";
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
</div>
</body>
</html>
Expected result : no white space above the Table
Many Thanks
Why are you adding a <br>
echo "</br>";
in your while loop.
Try this -
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
If this does not fix, try inspecting and see what is adding space. Also check for the padding and margins in the computed view in chrome inspect view.
If nothing works, try setting top:0 for the div and may be for table :)

how to view different tables in a table interface using php and sqli

I trying to add 3 tables for to fetch data from 3 table individually. I did mine and seem to have errors, kindly need some help, posting my code i have done but seem to be having errors been trying to have three table interface in one page to fetch 3 different tables
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecommerce";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
enter code here
$sql = "SELECT id, firstname, lastname, username, email FROM tbl_member";
$results = $conn->query($sql);
if ($results->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $results->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td><td>" . $row["username"]. "</td><td>" . $row["email"]. "</td> </tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecommerce";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// $sql = "SELECT id, firstname, lastname, username, email FROM tbl_member";
// $result = $conn->query($sql);
// if ($result->num_rows > 0) {
// echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
//while($row = $result->fetch_assoc()) {
// echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td><td>" . $row["username"]. "</td><td>" . $row["email"]. "</td> </tr>";
// }
// echo "</table>";
// } else {
//echo "0 results";
$sql = "SELECT memberid, plan, start_date, end_date, FROM tbl_orders";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row= $result->fetch_assoc()) {
echo "<tr><td>" . $row["memberid"]. "</td><td>" . $row["plan"]. "</td><td>" . $row["start_date"]. "</td><td>" . $row["end_date"]. "</td> </tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

Css percentage value in PHP

I'm trying to display results as a percentage bar (css), I get $row['Balai'] from mysql query, then count the $proc of it, I set $sum as well. It's works fine, but instead of getting different inner value(bar) I always get the same and all the bars looks the same
while($row=$list->fetch_assoc()){
$proc = round((($row['Balai'] / $sum) * 100),1); ?>
<style type="text/CSS">
.outter{
height 25px;
width:500px;
border-right:solid 1px #000;
background-color: red;
}
.inner{
height:25px;
width: <?php echo $proc ?>%;
border-right:solid 1px #000;
background-color:lightblue;
</style>
<tr>
<center>
<?php
echo "<td>" . $row['ID'] . "</td><td>" . $row['Vardas'] . "</td><td>" . $row['Pavarde'] . "</td><td>" . $row['Balai'] . "<td>"; ?>
<div class="outter">
<div class='inner'><?php echo $proc ?>%</div>
<?php
echo "</td><br />";
echo "</center>";
echo "</tr>";
$x++;
}
echo "</table>";
echo "<b>100% sudaro: " . $sum . "</b>";
Try this:(Please ensure the value of $proc is different)
<style type="text/CSS">
.outter{
height 25px;
width:500px;
border-right:solid 1px #000;
background-color: red;
}
.inner{
height:25px;
border-right:solid 1px #000;
background-color:lightblue;
</style>
<?php
while($row=$list->fetch_assoc()){
$proc = round((($row['Balai'] / $sum) * 100),1); ?>
<tr>
<center>
<?php
echo "<td>" . $row['ID'] . "</td><td>" . $row['Vardas'] . "</td><td>" . $row['Pavarde'] . "</td><td>" . $row['Balai'] . "<td>"; ?>
<div class="outter">
<div class='inner' style='width: "<?php echo $proc ?>"%'><?php echo $proc ?>%</div>
<?php
echo "</td><br />";
echo "</center>";
echo "</tr>";
$x++;
}
echo "</table>";
echo "<b>100% sudaro: " . $sum . "</b>";

How to Print the Data that has been Populated by MYSQL in PHP or HTML

The code below is my way on how to print my data in Table.
<style media="screen">
.noPrint{ display: block; }
.yesPrint{ display: block !important; }
</style>
<style media="print">
.noPrint{ display: none; }
.yesPrint{ display: block !important; }
</style>
Here is my code of printing the data from the table
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
echo "<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>";
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_Close($con);
?>
</div>
</div>
</div>
To initiate the command
<input TYPE="button" value = "Print Report" onClick="window.print()">
Further Explanation: My code here is to print the data from my table and the table has been populated from my database. Data Transfer to Table then Print but what happens is the whole website has been print. It seems that my code Capture the site as Image and this what it print.
What I want to do is print what only on the table, how can achieve it? TY
try this,
you r using mysql_Close($con); but in real mysql_close($con);
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
echo "<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>";
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
</div>
</div>
With the below javascript function, you can print any element by just placing the element in the parentheses.
<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />
Here is the entire code:
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script>
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'my div', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.print();
mywindow.close();
return true;
}
</script>
<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;" class="noPrint">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
?>
<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>
<?php
while ($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
?>
</table>
<?php mysql_close($con); ?>
</div>
</div>
</div>
<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />

How to display mysql data inside a specific div with table

I am trying to display the data output in to specific div within a table.
I am able to get the table setup and display the first row of the data within the table, but the rest of the output does not make it inside the table.
Anyone please can point out what am I doing wrong?
Thank you!
<?php include('header.php'); ?>
<?php
include_once 'scripts/db.php';
$result = mysqli_query($con,"SELECT * FROM employee");
?>
<div id="userlist">
<center><h3>User Listing</h3></center>
<?php
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>location</th>
</tr>";
?>
<?php
while($row = mysqli_fetch_array($result))
{?>
<div class="userlistoutput">
<?php
echo "<td width=120px>" . $row['firstname'] . "</td>";
echo "<td width=120px>" . $row['lastname'] . "</td>";
echo "<td width=120px>" . $row['location'] . "</td>";
echo "</tr>";
?>
</div>
<? echo "</table>" ?>
<?php
}?>
</div>
<?php
mysqli_close ($con);
?>
And this is the CSS for userlist:
#userlist {
list-style:none;
width: auto;
margin:30px auto 1px auto;
height:100%;
padding:0px 20px 0px 20px;
/* Rounded Corners */
border-radius: 10px;
/* Background color and gradients */
background:#F4F4F4;
background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB); */
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));
/* Borders */
border: 1px solid #002232;
}
That is happening because you have put the </table> inside the while statement. Try changing it to:
<?php include('header.php');
include_once 'scripts/db.php';
$result = mysqli_query($con,"SELECT * FROM employee");
?>
<div id="userlist">
<center><h3>User Listing</h3></center>
<?php
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr class='userlistoutput'>;
echo "<td width='120px'>" . $row['firstname'] . "</td>";
echo "<td width='120px'>" . $row['lastname'] . "</td>";
echo "<td width='120px'>" . $row['location'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysqli_close ($con);
?>

Categories