I got a homework that tells me to connect MySQL database to our PHP files and echo it in an HTML table. I managed to connect it, but my problem is I cannot style the table, the other element seems fine, so I think the stylesheet is linked properly.
This is my PHP ->
<div id="stats" class="stats">
<center><h2>Statistic.</h2></center>
<table>
<tr>
<th>Number</th>
<th>Degree</th>
<th>Institution</th>
<th>Graduate</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "my_edu");
if ($conn -> connect_error) {
die("Connection Failed:". $conn -> connect_error);
}
$sql = "SELECT * FROM edu";
$result = $conn -> query($sql);
if ($result -> num_rows > 0) {
while ($row = $result -> fetch_assoc()) {
echo "<tr><td>". $row["number"] ."</td><td>". $row["degree"] ."</td><td>". $row["name"] ."</td><td>". $row["graduate"] ."</td></tr>";
}
}
else {
echo "0 result";
}
mysqli_close($conn);
?>
</table>
</div>
The table is printed, as you can see here
and this is the one that was supposed to style the table
.stats table{
width: 800px;
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
text-align: center;
}
/*STATISTIC*/
.stats{
width: 100%;
height: 600px;
background: #FFFFFF;
}
.stats h2{
padding: 50px;
font-weight: bold;
font-size: 40px;
}
.stats h3{
padding-top: 100px;
font-weight: bold;
font-size: 50px;
}
.stats table{
width: 800px;
border: 1px solid black;
border-collapse: collapse;
padding: 3px;
text-align: center;
}
<div id="stats" class="stats">
<center><h2>Statistic.</h2></center>
<table>
<tr>
<th>Number</th>
<th>Degree</th>
<th>Institution</th>
<th>Graduate</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "my_edu");
if ($conn -> connect_error) {
die("Connection Failed:". $conn -> connect_error);
}
$sql = "SELECT * FROM edu";
$result = $conn -> query($sql);
if ($result -> num_rows > 0) {
while ($row = $result -> fetch_assoc()) {
echo "<tr><td>". $row["number"] ."</td><td>". $row["degree"] ."</td><td>". $row["name"] ."</td><td>". $row["graduate"] ."</td></tr>";
}
}
else {
echo "0 result";
}
mysqli_close($conn);
?>
</table>
</div>
You have to apply css on table, thead,th,td ,tr. You'r applying css on which is parent div.
here's a example
*{
font-family:sans-serif;
}
table tr td,th:not(.th-head){
border-right: 1px solid blue;
}
table,tfoot{
border:1px solid #a8afcc;
word-break: break-all;
}
table:nth-of-type(1) caption {
font-weight: 800;
}
table {
border-collapse: collapse;
border-spacing:1rem 0.1rem;
table-layout:fixed;/*auto viene por defecto*/
width: 100%;
empty-cells: hide;
text-align:center
}
thead{
color:white;
background:rgb(25, 27, 151) ;
}
thead th{
/* border: none; */
}
table tbody tr:not(tfoot):nth-child(odd) {
background: #e1e6f8;
}
table tbody tr:nth-child(even) {
background: #c3cdff;
}
tr:not(.tr-head):hover{
background:rgb(130, 130, 130);
}
td:hover{
background:rgb(126, 126, 126);
}
td{
text-align: right;
}
th{
text-align:center;
}
<table >
<thead>
<tr class="tr-head">
<th class="th-head">Col 1</th>
<th class="th-head">Col 2</th>
<th class="th-head">Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>300</td>
<td>2</td>
<td>600</td>
</tr>
<tr>
<td>1000</td>
<td>40</td>
<td>4000</td>
</tr>
</tbody>
</table>
<div id="stats" class="stats">
<h2>Statistic.</h2>
<table>
<tr>
<th>Number</th>
<th>Degree</th>
<th>Institution</th>
<th>Graduate</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "my_edu");
if ($conn -> connect_error) {
die("Connection Failed:". $conn -> connect_error);
}
$sql = "SELECT * FROM edu";
$result = $conn -> query($sql);
if ($result -> num_rows) {
while ($row = $result -> fetch_assoc()) {
?>
<tr>
<td><?= $row["number"] ?></td>
<td><?= $row["degree"] ?></td>
<td><?= $row["name"] ?></td>
<td><?= $row["graduate"] ?></td>
</tr>
<?php
}
}
else {
echo "0 result";
}
mysqli_close($conn);
?>
</table>
</div>
<link rel="stylesheet" href="yourStyleCode.css">
I went ahead and made a little tweak on your css
/*STATISTIC*/
.stats{
width: 100%;
height: 600px;
background: #FFFFFF;
}
.stats h2{
padding: 50px;
font-weight: bold;
font-size: 40px;
}
.stats th {
padding-top: 12px;
padding-bottom: 12px;
text-align: center;
background-color: #0e76a7;
color: white;
}
.stats table {
width: 75%;
margin: auto;
border-radius: 9px;
padding: 3px;
text-align: center;
}
.stats td, .stats th {
border: 1px solid #ddd;
padding: 8px;
}
With the stats id you scoped your style. Means that if you style something inside the scope it have not affect on other part your website. But you need the right selectors to go inside the scope. .stats table tr {...} for example. In precompiled CSS like SASS etc. it quite easy to write the style. But this is only a note.
.scope {
tr, td { ... }
}
Related
I am a new developer. I am now doing a search function for multiple tables. The issue is when I search it only appears the jobname but not the whole information in the same table. The output is supposed to show the work information together with the jobname searched. I hope you guys can help me. Thank you in advance !! Below is my current output before & after search with codes attached.
Before search
After search
Codes
<!DOCTYPE html>
<html>
<head>
<title>My Job</title>
<script>
//searching
function myFunction() {
var input, filter, table, tr, td, i,alltables;
alltables = document.querySelectorAll("table[data-name=mytable]");
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
alltables.forEach(function(table){
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none"
}
}
}
});
}
</script>
<link rel="stylesheet" type="text/css" href="joblist.css">
</head>
<body><br>
<div class="title">
<center>APPLY JOB<center>
</div><br>
<ul align="center">
<li>HOME</li>
</ul>
<br><br>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search your job" class="carian">
<br><br>
<ol>
<?php
include("DB.php");
$query = "SELECT * FROM createjob";
$result = mysqli_query($link,$query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)){
$id = $row["id"];
$jobname = $row["jobname"];
$jobtime = $row["jobtime"];
$jobday = $row["jobday"];
$venue = $row["venue"];
$worker = $row["worker"];
$inform = $row["inform"];
$phonenum = $row["phonenum"];
?>
<fieldset class="box" >
<table data-name="mytable">
<tr>
<th align="left">Job Name </th>
<td><?php echo $jobname ; ?></td>
</tr>
<tr>
<th align="left">Job Time </th>
<td><?php echo $jobtime; ?></td>
</tr>
<tr>
<th align="left">Job Day </th>
<td><?php echo $jobday; ?></td>
</tr>
<tr>
<th align="left">Venue </th>
<td><?php echo $venue; ?></td>
</tr>
<tr>
<th align="left">Number Of Worker </th>
<td><?php echo $worker; ?></td>
</tr>
<tr>
<th align="left">Phone Number </th>
<td><?php echo $phonenum; ?></td>
</tr>
<tr>
<th align="left">Information </th>
<td><?php echo nl2br($inform);?></td>
</tr>
</table>
<br>
<button class="bottom">Apply</button>
</fieldset>
<?php
}
}else {
echo "0 results";
}
?>
</ol>
</body>
</html>
body{
background-image: linear-gradient(rgba(0, 0, 0, 0.45),
rgba(0, 0, 0, 0.45)), url("16.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
border-collapse: collapse;
}
thead{
background-color: #FFD34E;
}
.bottom{
background-color: #FFD34E;
border-radius: 4px;
height: 30px;
padding-top: 5px;
width: 100px;
border: none;
}
.box
{
background-color:white;
border-radius: 10px;
float:left;
width:320px;
border-style: outset;
bottom:10px;
}
.carian{
position: relative;
left:55px;
height:20px;
width: 190px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 5px;
font-size: 16px;
background-color: white;
background-image: url('18.png');
background-position: 7px 0px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
.carian:focus {
width: 20%;
}
.title{
font-weight:bold;
font-size:40px;
color:white;
text-shadow: 4px 4px black;
}
.bottom
{
background-color: #FFD34E;
border-radius: 4px;
height: 30px;
padding-top: 5px;
border: none;
width:100%;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #FFD34E;
position: -webkit-sticky; /* Safari */
position: sticky;
width: 100%;
}
li {
float: center;
}
li a {
display: block;
color: black;
text-align: center;
padding: 20px 340px;
text-decoration: none;
}
li a:hover {
color: white;
background-color:#552B00;
}
This is maybe a better way to create your table:
<fieldset class="box">
<table id="myTable" data-name="mytable">
<thead>
<tr>
<th>Job Name</th>
<th>Job Time</th>
<th>Job Day</th>
<th>Venue</th>
<th>Number Of Worker</th>
<th>Phone Number</th>
<th>Information</th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td><?= $row["jobname"]; ?></td>
<td><?= $row["jobtime"]; ?></td>
<td><?= $row["jobday"]; ?></td>
<td><?= $row["venue"]; ?></td>
<td><?= $row["worker"]; ?></td>
<td><?= $row["inform"]; ?></td>
<td><?= $row["phonenum"]; ?></td>
<td>
<button class="bottom">Apply</button>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</fieldset>
With a normal table like this your able to use the search function from w3schools:
https://www.w3schools.com/howto/howto_js_filter_table.asp
I also made an extended version of the example from w3schools. This function will search in all td fields:
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
var tds = tr[i].getElementsByTagName("td");
console.log(tds.length);
var found = false;
for (var ii = 0, c = tds.length; ii < c; ii++) {
var td = tds[ii];
if (td && found == false) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
found = true;
} else {
found = false
}
}
if (found) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
this is my first post and im already totally confused about my project right now :x
im able to make a successful connection to the sql db and also receive my data which displays on the top left, BUT its supposed to display in a html table, not somewhere on the screen :O
Here is the website to see the code in action, though there is none yet!
http://blackskill.square7.ch/
Here is the code which is running fine, but maybe im just too stupid and new for coding yet xd
<!DOCTYPE html>
<?php
$connection=mysqli_connect("localhost","username","password","db_name");
if ($connection) {
echo "database online <br>";
} else {
die("Connection failed. Reason: ".mysqli_connection_error());
}
$sql="SELECT * FROM blackskill_playerdb";
$results=mysqli_query($connection,$sql);
if (mysqli_num_rows($results)>0) {
while($row=mysqli_fetch_array($results)) {
echo '<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
<td>'.$row[3].'</td>
</tr>';
echo "<br>";
}
}
mysqli_close($connection);
?>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 20%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}
#myTable th, #myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>
<h2><center>Inofficial S.K.I.L.L. - Special Force 2 - Dishonor List</center></h2>
<input type="text" id="myInput" onkeyup="aa" placeholder="Search for player..." title="Type in a name">
<table id="myTable">
<tr class="header">
<th style="width:40%;">Player</th>
<th style="width:20%;">Clan</th>
<th style="width:20%;">Evidence</th>
<th style="width:20%;">Added to list</th>
<?php while($row=mysqli_fetch_array($results)) : ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['clan']; ?></td>
<td><?php echo $row['rulebreak']; ?></td>
<td><?php echo $row['addlist']; ?></td>
</tr>
<?php endwhile ?>
</tr>
</table>
<tbody>
</body>
</html>
i hope we can find an answer together with friendship, emotions and friendship!
regards
I want to apply filter to my html table for following:
1. Search name/emailid
2. Page Length (As my table has thousands of records)
Also, I want to give an option to update record where I enter name, all information should be autopopulated to that form and then user can update whatever he wants to.
Following is my code given:
<?php
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'contacts'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = 'SELECT *
FROM usait LIMIT 50' ;
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>
<html>
<head>
<title>Displaying MySQL Data in HTML Table</title>
<style type="text/css">
<link rel="stylesheet" href="http://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
<script src="http://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
body { {
font-size: 15px;
color: #343d44;
font-family: "segoe-ui", "open-sans", tahoma, arial;
padding: 0;
margin: 0;
}
table {
margin: auto;
font-family: "Lucida Sans Unicode", "Lucida Grande", "Segoe Ui";
font-size: 12px;
}
h1 {
margin: 25px auto 0;
text-align: center;
text-transform: uppercase;
font-size: 17px;
}
table td {
transition: all .5s;
}
/* Table */
.data-table {
border-collapse: collapse;
font-size: 14px;
min-width: 537px;
}
.data-table th,
.data-table td {
border: 1px solid #e1edff;
padding: 7px 17px;
}
.data-table caption {
margin: 7px;
}
/* Table Header */
.data-table thead th {
background-color: #508abb;
color: #FFFFFF;
border-color: #6ea1cc !important;
text-transform: uppercase;
}
/* Table Body */
.data-table tbody td {
color: #353535;
}
.data-table tbody td:first-child,
.data-table tbody td:nth-child(4),
.data-table tbody td:last-child {
text-align: right;
}
.data-table tbody tr:nth-child(odd) td {
background-color: #f4fbff;
}
.data-table tbody tr:hover td {
background-color: #ffffa2;
border-color: #ffff0f;
}
/* Table Footer */
.data-table tfoot th {
background-color: #e5f5ff;
text-align: right;
}
.data-table tfoot th:first-child {
text-align: left;
}
.data-table tbody td:empty
{
background-color: #ffcccc;
}
</style>
</head>
<body>
<table class="data-table" id="example">
<caption class="title">US Data</caption>
<thead>
<tr>
<th>id</th>
<th>ContactOwner</th>
<th>LeadSource</th>
<th>First_name</th>
<th>Last_name</th>
<th>AccountName</th>
<th>Title</th>
<th>EmailID</th>
<th>Department</th>
<th>Industry</th>
<th>Phone</th>
<th>Mobile</th>
<th>Fax</th>
<th>DOB</th>
<th>Assistant</th>
<th>AsstPhone</th>
<th>ReportsTo</th>
<th>LinkedIn</th>
<th>CallStatus</th>
<th>Street</th>
<th>OtherStreet</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Country</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['ContactOwner'].'</td>
<td>'.$row['LeadSource'].'</td>
<td>'.$row['First_name'].'</td>
<td>'.$row['Last_name'].'</td>
<td>'.$row['AccountName'].'</td>
<td>'.$row['Title'].'</td>
<td>'.$row['EmailID'].'</td>
<td>'.$row['Department'].'</td>
<td>'.$row['Industry'].'</td>
<td>'.$row['Phone'].'</td>
<td>'.$row['Mobile'].'</td>
<td>'.$row['Fax'].'</td>
<td>'.$row['DOB'].'</td>
<td>'.$row['Assistant'].'</td>
<td>'.$row['AsstPhone'].'</td>
<td>'.$row['ReportsTo'].'</td>
<td>'.$row['LinkedIn'].'</td>
<td>'.$row['CallStatus'].'</td>
<td>'.$row['Street'].'</td>
<td>'.$row['OtherStreet'].'</td>
<td>'.$row['City'].'</td>
<td>'.$row['State'].'</td>
<td>'.$row['Zip'].'</td>
<td>'.$row['Country'].'</td>
<td>'.$row['Description'].'</td>
</tr>';
$no++;
}?>
</tbody>
<tfoot>
<tr>
<th colspan="26">TOTAL</th>
<th><?=number_format($total)?></th>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function() {
$('#example').DataTable( {
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, 100, "All"]]
} );
} );
</script>
</body>
</html>
Use server-side processing feature of DataTables instead of rendering inside HTML. With server-side processing able to handle large data sets. Also other features will be work like a charm filtering, searching, pagination etc. as you want.
DataTables provide simple example for this. Frontend and server-side scripts are both available, you have to just configure your MySQL connection and will be ready to go.
I have one table which is displaying proper in desktop. I want to change the layout or structure of table in mobile. Please check below image. I have to display like that. I have to display all the information in one row in mobile.Would you help me in this?
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
</tr>
</thead>
<tbody>
<?php
if (isset($result_all_records->num_rows) > 0) {
// output data of each row
while($row = $result_all_records->fetch_assoc()) {
echo "
<tr>
<td>{$row['id']}</td>
<td>{$row['Name']}</td>
<td>{$row['Email']}</td>
<td>{$row['Mobile']}</td>
";
}
}
else {
echo "No records found";
}
?>
</tbody>
</table>
I found the solution from http://codepen.io/anon/pen/QwPVNW
We have to add `data-label='column name' and need to use below code in media queries. it will work
echo "
<tr>
<td data-label='id'>{$row['id']}</td>
<td data-label='Name'>{$row['Name']}</td>
<td data-label='Email'>{$row['Email']}</td>
<td data-label='Mobile'>{$row['Mobile']}</td>
";
#media only screen and (max-width: 384px) {
table {
border: 0;
}
table thead {
display: none;
}
table tr {
margin-bottom: 10px;
display: block;
border-bottom: 2px solid #ddd;
}
table td {
display: block;
text-align: left;
font-size: 13px;
border-bottom: 1px dotted #ccc;
}
table td:last-child {
border-bottom: 0;
}
table td:before {
content: attr(data-label);
float: left;
text-transform: uppercase;
font-weight: bold;
margin-right: 20px;
}
}
So I've been working on a small system which involves codes which is used for a user to redeem them for some sort of reward.
So far I have this script in PHP and HTML:
http://pastebin.com/UUsEKpev
It's only showing one result which you can see here, I want it to display multiple results in a table going down showing all results.
<?php
$yn;
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("mcv") or die(mysql_error());
$result = mysql_query("SELECT * FROM Codes")
or die(mysql_error());
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
if ($row['Used'] == 1) {
$yn = "Yes";
}
else{
$yn = "No";
}
?>
<html>
<head>
<title>Minecraft Codes</title>
<style type="text/css">
tbody {
display: table-row-group;
vertical-align: middle;
border-color: inherit;
}
tr {
display: table-row;
vertical-align: inherit;
border-color: inherit;
}
th {
border: 1px solid #C3C3C3;
padding: 3px;
vertical-align: top;
width: 20%;
background-color: #E5EECC;
}
table.reference {
background-color: white;
border: 1px solid #C3C3C3;
border-collapse: collapse;
width: 50%;
}
table.reference td {
border: 1px solid #C3C3C3;
padding: 3px;
vertical-align: top;
width: 20%;
}
table, th, td, input, textarea {
font-size: 100%;
}
body, p, h1, h2, h3, h4, table, td, th, ul, ol, textarea, input {
font-family: verdana,helvetica,arial,sans-serif;
}
</style>
</head>
<body>
<center>
<br />
<h1><u>Minecraft Server VIP Codes</u></h1>
<br />
<table class="reference">
<tr>
<th>Code</th>
<th>Used?</th>
</tr>
<?php echo "<tr><td>".$row['Code']."</td><td>".$yn."</td></tr>"; ?>
</table>
</center>
</body>
</html>
Your problem is that you are only fetching one row:
$row = mysql_fetch_array( $result );
That line fetches the current row of the result set. You want to do that in a loop:
$rows = array();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
If you can please avoid using the mysql_ functions, they are deprecated, see the giant warning here and read this
<table class="reference">
<tr>
<th>Code</th>
<th>Used?</th>
</tr>
<?php while ($row = mysql_fetch_array($result)): ?>
<?php
if ($row['Used'] == 1) {
$yn = "Yes";
}
else{
$yn = "No";
}
?>
<tr>
<td><?php echo $row['Code']; ?></td>
<td><?php echo $yn; ?></td>
</tr>
<?php endwhile; ?>
</table>
Well yes, you call mysql_fetch_row() only a single time, thus you only retrieve a single row. You have to wrap that into a loop that is executed as many times as there are rows.
There are millions of examples on the internet that you can learn from...