How to populate table cell by column using javascript or php - javascript

I have a html table Image table, and I would like to populate or fill the html table using data from database(mysql). This is the data from my database Image Data. I can not think of anything to put the data into my html table per supplier name. Do you guys know how to do this? Using any of the languages such as PHP, JavaScript.
This is my actual table Actual Image. I already tried it myself but I only get the supplier name. This is the my code to get the supplier name. This should be the expected output Output Image
<thead>
<tr id="tr-1">
<th><th><th><th><th></th></th></th></th></th>
<?php $i = 1; foreach ($supp_name as $key => $value) { ?>
<th colspan="2" class="supplier_name_class"><input type="text" class="supplier_name" placeholder="Supplier Name" id="supplier_name-1" value="<?php echo $value->supplier_name; ?>" style="width: 300px; text-align: center;"></th>
<?php $i++; } ?>
</tr>
<tr id="tr-2">
<th>LN</th>
<th>PR No</th>
<th>Description</th>
<th>Qty</th>
<th>Unit</th>
<?php $i = 1; foreach ($supp_name as $key => $value) { ?>
<th>Price</th>
<th>Total</th>
<?php $i++; } ?>
</tr>
</thead>
<tbody>
<?php $i = 1; foreach ($supp_dtl as $key => $value) { ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>

Related

How to calculate the previous row and show in the current row of the table?

I am fetching some data from the table using codeigniter
My table picture
This is my table that i have fetched from the table.in this i have show the balance column in first row as 10 and next row as 35 how i calculate this using java script or php itself.
My code:
<table class="table datatable-button-html5-basic">
<thead>
<tr><h6>
<th class="col-sm">Bill No</th>
<th class="col-sm" >Date</th>
<th class="col-sm" >Particulars</th>
<th class="col-sm" >Op.Stock</th>
<th class="col-sm-7">Pur/Prod</th>
<th class="col-sm" >Sales/Cons</th>
<th class="col-sm" >Balance</th>
</tr>
</thead>
<tbody>
<?php foreach ($query as $row): ?>
<tr>
<?php $total = 0;
$total += $row['qty']; ?>
<td> <?php echo $row['orderno'];?></td>
<td ><?php echo $row['orderdate'];?></td>
<td >PRODUCTION</td>
<td></td>
<td dir="rtl"><?php echo $row['qty'];?></td>
<td></td>
<td><?php echo "$total" ;?></td>
</tr><?php endforeach ?>
<tr><?php foreach ($query1 as $row1): ?>
<td> <?php echo $row1['billno'];?></td>
<td ><?php echo $row1['billdate'];?></td>
<td ><?php echo $row1['accName'];?></td>
<td></td>
<td></td>
<td dir="rtl"><?php echo $row1['salesqty'];?></td>
<td></td>
</tr>
<?php endforeach ?>
</tbody>
This is my table code.
My another sample table:
Define $balance before foreach, and sum as $balance += $total; every row of balance column.
<table class="table datatable-button-html5-basic">
<thead>
<tr><h6>
<th class="col-sm">Bill No</th>
<th class="col-sm" >Date</th>
<th class="col-sm" >Particulars</th>
<th class="col-sm" >Op.Stock</th>
<th class="col-sm-7">Pur/Prod</th>
<th class="col-sm" >Sales/Cons</th>
<th class="col-sm" >Balance</th>
</tr>
</thead>
<tbody>
<?php
$balance = 0;
foreach ($query as $row): ?>
<tr>
<?php $total = 0;
$total += $row['qty'];
$balance += $total;
?>
<td> <?php echo $row['orderno'];?></td>
<td ><?php echo $row['orderdate'];?></td>
<td >PRODUCTION</td>
<td></td>
<td dir="rtl"><?php echo $row['qty'];?></td>
<td></td>
<td><?php echo "$total" ;?></td>
</tr><?php endforeach ?>
<tr><?php foreach ($query1 as $row1): ?>
<td> <?php echo $row1['billno'];?></td>
<td ><?php echo $row1['billdate'];?></td>
<td ><?php echo $row1['accName'];?></td>
<td></td>
<td></td>
<td dir="rtl"><?php echo $row1['salesqty'];?></td>
<td></td>
</tr>
<?php endforeach ?>
</tbody>

datatables default sort (asc/desc) not working

i tried to fetch data to a mysql table using this
$query=$conn->query("SELECT * FROM users ORDER BY id_user ASC");
this is my table structure
users
id_user | INT auto-increment
username | varchar
password | varchar
nama | varchar
role | varchar
status | SET
but the displayed data order is ordered like this 1,10,11,12,13,2,3,4,5,6,7,8,9
(13 data in database)
Edit: full code to display
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th align="center">ID</th>
<th align="center">Username</th>
<th align="center">Nama</th>
<th align="center">Role</th>
<th width="13%" align="center">Status</th>
<th width="7%" align="center">Edit</th>
<th width="8%" align="center">Hapus</th>
</tr>
</thead>
<?php
$query=$conn->query("SELECT * FROM users ORDER BY id_user ASC");
while($row=$query->fetch()){
$id=$row['id_user'];
$name=$row['username'];
$nama=$row['nama'];
$role=$row['role'];
$status=$row['status'];
?>
<tr>
<td>
<?php echo $id ;?>
</td>
<td>
<?php echo $name ;?>
</td>
<td>
<?php echo $nama ;?>
</td>
<td>
<?php echo $role ;?>
</td>
<td>
<?php echo $status ;?>
</td>
<td>
<button class="alert-success">Edit</button>
</td>
<td>
<button class="alert-success">Delete</button></td>
</td>
</tr>
<?php }?>
</table>
added script for datatable but it return error cannon reinitialize data table
$(document).ready(function (){
var table = $('#example').dataTable({
"order": [[ 0, 'asc' ]]
});
});
Male sure "id_user" field is of "int" data type in DB Table.
And after that try using "aaSorting". Ref: http://legacy.datatables.net/release-datatables/examples/basic_init/table_sorting.html
$(document).ready(function() {
$('#example').dataTable( {
"aaSorting": [[ 0, "asc" ]]
} );
} );
try this :
<td>
<?php echo $id ;?>
</td>
Just remove from display record
for centered data you can use the text-center bootstrap class

want to set the border to table by exporting data to excel sheet in codeingiter

I am having problem setting a border to a table.
When I export the table from an Excel sheet I get the values without a border.
I want to fix the border to the table but it shows an error.
<div class="dvData">
<table>
<thead>
<tr>
<th>Customer Name</th>
<th>Email Id</th>
<th>Mobile Number</th>
<th>Residential Address</th>
<th>Temporary Address</th>
<th>Company name</th>
<th>Company type</th>
<th>Bank Name</th>
<th>Branch Name</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $result) { ?>
<tr>
<td>
<?php echo $result->first_name;?></td>
<td>
<?php echo $result->email;?></td>
<td>
<?php echo $result->phone_num;?></td>
<td>
<?php echo $result->address1;?></td>
<td>
<?php echo $result->address2;?></td>
<td>
<?php echo $result->company_name;?></td>
<td>
<?php echo $result->customer_type;?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
this is my script code:
jQuery(document).ready(function() {
$(".dvData").hide();
EditableTable.init();
$("#btnExport").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('.dvData').html());
e.preventDefault();
});
});
this is php code:
<input type="button" class="btn btn-info pull-right" id="btnExport" value=" Export" />
without setting border am getting output like this .. refer noborder.png table contain data with no border.
When I'm setting border value to table am getting output like this.. border.png
]2
<table border="1" width="1000px" cellspacing="0" cellpadding="0">
Please make this file, suppose export.php :
<?php
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=listing-export.xls");
?>
<table border="1">
<thead>
<tr>
<th>Customer Name</th>
<th>Email Id</th>
<th>Mobile Number</th>
<th>Residential Address</th>
<th>Temporary Address</th>
<th>Company name</th>
<th>Company type</th>
<th>Bank Name</th>
<th>Branch Name</th>
</tr>
</thead>
<tbody>
<?php
foreach ($results as $result)
{
?>
<tr>
<td><?php echo $result->first_name;?></td>
<td><?php echo $result->email;?></td>
<td><?php echo $result->phone_num;?></td>
<td><?php echo $result->address1;?></td>
<td><?php echo $result->address2;?></td>
<td><?php echo $result->company_name;?></td>
<td><?php echo $result->customer_type;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Than make other file such as do_export.php and code as following :
<script>
function exportfile()
{
var export_file = window.open("export.php","", "left=0,top=0,width=500,height=300,toolbar=0,scrollbars=1,status=0");
export_file.focus();
}
</script>
<input type="Button" value="Export" onclick="exportfile()" />
After making above two files, please run second file (do_export.php) and click on Export button.
Thanks...

Echo sum from a table column using php

I have a table like this:
<table>
<tr>
<td>100</td>
</tr><br>
<tr>
<td>200</td>
</tr><br>
<tr>
<td>300</td>
</tr><br>
<tr>
<td>400</td>
</tr><br>
<tr>
<td>500</td>
</tr><br>
<tr>
<td>600</td>
</tr><br><br>
<tr>
<td>Total: </td>
<td>2100</td>
</tr>
</table>
I want to count and display the total of the column as shown above using PHP but I am not sure how to make this possible. Please help.
You could easily do this with javascript document.getElementById("idHere").value and stock the sum of them in a single variable.
The bad part is that you should give every td an Id.
In PHP you should give them name attribute, add a submit button and get the values from the global-variable $_POST["nameOfTD"];
You can use buffer to read the content of the page then parse html:
<?php
ob_start();
?><table>
<tr>
<td class="foo">100</td>
</tr><br>
<tr>
<td class="foo">200</td>
</tr><br>
<tr>
<td>300</td>
</tr><br>
<tr>
<td>400</td>
</tr><br>
<tr>
<td>500</td>
</tr><br>
<tr>
<td>600</td>
</tr><br><br>
<tr>
<td>Total: </td>
<?php
$DOM = new DOMDocument;
$DOM->loadHTML(ob_get_contents());
$items = $DOM->getElementsByTagName('td');
function getClass($item) {
foreach ($item->attributes as $name => $attrNode) {
if ($name == 'class') {
return $attrNode->nodeValue;
}
}
}
$sum = 0;
for ($i = 0; $i < $items->length; $i++) {
$item = $items->item($i);
if (getClass($item) == 'foo') {
$sum += intval($item->nodeValue);
}
}
echo "<td>" . $sum . "</td>";
?>
</tr>
</table>
<?php
ob_end_flush();
?>

display data in single field of a table

I am trying to display name and age in a single field of the table and I want data to be displayed in the place of . I tried the code below but it doesn't work.
CODE:
<?php
$con=mysqli_connect("localhost","root","","dva");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM apform");
while($row = mysqli_fetch_array($result)) {
echo "My Mood: ";
echo $row['mood'];
echo "<br>";
}
?>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Doctor</th>
<th>Telephone</th>
<th>Mobile</th>
<th>Meeting Time</th>
<th>Fee</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td data-title="Name"><span> <?php echo $row['name']; ?> </span></td>
<td data-title="Relationship"><span> </span></td>
<td data-title="Address"><span> </span></td>
<td data-title="Telephone"><span> </span></td>
<td data-title="Mobile"><span> </span></td>
<td data-title="Contact at night"><span> </span></td>
<td data-title="Next of kin"><span> </span></td>
<td data-title="Delete"><span> </span></td>
</tr>
You can easily achieve what you need to putting the while loop inside of your table and echoing out all of the relevant fields.
<?php
$con=mysqli_connect("localhost","root","","dva");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM apform");
while($row = mysqli_fetch_array($result)) {
echo "My Mood: ";
echo $row['mood'];
echo "<br>";
}
?>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Doctor</th>
<th>Telephone</th>
<th>Mobile</th>
<th>Meeting Time</th>
<th>Fee</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($result)) { ?>
<tr>
<td data-title="Name"><span><?php echo $row['name']; ?></span></td>
<td data-title="Relationship"><span><?php echo $row['relationship']; ?></span></td>
<td data-title="Address"><span><?php echo $row['address']; ?></span></td>
<td data-title="Telephone"><span><?php echo $row['telephone']; ?></span></td>
<td data-title="Mobile"><span><?php echo $row['mobile']; ?></span></td>
<td data-title="Contact at night"><span><?php echo $row['contact']; ?></span></td>
<td data-title="Next of kin"><span><?php echo $row['nok']; ?></span></td>
<td data-title="Delete"><span>Delete</span></td>
</tr>
<?php } ?>
</tbody>

Categories