How to make a table responsive? - php

Given below is my table which i wanna make responsive.
<table style="width:100%" class="table table-hover">
<tr>
<th>Booking Date</th>
<th>Booking Id</th>
<th>Amount</th>
<th>Movie Name</th>
<th>Theatre Name</th>
</tr>
<?php
foreach ( $data as $history )
{
?>
<tr>
<td> <?php print_r( $history->booking_date );?></td>
<td> <?php print_r( $history->booking_id ); ?></td>
<td> <?php print_r( $history->amount ); ?></td>
<?php $movie = Movies::find()->where( [ 'id' => $history->movie_id ] )->one(); ?>
<td><?php print_r( $movie->movie_name ); ?></td>
<?php $theatre = Theatres::find()->where( [ 'id' => $history->theatre_id ] )->one();?>
<td><?php print_r( $theatre->theatre_name ); ?></td>
</tr>
<?php
}
?>
</table>
class="table table-hover" was used so that when i hover over the rows i get a
different color. I changed it to class="table table-responsive" but even then
i found my table not responsive. What should i do to make it responsive?

<style type="text/css">
/*
Generic Styling, for Desktops/Laptops
*/
.table-hover {
width: 100%;
border-collapse: collapse;
}
/* Zebra striping */
tr:nth-of-type(odd) {
background: #eee;
}
th {
background: #333;
color: white;
font-weight: bold;
}
td, th {
padding: 6px;
border: 1px solid #ccc;
text-align: left;
}
tr:hover {
background-color: lightyellow;
}
</style>
<table class="table table-hover">
<tr>
<th>Booking Date</th>
<th>Booking Id</th>
<th>Amount</th>
<th>Movie Name</th>
<th>Theatre Name</th>
</tr>
<?php
foreach ( $data as $history )
{
?>
<tr>
<td> <?php print_r( $history->booking_date );?></td>
<td> <?php print_r( $history->booking_id ); ?></td>
<td> <?php print_r( $history->amount ); ?></td>
<?php $movie = Movies::find()->where( [ 'id' => $history->movie_id ] )->one(); ?>
<td><?php print_r( $movie->movie_name ); ?></td>
<?php $theatre = Theatres::find()->where( [ 'id' => $history->theatre_id ] )->one();?>
<td><?php print_r( $theatre->theatre_name ); ?></td>
</tr>
<?php
}
?>
</table>
try this one
source: https://css-tricks.com/responsive-data-tables/

Related

How to display MySql records in a table with multi-level headers in PHP. Dynamic Pivot Table in PHP using MySqli

