PHP controlling where a table is placed on a web page - php

I'm new to PHP and I'm working on a very simple SELECT query pulling information from a MySQL database. I have an echo statement that prints out that my connection to the database was successful which is the very first line on the webpage. After "Connected Successfully" is display the SELECT statement pulls the query information into a table.
Right now everything is pulling and displaying correctly but there is a large gap between my echo connection statement and the actual table results. I've tried to directly align my table to 'TOP' but that is still producing the large gap.
<!DOCTYPE html>
<html>
<style>
#trip th {
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
background-color: #4CAF50;
color: white;
}
#trip {
border-collapse: collapse;
width: 75%;
}
#trip td, #trip th {
border: 1px solid #ddd;
padding: 8px;
}
#trip tr:nth-child(even){
background-color: #f2f2f2;
}
#trip tr: hover {
background-color: #ddd;
}
</style>
<body>
<?php
require "Login.php";
?>
<?php
//Create Connection
$conn = mysqli_connect($serverName, $userName, $password, $dbName);
//Check connection
if ($conn === false) {
die("ERROR: Could not Connect. " . mysqli_connect_error());
}
echo "Connected successfully";
//SQL SELECT query execution
$sql = "SELECT * FROM Trip";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table id='trip' align='top'>";
echo "<tr>";
echo "<th>TID </th>";
echo "<th>NAME </th>";
echo "<th>DATE </th>";
echo "<th>COST </th><br>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['TID'] . " </td>";
echo "<td>" . $row['NAME'] . " </td>";
echo "<td>" . $row['DATE'] . " </td>";
echo "<td>" . $row['COST'] . " </td>";
echo "</tr>";
}
echo "</table>";
//Free result set
mysqli_free_result($result);
}
else {
echo "No records matching your query were found";
}
}
else {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
//Close connection
mysqli_close($conn);
?>
</body>
</html>
I'd like the table to display right underneath my "Connected Successfully" message but something seems to be putting the table further down the page.
Edit Fixed the '\' that was added when I tried to paste into Stackoverflow. Not sure why that was added. It's not in my actual PHP file.

The problem is the <br> you have at the end of this line:
echo "<td>" . $row['COST'] . " </td><br>";
Since <br> isn't allowed inside <tr> elements, the browser is rendering them before the table. So the number of blank lines before the table is the same as the number of table rows.
Get rid of that, it serves no purpose.
#trip th {
padding-top: 5px;
padding-bottom: 5px;
text-align: left;
background-color: #4CAF50;
color: white;
}
#trip {
border-collapse: collapse;
width: 75%;
}
#trip td,
#trip th {
border: 1px solid #ddd;
padding: 8px;
}
#trip tr:nth-child(even) {
background-color: #f2f2f2;
}
#trip tr: hover {
background-color: #ddd;
}
<div>Connected successfully</div>
<table id='trip' align='top'>
<tr>
<th>TID </th>
<th>NAME </th>
<th>DATE </th>
<th>COST </th>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
<tr>
<td>TID</td>
<td>NAME</td>
<td>DATE</td>
<td>COST</td>
</tr>
</table>

Related

Is there a way to get my images to show up on my PHP page through MySQL or phpMyAdmin?

