How to repeat a table on php based on MySQL database? - php

I have made a page on my website that has a table with information displayed whenever a new record is add on my MySQL database here is my code:
<?php
while($r = mysqli_fetch_assoc($queryD))
{
echo "<table width='800' border='0' align='center' style='margin-top: 10px; margin-bottom: 10px; background-color: #FFFFFF;' border-radius: '5px;' cellspacing='0px' cellpadding='2px;'>";
echo "<tbody>";
echo "<tr>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . "Name" . "</td>";
echo "<td width='193' class='generalFONT' style='border: 1px solid black;'>" . "Type" . "</td>";
echo "<td width='98' class='generalFONT' style='border: 1px solid black;'>" . "Phone" . "</td>";
echo "<td width='98' class='generalFONT' style='border: 1px solid black;'>" . "Age" . "</td>";
echo "<td width='148' class='generalFONT' style='border: 1px solid black;'>" . "Gender" . "</td>";
echo "<td width='160' class='generalFONT' style='border: 1px solid black;'>" . "Natunality" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Name"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Type"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Phone"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Age"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Gender"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Natu"] . "</td>";
echo "</tr>";
and here is my query:
$queryD = mysqli_query($con, "SELECT * FROM users");
All the code above is working fine but, my problem here is I want to repeat the table whenever a new user registered on the database. I have multiple record on my database but this code is showing only one table and only the first record. I want to show all the records, each one in a separated table. So how can I do that? hope you understand my question. Thank you

try working with foreach
http://php.net/manual/en/control-structures.foreach.php
this is a example of php.net
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)

Related

Php Dynamic Table Showing Server Date/Time Format

I have Created the Program where I am Fetching the Content from a text file from a cpanel server,
In my Table Fields It shows the Date Format in Server format i.e (YYYY/MM/DD) I want it to Display as
(DD/MM/YYYY).
This is My Code
<?php
$data = file_get_contents("file-path.txt");
$arr = explode(";", $data);
echo "<thead>";
echo "<tr style=\"border: 1px solid black;\">";
echo "<th style=\"border: 1px solid black;\">Doctor Name</th>";
echo "<th style=\"border: 1px solid black;\">Leave From (YYYY/MM/DD)</th>";
echo "<th style=\"border: 1px solid black;\">Leave To (YYYY/MM/DD)</th>";
echo '<th style="border: 1px solid black;">Remarks</th>';
echo "</tr>";
echo "</thead>";
echo "<tbody>";
for($value = 0; $value < count($arr)-1; $value=$value+4) {
echo "<tr style=\"border: 1px solid black;\">";
echo "<td style=\"border: 1px solid black;\">".$arr[$value]."</td>";
echo "<td style=\"border: 1px solid black;\">".$arr[$value+1]."</td>";
echo "<td style=\"border: 1px solid black;\">".$arr[$value+2]."</td>";
echo '<td style="border: 1px solid black;">'.$arr[$value+3].'</td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
You should use date_format() function bro
date_format($arr[$value+2],"d/m/Y");
date('d/m/Y', strtotime($arr[$value+2]));
Have a try, you can do it.

Error while using materialize css table

I have a php file designed to retrieve information from database. The problem is that if i use a normal table tags like the following it would work and show the page for me.
<?php /*Template Name: contactread */
get_header();
$servername = "localhost";
$username = "***** (for security purpose only)";
$password = "*****";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM contact";
$result = mysqli_query($conn,$query);
echo "<table class='table'>
<tr>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>نام</th>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>ایمیل</th>
<th class='tda' style='border-bottom: 1px solid #313131; border-top: 1px solid #313131; border-right: 1px solid #313131;'>پیام</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['name'] . "</td>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['email'] . "</td>";
echo "<td class='tda' style='border-bottom: 1px solid #313131; border-right: 1px solid #313131;'>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
And When i use it like the following which is calling everything from the framework, it gives me the http 500 error. (I included the entire page for showing the first type of table so you have the idea of the page, however i dont see a reason to re write it all again)
echo "<table>
<thead>
<tr>
<th>نام</th>
<th>ایمیل</th>
<th>پیام</th>
</tr>
</thead>
<tbody>";
while($row = mysqli_fetch_array($result))
{
echo <tr>
echo <td>. $row['name'] .</td>
echo <td>. $row['email'] .</td>
echo <td>. $row['message'] .</td>
echo </tr>
}
echo "</tbody>
</table>";
Can someone help me why is this making the error? I couldnt find anything helpful in the internet.
EDIT: I have added materialize-css css files with the link in the header and i have included the header as well.
Simple mistake. You are not wrapping the data you want to echo in quotes, so try doing it like this.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[name]</td>";
echo "<td>$row[email]</td>";
echo "<td>$row[message]</td>";
echo "</tr>";
}