How do I generate Dynamic Pivot table in PHP using MySqli
I have records in MySql Database. Table structure is shown here
Now, I want to display the records in a table with multi-level headers., Like
Multi-level table, how I want to display the record.
How do I solve this issue? Please help. Thanks in advance
I have found this code, but does not appear as I want.
<?php
$output = array();
$c=1;
while($result = mysqli_fetch_array( $res ))
{
$output[$c]['headname'] = $result['headname'];
$output[$c]['accyear'] = $result['accyear'];
$output[$c]['fundamt'] = $result['fundamt'];
$output[$c]['fundutiamt'] = $result['fundutiamt'];
$c++;
}
?>
<table border="1">
<tr>
<th></th>
<?php
foreach ($output as $key => $html)
{
echo "<th>Solution ".$key."</th>";
}
?>
</tr>
<tr>
<td>Shipping Line</td>
<?php
foreach ($output as $key => $html)
{
echo "<td>".$html['accyear']."</td>";
}
?>
</tr>
<tr>
<?php
foreach ($output as $key => $html)
{
echo "<td>".$html['headname']."</td>";
}
?>
<td>POL</td>
</tr>
</table>
You can create a new array using the mysql result
<?php
$output = array();
while($result = mysqli_fetch_array( $res ))
{
$key=$result['accyear'];
if(!array_key_exists($key,$output)){
$output[$key]=array();
array_push($output[$key],array(
$result['headcode'] =array(
"fundamt" => $result['fundamt'],
"fundutiamt" => $result['fundutiamt'],
)
));
}
}
?>
print_r($output);
Result
2019 => [ 061182 => [fundamt => 12, fundutiamt => 23] ]
[ 961182 => [fundamt => 2, fundutiamt => 23] ]
You can use this array to make the table in HTML
Use the colspan attribute.
td,
th {
border: 1px solid rgb(190, 190, 190);
padding: 10px;
}
td {
text-align: center;
}
tr:nth-child(even) {
background-color: #eee;
}
th[scope="col"] {
background-color: #696969;
color: #fff;
}
th[scope="row"] {
background-color: #d7d9f2;
}
caption {
padding: 10px;
caption-side: bottom;
}
table {
border-collapse: collapse;
border: 2px solid rgb(200, 200, 200);
letter-spacing: 1px;
font-family: sans-serif;
font-size: .8rem;
}
<table>
<caption>Alien football stars</caption>
<tr>
<th scope="col">Player</th>
<th scope="col" colspan=2>Gloobles</th>
<th scope="col" colspan=2>Za'taak</th>
</tr>
<tr>
<th scope="col"></th>
<th scope="col">2016</th>
<th scope="col">2017</th>
<th scope="col">2016</th>
<th scope="col">2017</th>
</tr>
<tr>
<th scope="row">TR-7</th>
<td>7</td>
<td>4,569</td>
<td>7</td>
<td>4,569</td>
</tr>
<tr>
<th scope="row">Khiresh Odo</th>
<td>7</td>
<td>7,223</td>
<td>7</td>
<td>7,223</td>
</tr>
<tr>
<th scope="row">Mia Oolong</th>
<td>9</td>
<td>6,219</td>
<td>9</td>
<td>6,219</td>
</tr>
</table>
(original table from https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td)

Scrollable Echo DB Table with Fix header

I'm trying to make a scrollable table with fix header. This works well if the data is entered to HTML, but when I try to echo content of my database to the table, the scroll bar messes up and my header is no longer fixed. As seen in the photo, the scroll bar is on each table entry. I've tried .div wrapping up the table body as some did but it doesn't work.
body{
background: lightblue;
}
#tble th {
text-align: left;
background-color: green;
color: white;
}
table{
display: block;
border: 1px solid;
margin-top: 10px;
width: 350px;
}
tbody{
display: block;
height: 50px;
overflow-y: scroll;
}
th {
width: 75px;
}
td {
width: 75px;
}
<html lang="en">
<body>
<table align="center" id="tble">
<thead>
<th style="text-align: center">ID</th>
<th style="text-align: center">Username</th>
<th style="text-align: center">Rights</th>
<th style="text-align: center">Age</th>
</thead>
<tbody>
<tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "account");
$result = mysqli_query($conn, "SELECT * FROM account.log WHERE rights IN ('Admin','User')", MYSQLI_USE_RESULT) or die(mysql_error());
while($row = mysqli_fetch_array( $result )) {
?>
<td style="text-align: center"><?php echo $row['id']; ?></td>
<td style="text-align: center"><?php echo $row['user']; ?></td>
<td style="text-align: center"><?php echo $row['rights']; ?></td>
<td style="text-align: center"><?php echo $row['age']; ?></td>
</tr>
</tbody>
<?php
}
?>
</body>
</table>
Is there something I missed? Hoping someone could point me to the right direction.
<!-- language: lang-html -->
<html lang="en">
<body>
<table align="center" id="tble">
<thead>
<th style="text-align: center">ID</th>
<th style="text-align: center">Username</th>
<th style="text-align: center">Rights</th>
<th style="text-align: center">Age</th>
</thead>
<tbody>
<tr>//move this from here
<?php
$conn = mysqli_connect("localhost", "root", "", "account");
$result = mysqli_query($conn, "SELECT * FROM account.log WHERE rights IN ('Admin','User')", MYSQLI_USE_RESULT) or die(mysql_error());
while($row = mysqli_fetch_array( $result )) {
?>
<tr> //to here
<td style="text-align: center"><?php echo $row['id']; ?></td>
<td style="text-align: center"><?php echo $row['user']; ?></td>
<td style="text-align: center"><?php echo $row['rights']; ?></td>
<td style="text-align: center"><?php echo $row['age']; ?></td>
</tr>
</tbody>
<?php
}
?>
</body>
</table>