I am wondering if I can get my PHP page running because I try to load image files from the database in phpMyAdmin and on MySQL. Here is the line of code I was working on:
CREATE DATABASE WORLD;
use WORLD;
CREATE TABLE COUNTRY (
country_Order INT AUTO_INCREMENT NOT NULL,
continent_Name VARCHAR(225),
country_Name VARCHAR(225),
img BLOB,
country_Region VARCHAR(225),
country_Population INT,
country_Info VARCHAR(225),
PRIMARY KEY (country_Order)
) ENGINE=INNODB;
INSERT INTO COUNTRY (country_Order, continent_Name, country_Name, img, country_Region, country_Population, country_Info)
VALUES (1, "Asia", "Afghanistan", LOAD_FILE('\Users\sammyabdulkader\desktop\World-Data\img\Afghanistan.png'), "Central Asia", 38928346,"Afghanistan is a country located in Central Asia. It is a mountainous landlocked country.");
I want manually insert my own images that why from my folder path. This is being done on my MacBook Pro. Here is my PHP code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<body>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>World Database</title>
<style type="text/css">
h1 {
color: #42963d;
font-family: 'Open Sans', sans-serif;
font-size: 34px;
font-weight: 300;
line-height: 40px;
margin: 0 0 16px;
text-align: center;
}
h2 {
font-family: 'Open Sans', sans-serif;
font-weight: 300;
text-align: center;
color: #50d248;
}
p.footer {text-align: center}
table.output {font-family: Ariel, sans-serif}
table {
border-collapse: collapse;
width: 100%;
color: #4ee44e;
font-family: monospace;
font-size: 25px;
text-align: center;
}
th {
background-color: #0b7208;
color: white;
}
tr:nth-last-child(even) {background-origin: #f2f2f2}
hr {
height: 12px;
border: 0;
box-shadow: inset 0 12px 12px -12px rgba(0, 0, 0, 0.5);
}
a {
color: red;
font-family: helvetica;
text-decoration: none;
text-transform: uppercase;
}
a:hover {
text-decoration: underline;
}
a:active {
color: black;
}
a:visited {
color: purple;
}
</style>
</head>
<body>
<?php
// Get connection
//$Conn = odbc_connect('database name','database user','database user & password');
$Conn = odbc_connect('WORLD', 'WORLD-User', 'WORLD-User+password');
// Test connection
if (!$Conn)
{
exit("ODBC Connection Failed: " . $Conn);
}
// Create SQL statement
$SQL = "SELECT * FROM COUNTRY";
$RecordSet = odbc_exec($Conn, $SQL);
// Test existence of recordset
if (!$RecordSet)
{
exit("SQL Statement Error: " . $SQL);
}
?>
<!-- Page Headers -->
<h1>List of countries from around the global.</h1>
<hr />
<h2>All Country's Alphabetical Order</h2>
<?php
// Table headers
echo "<table class='output' border='1'>
<tr>
<th>Country Order</th>
<th>Continent Name</th>
<th>country Name</th>
<th>Country Flag</th>
<th>Continent Region</th>
<th>Country Population</th>
<th>Country Info</th>
</tr>";
// Table data
while ($RecordSetRow = odbc_fetch_array($RecordSet))
{
echo "<tr>";
echo "<td>" . $RecordSetRow['country_Order'] . "</td>";
echo "<td>" . $RecordSetRow['continent_Name'] . "</td>";
echo "<td>" . $RecordSetRow['country_Name'] . "</td>";
echo "<td>" . $RecordSetRow['img'] . "</td>";
echo "<td>" . $RecordSetRow['country_Region'] . "</td>";
echo "<td>" . $RecordSetRow['country_Population'] . "</td>";
echo "<td>" . $RecordSetRow['country_Info'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close connection
odbc_close($Conn);
?>
<br />
<hr />
<p class="footer">Return to World Data</p>
<hr />
</body>
</html>
My problem is that when I did the manual way, I get null values for my PHP table.
The other problem is doing it on phpMyAdmin makes it look worse. Instead, it returns stranges characters.
Is there a possible way to fix my problem so that I can get my images in my database table up and running?
phpMyAdmin way inserting the images from the online database
Manual way from MySQL insert
This is just an example approach on how to do it.
The idea is to create a unique filename for each image. A simple fast way is a MD5 hash. Then we check if the file is there, and if not we create it. This caching improves performance on reload and image does not need to be created again.
With the use of IMG tag we just display the (newly created) file.
Note: You may need to style it with CSS and place the images into a directory for better organization.
while ($RecordSetRow = odbc_fetch_array($RecordSet))
{
$filename = md5($RecordSetRow['img']) . '.png';
if(!file_exists($filename)) {
file_put_contents($filename, $RecordSetRow['img']);
}
echo "<tr>";
echo "<td>" . $RecordSetRow['country_Order'] . "</td>";
echo "<td>" . $RecordSetRow['continent_Name'] . "</td>";
echo "<td>" . $RecordSetRow['country_Name'] . "</td>";
// Use HTML image tag with that file
echo "<td><img src='{$filename}' alt=''></td>";
echo "<td>" . $RecordSetRow['country_Region'] . "</td>";
echo "<td>" . $RecordSetRow['country_Population'] . "</td>";
echo "<td>" . $RecordSetRow['country_Info'] . "</td>";
echo "</tr>";
}
echo "</table>";

Deleting a Table Entry with PHP and SQL

So I have created a form that takes in entries and stores them in a SQL database called Warehouse_Maitenence (yes I know it is mispelled and I did that on purpose). The goal for this set of code is to connect to the database and display the table accordingly
<?php include("nav.php") ?>
<body style='background-color:#C0C0C0'>
<style>
.top{
text-align: Left;
}
.Headers_Inputs{
padding-top: 20px;
padding-bottom: 20px;
border-style: solid;
border-width: 1px;
}
input{
width: 100%;
}
.PRIMARY_TABLE{
padding-left: 20px;
padding-right: 20px;
width: 100%;
}
body{
padding-left: 20px;
padding-right: 20px;
padding-top: 20px;
padding-bottom: 60px;
}
.PRIMARY_TRAY{
border-style: solid;
border-width: 2px;
}
</style>
<body>
<table width="100%" border="1">
<tr>
<th style="text-align: center; font-size: 16px;"> ID </th>
<th style="text-align: center; font-size: 16px;"> MACHINE </th>
<th style="text-align: center; font-size: 16px;"> LABEL </th>
<th style="text-align: center; font-size: 16px;"> CONDITION </th>
<th style="text-align: center; font-size: 16px;"> ACTION </th>
<th style="text-align: center; font-size: 16px;"> ADDRESS </th>
<th style="text-align: center; font-size: 16px;"> DATE </th>
<th style="text-align: center; font-size: 16px;"> COMMENT </th>
<th style="text-algin: center; font-size: 16px;"> DELETE </th>
</tr>
<?php
$username="";
$password="";
$database="";
$servername = "";
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT ID, MACHINE, LABEL, COND, ACTION, ADDRESS, DATE, COMMENT FROM Warehouse_Maitenence ORDER BY `Warehouse_Maitenence`.`MACHINE` ASC, `Warehouse_Maitenence`.`LABEL` ASC, `Warehouse_Maitenence`.`ID` ASC';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td style='text-align: center; font-size: 16px;' id=".$row["ID"].">".$row["ID"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["MACHINE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["LABEL"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COND"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ACTION"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["ADDRESS"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["DATE"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'>".$row["COMMENT"]."</td>";
echo "<td style='text-align: center; font-size: 16px;'><input type='button' onclick=delete_entry(".$row["ID"].")</td>";
echo "</tr>";
}
} else { echo "0 results"; }
echo "</table>";
mysqli_close($conn);
?>
So This level of code does work as intended up to the deletion point. I want to be able to load buttons on the end of each row of the table and onclick delete that row from the table and SQL Database.
For the table I have a JS already which works in another set of code I have been working on.
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("dataTable").deleteRow(i);
}
I know that onclick I will need to reconnect to the database (no issues there since it is already in this code) and then run
DELETE FROM `Warehouse_Maitenence` WHERE `Warehouse_Maitenence`.`ID` = whatever row was selected
but I am not sure how to put it all together, since I am not doing a form submission.
Am only good with php and i will advice you to do this..
add this to the list of the table
<td>
<form action="delete.php" method="post" enctype="multipart/form-data">
<input type="text" name="id" value="<?php echo $id; ?>" style="display:none;" />
<input type="submit" name="delete" value="Delete" class="btn btn-danger" />
</form>
</td>
then create a page call delete.php
<?php
if(!isset($_SERVER['HTTP_REFERER'])){ //This is to stop direct access to this delete.php page
header('location: /404.php'); //where to be redirected to
exit;
}
session_start();
include("connect.php"); //your database connection declaration page
if(isset($_POST['delete'])){ //name from the form from table
$id = mysqli_real_escape_string($con, $_POST['id']); //id of the row
$variable = $con->query("DELETE FROM tablename WHERE id = '$id'") or die(mysqli_error($con)); //connecting to the db to the table to delete
if($variable == TRUE){
echo"<script>alert('Deletion Completed!');</script>";
echo"<meta http-equiv='refresh' content='0 url=page.php' />"; //where to be redirected to after deleting.
}else{
echo"<script>alert('Error');</script>";
print_r("Error, Lets go back and Try again");
echo"<meta http-equiv='refresh' content='0 url=page.php' />";
}
exit();
}
?>
I hope am able to solve your issues.!