How can I generate tables in php based on MySQL database

I have made a page on my website that has a table with information that supposed to displayed whenever a new record is add on my MySQL database here is my code:
<?php
while($r = mysqli_fetch_assoc($queryD))
{
echo "<table width='800' border='0' align='center' style='margin-top: 10px; margin-bottom: 10px; background-color: #FFFFFF;' border-radius: '5px;' cellspacing='0px' cellpadding='2px;'>";
echo "<tbody>";
echo "<tr>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . "Name" . "</td>";
echo "<td width='193' class='generalFONT' style='border: 1px solid black;'>" . "Type" . "</td>";
echo "<td width='98' class='generalFONT' style='border: 1px solid black;'>" . "Phone" . "</td>";
echo "<td width='98' class='generalFONT' style='border: 1px solid black;'>" . "Age" . "</td>";
echo "<td width='148' class='generalFONT' style='border: 1px solid black;'>" . "Gender" . "</td>";
echo "<td width='160' class='generalFONT' style='border: 1px solid black;'>" . "Natunality" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Name"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Type"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Phone"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Age"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Gender"] . "</td>";
echo "<td width='93' class='generalFONT' style='border: 1px solid black;'>" . $r["Natu"] . "</td>";
echo "</tr>";?>
and here is my query:
$queryD = mysqli_query($con, "SELECT * FROM users");
All the code above is working fine but, my problem here is I want to repeat the table whenever a new user registered on the database. I have multiple record on my database but this code is showing only one table and only the first record. I want to show all the records, each one in a separated table and displaying them by stack them on top of each other. So how can I do that? hope you understand my question. Thank you

How to increase width of a field in html page which has CSS and jQuery

I am new to programming, I have managed to create a page which fetches the data from MySQL.
I have used a jQuery plugin to freeze the first column and first row of my table.
The problem I am facing is, when I try to increase the width of a specific column it is not working.
Please help me to understand how do I increase width for specific column. This is my PHP and CSS for your reference.
Thanks.
PHP
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Region Wise</title>
<link href="css/test.css" rel="stylesheet" media="screen" />
<header class="header">
<div class="titulosHeader">
<h2>Region Wise</h2>
</header>
</head>
<main class="main">
<div class="ContenedorTabla">
<?php
$sql = 'SELECT * from rgn_report';
$result = mysqli_query($con,$sql);
echo "<table id='pruebatabla' class='fht-table hoverTable'>
<thead>
<tr>
<th class='celda_encabezado_general' rowspan='3' style='height:100px;'>REGION</th>
<tr>
<th class='celda_encabezado_general' colspan='4'>KA</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>MI</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>OT</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>FA</th>
<th class='space'></th>
<th class='celda_encabezado_general' colspan='4'>HA</th>
</tr>
<tr>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
<th class='space'></th>
<th class='celda_encabezado_general'>SOH</th>
<th class='celda_encabezado_general'>CY</th>
<th class='celda_encabezado_general'>CT</th>
<th class='celda_encabezado_general'>WC</th>
</tr>
</thead>";
echo "<tbody>";
while ($row = mysqli_fetch_array($result))
{
$rowhighlight = array("RGN1","RGN2","Grand Total");
if (in_array($row['rgn'],$rowhighlight)){
echo "<tr>";
echo "<td class='highlight_cell'>" . $row['rgn']."</td>";
echo "<td class='highlight_cell'>" . number_format($row['KA_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['KA_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['KA_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['KA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['MI_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['MI_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['MI_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['MI_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['OT_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['OT_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['OT_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['OT_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['FA_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['FA_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['FA_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['FA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='highlight_cell'>" . number_format($row['HA_SOH'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['HA_CY'])."</td>";
echo "<td class='highlight_cell'>" . number_format($row['HA_CT'])."</td>";
echo "<td class='highlight_cell'>" . $row['HA_CVR']."</td>";
echo "</tr>";}
else
{
echo "<tr>";
echo "<td class='excel_cell'>" . $row['rgn']."</td>";
echo "<td class='excel_cell'>" . number_format($row['KA_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['KA_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['KA_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['KA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['MI_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['MI_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['MI_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['MI_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['OT_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['OT_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['OT_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['OT_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['FA_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['FA_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['FA_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['FA_CVR']."</td>";
echo "<td class='space'></td>";
echo "<td class='excel_cell'>" . number_format($row['HA_SOH'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['HA_CY'])."</td>";
echo "<td class='excel_cell'>" . number_format($row['HA_CT'])."</td>";
echo "<td class='excel_cell'>" . $row['HA_CVR']."</td>";
echo "</tr>";
}
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
?>
</div>
</main>
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/jquery.CongelarFilaColumna.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#pruebatabla").CongelarFilaColumna({Columnas:1});
});
</script>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
border:0;
}
.header, .main {
display: inline-block;
height: auto;
width: 100%;
}
.titulosHeader {
height: auto;
}
.ContenedorTabla {
height: 540px;
border: 1px solid green;
overflow: auto;
position: relative;
max-width:auto;
}
.fht-table,
.fht-table thead,
.fht-table tfoot,
.fht-table tbody,
.fht-table tr,
.fht-table th,
.fht-table td
{
margin: 0;
}
.fht-table{
height: auto;
width: auto;
border-collapse: collapse;
border-spacing: 0;
border: 0 none;
table-layout:fixed;
}
.fht-table th,.fht-table td {
overflow: hidden;
table-layout:fixed;
}
.fht-table-wrapper,
.fht-table-wrapper .fht-thead,
.fht-table-wrapper .fht-tfoot,
.fht-table-wrapper .fht-fixed-column .fht-tbody,
.fht-table-wrapper .fht-fixed-body .fht-tbody,
.fht-table-wrapper .fht-tbody {
overflow: hidden;
position: relative;
}
.fht-table-wrapper .fht-fixed-body .fht-tbody,
.fht-table-wrapper .fht-tbody {
overflow: auto;
}
.fht-table-wrapper .fht-table .fht-cell {
overflow: hidden;
height: 1px;
}
.fht-table-wrapper .fht-fixed-column,
.fht-table-wrapper .fht-fixed-body {
top: 0;
left: 0;
position: absolute;
}
.fht-table-wrapper .fht-fixed-column {
z-index: 1;
}
.fht-fixed-body .fht-thead table {
margin-right: 20px;
border: 0 none;
}
.celda_encabezado_general {
background-color: #4BACC6;
border: 1px solid black;
color: #ffffff;
text-align: center;
font-size: 12px;
font-family: Calibri, sans-serif;
white-space: nowrap;
}
._Separador{
background-color: #fff;
height: 12px;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
width: 7px;
}
._Separador div{
width: 4px;
}
.celda_normal {
background-color: #fff;
border: 1px solid #ccc;
padding: 2px 4px;
}
.excel_cell{
border: 1px solid black;
color: #222;
text-align: center;
font-size: 11px;
font-weight: normal;
padding: 5px;
empty-cells: show;
font-family: Calibri, sans-serif;
white-space: nowrap;
}
._cell_header{
background-color: #EEE;
}
._cell_Default{
background-color: #FFF;
text-align: left;
}
.excel_cell div{
width: 30px;
height: 20px;
}
.highlight_cell{
border: 2px solid black;
color: #222;
text-align: center;
font-size: 11px;
font-weight: bold;
padding: 5px;
empty-cells: show;
font-family: Calibri, sans-serif;
white-space: nowrap;
}
.hoverTable tr:hover {
background-color: grey;
}
.space{
border: none;
empty-cells: show;
background-color:grey;
width:10px;
}

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

Categories