What's going wrong with my code

I am targeting a table in the database where the data is stored using serialize() function. So I am trying to unserialize the data & fetching in order to display in the HTML Table. The first row in the table, i.e, table data () is somehow displaying properly. But, the next data (coming through looping ) will be showing on the same line instead of next . Please help me out with this. Also if I am wrong in my explanation, please edit it. Here is my code...
<body style="font-family: verdana; letter-spacing: 2px">
<br><br>
<style>
table, td, th {
border: 1px solid #ddd;
text-align: left;
font-size: 14px;
}
table {
border-collapse: collapse;
width: 90%;
margin: 0 auto;
}
th, td {
padding: 10px;
}
</style>
<table style="border-collapse: collapse; margin: 0 auto" cellpadding="13" border="1" width="80%">
<tr>
<th>Status</th>
<th>Company Name</th>
<th>Address</th>
<th>Your Name</th>
<th>Email</th>
<th>Phone</th>
<th>Website</th>
<th>Software Link</th>
</tr>
<tr>
<?php
$con = mysqli_connect("localhost", "ghgh", "1223546", "rererer");
$que = "SELECT * FROM wp_db7_forms WHERE form_post_id = 6167";
$run = mysqli_query($con, $que);
while ($row = mysqli_fetch_array($run)) {
$form_value = $row['2'];
$form_date = $row['3'];
$array = unserialize($form_value);
$expire = date("Y-m-d", strtotime("+90 days"));
while (list($key, $value) = each($array)) {
?>
<td style="text-align: center;"><?php echo $value; ?></td>
<?php
}
}
?>
</tr>
</table>
</body
You are managing the tr tags wrongly. You really put all dynamic content in one row, since you only generate one opening and one closing tr tag for it -- outside of all loops you have.
So put them inside the outer loop:
<table style="border-collapse: collapse; margin: 0 auto" cellpadding="13" border="1" width="80%">
<tr>
<th>Status</th>
<th>Company Name</th>
<th>Address</th>
<th>Your Name</th>
<th>Email</th>
<th>Phone</th>
<th>Website</th>
<th>Software Link</th>
</tr>
<?php
$con = mysqli_connect("localhost", "ghgh", "1223546", "rererer");
$que = "SELECT * FROM wp_db7_forms WHERE form_post_id = 6167";
$run = mysqli_query($con, $que);
while ($row = mysqli_fetch_array($run))
{ ?>
<tr>
<?php
$form_value = $row['2'];
$form_date = $row['3'];
$array = unserialize($form_value);
$expire = date("Y-m-d", strtotime("+90 days"));
while (list($key, $value) = each($array)) {
?>
<td style="text-align: center;"><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
as #trincot said, you are managing the tr tags wrong, here's a cleaner version of the code separating the php and HTML to keep your code clean and more readable :
<?php
$connection = mysqli_connect('','','','');
$result = mysqli_query($connection,'SELECT .... ');
$rows = [];
while($row = mysqli_fetch_array($result))
{
$rows[] = [
'value' => $row['2'],
'date' => $row['3'],
'expire' => date("Y-m-d", strtotime("+90 days")),
'data' => unserialize($row['2'])
];
}
?>
<table style="border-collapse: collapse; margin: 0 auto" cellpadding="13" border="1" width="80%">
<tr>
<th>Status</th>
<th>Company Name</th>
<th>Address</th>
<th>Your Name</th>
<th>Email</th>
<th>Phone</th>
<th>Website</th>
<th>Software Link</th>
</tr>
<?php foreach($rows as $row) : ?>
<tr>
<?php foreach ($row['data'] as $key => $value) : ?>
<td style="text-align: center;"><?= $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

How to create horizontal scroller inside table?

I have create a table where I have retrieve data from database and put into table. I have a column in my database which is duration. for example duration is 10 so, number of weeks are week1,week2,week3 and so on in the table format. Now, I want to give horizontal scroller of weeks. So, How can I do this ?Please help me.
code:
<table class="table table-bordered">
<thead>
<th>A1</th>
<th>A2</th>
<th>A3</th>
<th>A4</th>
<?php
foreach ($prop as $row)
{
$duration = $row['duration'];
for($i=1; $i<=$duration; $i++)
{
echo "<th>week".$i."</th>";
}
}
?>
<th>A5</th>
<th>Total</th>
</thead>
<tbody>
<?php
foreach ($prop as $row)
{
$duration = $row['duration'];
$role = explode(",", $row['role']);
foreach ($role as $key)
{
$this->db->select('*');
$this->db->from('designation_master');
$where = "id = '".$key."'";
$this->db->where($where);
$sql = $this->db->get();
$res = $sql->result_array();
foreach ($res as $rows)
{
$rol = $rows['designation_name'];
}
?>
<tr>
<td id="s-some"><?php echo $rol; ?></td>
<td id="s-some"></td>
<td id="s-some"></td>
<td id="s-some"></td>
<?php
$duration = $row['duration'];
for($i=1; $i<=$duration; $i++)
{
echo "<td>
<input type='text' name='a' id='a".$i."' style='width: 40px;'/>
</td>";
}
?>
<td id="s-some"></td>
<td id="s-some"></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Thank You
You can make a holder for your table. and place your table inside.
Use overflow and place the table inside an holder. You can check the code below for reference.
.table-box{
max-width: 100px;
overflow-x: scroll;
}
.table-holder{
overflow: hidden;
}
/* Not important -> table styling */
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;
}
<div class="table-holder">
<div class="table-box">
<table >
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
</div>
</div>

PHP, HTML table create

I have little problem with tables when u echo my results.
and this is my code
<?php
//return data if any
while ($client = mysqli_fetch_assoc($result)) {
//output data form each client
//var_dump($client);
?>
<table id="t01">
<tr>
<th>ID</th>
<th>name</th>
<th>lastname</th>
<th>age</th>
<th>Username</th>
<th>Password</th>
</tr>
<tr>
<td><?php echo $client['id'] ?></td>
<td><?php echo $client['emri'] ?></td>
<td><?php echo $client['mbiemri'] ?></td>
<td><?php echo $client['mosha'] ?></td>
<td><?php echo $client['username'] ?></td>
<td><?php echo $client['password'] ?></td>
</tr>
<style>
table {
width:100%;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color:#fff;
}
table#t01 th {
background-color: black;
color: white;
}
</style>
</table>
this is my code and the list its echo the first line like id, name ,lastname... for every result. so i want attributes on the top and then down results (i cant post a photo)
You just Need to reorder your code:
<table id="t01">
<tr>
<th>ID</th>
<th>name</th>
<th>lastname</th>
<th>age</th>
<th>Username</th>
<th>Password</th>
</tr>
<?php
//return data if any
while ($client = mysqli_fetch_assoc($result)) {
//output data form each client
//var_dump($client);
?>
<tr>
<td><?php echo $client['id'] ?></td>
<td><?php echo $client['emri'] ?></td>
<td><?php echo $client['mbiemri'] ?></td>
<td><?php echo $client['mosha'] ?></td>
<td><?php echo $client['username'] ?></td>
<td><?php echo $client['password'] ?></td>
</tr>
><?php } ?>
</table>
You had Everything in youre While so for every Entry everything got printed.
And put your Style Tag in the Head Section of your HTML DOC

Categories