Word wrapping in database or output php page?

Is there a way to wrap the text within the database?
I have a address being added into the database under the address column, but whenever I try to output the database through PHP it shows that the address column is seriously long which goes all the way out of the range of the paper size. Is there a method I could wrap these text into a specific amount of letters and have it line break on the output file or within the database?
edit: updated question with code
CODE:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>UGGL PEUI Customer Dividend - 12-01-2014</title>
<style type="text/css">
body,td,th {
font-family: Lato, Calibri, Arial, sans-serif;
font-style: normal;
font-weight: normal;
font-size: 12px;
}
body {
background: #C28E33;
color: #E6FFF2;
}
table{
border: 0px solid black;
border-spacing: 0px;
}
table thead tr{
font-family: Arial, monospace;
font-size: 14px;
}
table thead tr th{
border-bottom: 2px solid black;
border-top: 1px solid black;
margin: 0px;
padding: 2px;
background-color: #cccccc;
font-weight:bold
}
table tr {
font-family: arial, monospace;
color: black;
font-size:12px;
background-color: white;
}
table tr.odd {
background-color: #AAAAAA;
}
table tr td, th{
border-bottom: 1px solid black;
padding: 2px;
}
a:link{
font-family:arial, monospace;
text-decoration: none;
color: teal;
}
a:hover{
text-decoration: underline;
}
a:visited{
color:black;
text-decoration: none;
}
</style>
</head>
<body>
<?php
// HTML Table Output
echo
"<table border='1'>
<thead>
<tr>
<th>ID</th>
<th>Purchase Date</th>
<th>Mature Date</th>
<th>Amount</th>
<th>Beneficiary First Name</th>
<th>Beneficiary Last Name</th>
<th>Beneficiary ID</th>
<th>Beneficiary Contact No.</th>
<th>Beneficiary Address</th>
<th>Bank Account Name</th>
<th>Bank Account No.</th>
<th>Bank Swift Code</th>
<th>Bank Name</th>
<th>Bank Address</th>
<th>Bank Contact No.</th>
<th>Agent Name</th>
<th>Dec. 1, 2014 (Days)</th>
<th>Dec. 1, 2014 (Dividend)</th>
</thead>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['purchase_date'] . "</td>";
echo "<td>" . $row['mature_date'] . "</td>";
echo "<td>" . $row['unit'] . "</td>";
echo "<td>" . $row['beneficiary_first'] . "</td>";
echo "<td>" . $row['beneficiary_last'] . "</td>";
echo "<td>" . $row['beneficiary_id'] . "</td>";
echo "<td>" . $row['beneficiary_no'] . "</td>";
echo "<td>" . $row['beneficiary_add'] . "</td>";
echo "<td>" . $row['bank_acc_name'] . "</td>";
echo "<td>" . $row['bank_acc_no'] . "</td>";
echo "<td>" . $row['bank_swift'] . "</td>";
echo "<td>" . $row['bank_name'] . "</td>";
echo "<td>" . $row['bank_add'] . "</td>";
echo "<td>" . $row['bank_no'] . "</td>";
echo "<td>" . $row['agent'] . "</td>";
echo "<td>" . $row['first_days'] . "</td>";
echo "<td bgcolor=#E6FFF2><b>" . $row['first_payment'] . "</b></td>";
echo "</tr>";
}
echo "</table>";
// Close MySQL
mysqli_close($con);
?>
</body>
</html>
You can use two CSS properties:
<style>
.your-element{
word-wrap:break-word;
word-break:break-all;
}
</style>
Demo
References:
Word Wrap
Word Break

Design php output table with css

Please anyone who can help me with this problem. I have a html file with a question. When submit is clicked it calls a php(which is connected with my database in Oracle) and it shows a table inside the html (using ajax).
I would like to know how can i show that table in a nicer form? Can I design it?
Here is my code in php
<?php
{
session_start();
include "connect.php";
}
$conn = DBConnect();
$stid = DBExecute($conn, "select something...");
$total = 0;
echo "<table>
<tr>
<th>Name1</th>
<th>Name2</th>
</tr>";
while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
echo "<tr><td>";
echo $row['something'];
echo "</td>";
echo "<td>";
echo $row['something'];
echo "</td>";
}
echo "</table>";
?>
and I want to have for example this css instructions for the table i will show...
#newspaper-a
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 45px;
width: 480px;
text-align: left;
border-collapse: collapse;
border: 1px solid #69c;
}
#newspaper-a th
{
padding: 12px 17px 12px 17px;
font-weight: normal;
font-size: 14px;
color: #039;
border-bottom: 1px dashed #69c;
}
#newspaper-a td
{
padding: 7px 17px 7px 17px;
color: #669;
}
#newspaper-a tbody tr:hover td
{
color: #339;
background: #d0dafd;
}
Please does anyone know what should i do to succeed it???
Important the table will be shown inside the same page not in another one..
You need to add your ID to the table div - like this:
<?php
{
session_start();
include "connect.php";
}
$conn = DBConnect();
$stid = DBExecute($conn, "select something...");
$total = 0;
// ADD YOUR ID HERE vvvvvvvv
echo "<table id="newspaper-a">
<tr>
<th>Name1</th>
<th>Name2</th>
</tr>";
while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
echo "<tr><td>";
echo $row['something'];
echo "</td>";
echo "<td>";
echo $row['something'];
echo "</td>";
}
echo "</table>";
?>
http://jsfiddle.net/jakemulley/hs3mF/

Coloring the table with alternate color

Below i have added my css file and my html file i need to color my table with alter name color but its not working i don't know where i have done the mistake.
CSS FILE :-
table.gridtable{
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
float:center;
border-width:1px;
border-style:groove;
border-color:black;
border-collapse:collapse;
width:600px;
}
table.gridtable th{
border-width:1px;
padding:3px;
border-style:solid;
border-color:black;
}
table.gridtable td{
border-width:1px;
padding:3px;
border-style:solid;
border-color:black;
}
.oddrowcolor{
background-color: black;
}
.evenrowcolor{
background-color: red;
}
table.gridtable td.darkcol{
border-top:0px;
border-bottom:0px;
border-right: 1px;
}
table.gridtable td.emptycol{
border-bottom:0px;
border-right: 1px;
}
HTML & PHP:-
<table class="gridtable" id="alternatecolor" align="center"
<tr><th>disease Name/th><th>Gene Name/th><th>OMIM Number</th></tr>
<?php
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$selected = mysql_select_db("missensencemuttation",$dbhandle) or die("Could not select disease");
$result = mysql_query("SELECT DISTINCT * FROM `gene_data` ORDER BY disease_name");
$last=" ";
while($row = mysql_fetch_array($result))
$now=$row[2];
if($last != $now){
$rcnt=mysql_query("select count(gene_name) from gene_data where gene_name='$now'");
if($rcnt==1){
echo "<tr><td>";
echo "<a href='d1.php?d_name=".$row['disease_name']."'>".$row['disease_name'];
echo "</a></td>";
echo "<td>".$row['gene_name']."</td>";
echo "<td>".$row['ommi_number']."</td></tr>";
}
else{
echo "<tr><td class='emptycol'>";
echo "<a href='d1.php?d_name=".$row['disease_name']."'>".$row['disease_name'];
echo "</a></td>";
echo "<td>".$row['gene_name']."</td>";
echo "<td>".$row['ommi_number']."</td></tr>";
}
}
else
{
echo "<tr'>";
echo "<td class='darkcol'>";
echo '&nbsp';
echo "</td>";
echo "<td>".$row['gene_name']."</td>";
echo "<td>".$row['ommi_number']."</td></tr>";
}
$last=$row[2];
can any one help me with this ??
Maybe I'm misunderstanding, but it looks like you want to apply oddrowcolor or evenrowcolor to alternating rows - but in your actual php/html, I don't see those class names anywhere. They're not being put on the rows, so their rules wont be applied. Just add them to the appropriate <tr> tags in your php and it should work.
Alternatively you could use the nth-of-type selector to apply the rules without altering your php:
tr:nth-of-type(odd) {
background-color: black;
}
tr:nth-of-type(even) {
background-color: red;
}
If you want alternate colors for each table row, then a simple CSS should do it. This way, if you want to insert new rows dynamically, it will adjust the colors automatically.
.stripped-rows:nth-child(odd){
background-color:red;
}
.stripped-rows:nth-child(even){
background-color:blue;
}
and html ...
<table>
<tr class = 'stripped-rows'>
<td>sad</td>
</tr>
<tr class = 'stripped-rows'>
<td>sad</td>
</tr>
<tr class = 'stripped-rows'>
<td>sad</td>
</tr>
DEMO

Categories