How to display data in a table horizontally? - php

I am trying to display these data horizontally, the problem is it overlaps the headers and it only creates duplicate entry on 1st and 2nd rows of the table.
Here is an example of the problem:
Column A
Column B
Column C
Column D
Column E
No Column
No Column
No Column
No Column
#1
20
30
40
50
30
25
15
10
#2
20
30
40
. 50.
30
25
15
10
What the correct should be is:
Column A
Column B
Column C
Column D
Column E
#1
20
30
40
50
#2
30
25
15
. 10
I've already construct the table like this:
<div class="table-responsive">
<table class="table table-striped table-bordered" style="width:100%">
<tbody class="text-center">
<?php foreach($candidatesf as $candidate): ?>
<tr> <td><?php echo $candidate['fullname'] ?></td>
<?php foreach($candscores as $candscore): ?>
<?php echo "\t<td>"; ?><?php echo $candscore['score']; ?></td>
<?php endforeach; ?>
<tr></tr>
<?php endforeach; ?>
</tbody>
</table>
</div>

You're opening the table row
Then you add some td elements.
At the end, you open another and close that one.
You have a syntax error here:
<tr></tr>
You should only close it like this:
</tr>

<tbody class="text-center">
<?php foreach($candidatesf as $candidate): ?>
<tr>
<td><?php echo $candidate['fullname'] ?></td>
<?php foreach($candscores as $candscore): ?>
<td><?php echo $candscore['score']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>

Related

How to insert new row in the same table using while loop

I'm trying to make 3 tables, I've used this while loop,
the problem that
I didn't know how to make the loop insert the new row in the same table.
It makes new tables for each new row or just mess it up by putting information that should appear in another table.
Please help me.
What I got:
contract name cost period
210 sama 20 3
id date account visit
2 3-jun 20 3
...
contract name cost period
200 mama 10 3
id date account visit
4 10 3 2
....
contract name cost period
210 ana 50 6
id date account visit
6 50 6 5
What I want:
contract name cost period
210 sama 20 3
200 mama 10 3
210 ana 50 6
id date account visit
2 3-jun 20 3
4 10 3 2
6 50 6 5
This is my code:
<?php while($row = mysqli_fetch_array($search_result)):?>
<div style="font-size:1.5rem; padding:0px; margin:0px; position:relative; width:95%; " dir="rtl">
<h1>contract </h1>
<table bgcolor="#fff" align="center" width="70%" border="3" dir="rtl" style="font-size:1.5rem; ">
<tr>
<td>name</td>
<td>cost</td>
<td>period</td>
<tr>
<td><?php echo $row['contract_num'];?></td>
<td><?php echo $row['cost'];?></td>
<td><?php echo $row['bla'];?></td>
<td><?php echo $row['serv'];?></td>
</tr>
</table></div>
<div style="font-size:1.5rem; padding:0px; margin:0px; position:relative; width:95%; " dir="rtl">
<h1>visits</h1>
<table bgcolor="#fff" align="center" width="70%" border="3" dir="rtl" style="font-size:1.5rem; ">
<tr>
<td>id</td>
<td>date</td>
<td>account</td>
<td>visit</td>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['account'];?></td>
<td><?php echo $row['visit'];?></td>
</tr>
</table></div>
<?php endwhile;?>
Read more on tables: https://www.w3schools.com/html/html_tables.asp
thead - define table header. It has one row (tr) with your table's headers (th)
tbody - set the data for the columns. tr = table row, td = table data, or table cell
<table>
<thead> <!-- define the header outside the while, it only needs to be defined once -->
<tr>
<th>Contract</th>
<th>Name</th>
<th>Cost</th>
<th>Period</th>
</tr>
</thead>
<tbody> <!-- in the body loop through all results and print one row for each result -->
<?php while($row...): ?>
<tr>
<td><?php echo $row['contract'] ?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['cost'] ?></td>
<td><?php echo $row['period'] ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>

How Can I Break the Output from same result a PHP While Loop?

For example in while loop the result will be displayed order by vehicle numbers like this
Vehicle num total_amount
TEST V 1234 500
TEST V 1234 500
TEST V 1234 500
TEST w 785 1000
TEST w 785 1000
TEST Z 589 700
TEST Z 589 700
TEST Z 589 700
But i want to like this.
Vehicle num total_amount
TEST V 1234 500
TEST V 1234 500
TEST V 1234 500
Vehicle No: TEST V 1234
Total amount: 1500
TEST w 785 1000
TEST w 785 1000
Vehicle No: TEST w 785
Total amount: 2000
TEST Z 589 700
TEST Z 589 700
TEST Z 589 700
Vehicle No: TEST Z 589
Total amount: 2100
I want display like dis using php + mysql
how to solve this problem, can anybody tell?
here my code example?
<table class="table table-table-striped accord-content even" width="100%" style="clear:both;" id="textttt">
<thead>
<tr class="bg-green" style="background:#DBB74E!important; color:#698602 !important" >
<th>S No</th>
<th>Owner Name</th>
<th>Truck Number</th>
<th>total</th>
</tr>
</thead>
<?php
$query_all = mysql_query("select * from testing where ownername='" . $_POST['ownername'] . "' and dc_date between '".$weekenddate."' and '" . $_POST['datewe'] . "' and status='Verified' order by truckk_number ASC");
while ($fet_all = mysql_fetch_array($query_all)) { ?>
<tr class="accord-content even bg-gray" style="color:#698602 !important;">
<td><?php echo $fet_all['id']; ?></td>
<td><?php echo $fet_all['ownername']; ?></td>
<td><?php echo $fet_all['truckk_number']; ?></td>
<td><?php echo $fet_all['total']; ?></td>
</tr>
<?php } ?>
</table>
I want display vehicle wise total and vehicle number.
Because there is no code in your question, I use pseudo code here
Set track = null, total = 0
Loop through your collection
If track is not null and current item vehicle_num is different from track
Print current vehicle_num
Print total
Set total = 0
else
Update total = total + current total_amount
endif
Set track = current item vehicle_num
Print current item info
endloop
For your code, consider replacing this piece:
while ($fet_all = mysql_fetch_array($query_all)) { ?>
<tr class="accord-content even bg-gray" style="color:#698602 !important;">
<td><?php echo $fet_all['id']; ?></td>
<td><?php echo $fet_all['ownername']; ?></td>
<td><?php echo $fet_all['truckk_number']; ?></td>
<td><?php echo $fet_all['total']; ?></td>
</tr>
<?php } ?>
to this:
<?php
#other code
$track = null;
$total = 0;
while ($fet_all = mysql_fetch_array($query_all)) :?>
<?php if ($track !== null && $track !== $fet_all['truckk_number']): ?>
<tr>
<td colspan="3">Vehicle No:</td>
<td><?php echo $track; ?></td>
</tr>
<tr>
<td colspan="3">Total:</td>
<td><?php echo $total; ?></td>
</tr>
<?php $total = $fet_all['total']; ?>
<?php else: ?>
<?php $total += $fet_all['total']; ?>
<?php endif; ?>
<tr class="accord-content even bg-gray" style="color:#698602 !important;">
<td><?php echo $fet_all['id']; ?></td>
<td><?php echo $fet_all['ownername']; ?></td>
<td><?php echo $fet_all['truckk_number']; ?></td>
<td><?php echo $fet_all['total']; ?></td>
</tr>
<?php $track = $fet_all['truckk_number']; //add this line ?>
<?php endwhile; ?>
<?php if ($total > 0): ?>
<tr>
<td colspan="3">Vehicle No:</td>
<td><?php echo $track; ?></td>
</tr>
<tr>
<td colspan="3">Total:</td>
<td><?php echo $total; ?></td>
</tr>
<?php endif; ?>
your query is something like this...
SELECT *, SUM(total_amount) FROM YOUR_TABLENAME GROUP BY Vehicle_num_COLUMN_NAME
use GROUP BY clause.

Display each color separate with values

I am having trouble in printing values from database.
ITEM TABLE
ITEM | COLOR | MATERIAL | DIMENSIONS | CATEGORY | QUANTITY
- 01 33 05 111 12 1000.00
- 02 33 07 125 18 200.00
- 03 33 11 156 18 254.00
- 04 56 15 25 66 113.00
- 05 66 05 11 33 521.00
I am trying to print values in table(for each color print material dimension category)
So the output will be:
COLOR - > 33
MATERIAL | DIMENSION | CATEGORY | QUANTITY
05 111 12 1000.00
07 125 18 200.00
11 156 18 254.00
COLOR - > 56
MATERIAL | DIMENSION | CATEGORY | QUANTITY
15 25 66 113.00
COLOR - > 66
MATERIAL | DIMENSION | CATEGORY | QUANTITY
05 11 33 521.00
I am using query
$query = "SELECT a.itemnb, b.colorname, c.materialname, d.categoryname, sum(a.quantity) as quantity
FROM dbo_items a
JOIN dbo_color b
ON a.color=b.colorid
JOIN dbo_material c
on a.material=c.material
JOIN dbo_category
on a.category=d.categoryid
GROUP BY b.colorname, c.materialname, d.categoryname, ";
I am using PDO.
$q=$conn->query($query);
Now I can fetch all values in table, but that is not actually I want to make.
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php while ($r = $m->fetch()){?>
<tr>
<td><?=$r['colorname']?></td>
<td><?=$r['materialname']?></td>
<td><?=$r['categoryname']?></td>
<td><?=$r['quantity ']?></td>
<?php } ?>
</tbody>
</table>
I want to print first color and then all material related to that color.
I am having trouble there, any help or advice is appreciated?
First remove the colorname field from group by clause in query and add this column with order by means add order by colorname in query.
And then change from HTML and php code with the following:
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php
$tempColor = '';
while ($r = $m->fetch()){
if($tempColor != $r['colorname']) {
?>
<tr><td colspan="4">Color Name: <?=$r['colorname']?></td></tr>
<?php $tempColor = $r['colorname'];
} else {
?>
<tr>
<td><?=$r['colorname']?></td>
<td><?=$r['materialname']?></td>
<td><?=$r['categoryname']?></td>
<td><?=$r['quantity ']?></td>
<?php }
}
?>
</tbody>
</table>
You have to ORDER BY 'COLOR' and not to GROUP BY 'COLOR'
After that yout test if you are switching to a new color
<?php
<table class="table table-bordered table-striped">
<thead>
<tr class="bg-primary">
<td data-field="color">COLOR</td>
<td data-field="material">MATERIAL</td>
<td data-field="dim">DIMENSIONS</td>
<td data-field="quantity">QUANTITY</td>
</tr>
</thead>
<tbody>
<?php
$prevColor = '';
while ($r = $m->fetch()){?>
<tr>
<td><? print ($prevColor == $r['colorname'] ? '' : $r['colorname']) ?></td>
<td><? print $r['materialname']?></td>
<td><? print $r['categoryname']?></td>
<td><? print $r['quantity ']?></td>
<?php
$prevColor = $r['colorname'];}
?>
</tbody>
</table>
UPDATE
If you can not change your query, so you have to order by COLOR your resultset
Check if the current colorname is equal to the previous one:
<?php
$currcolor=array();
$i=0;
while ($r = $m->fetch()){
$currcolor[$i]=$r['colorname']; ?>
<tr>
<td>
<?php if($currcolor[$i] != $currcolor[$i-1]){
echo $currcolor[$i];
} ?>
</td>
<td><?php echo $r['materialname']; ?></td>
<td><?php echo $r['categoryname']; ?></td>
<td><?php echo $r['quantity ']; ?></td>
<?php
$i++;
} ?>
The easiest approach is to make one query for getting all colors, then you can make loop for result from first query and use another query for getting all information related to specific color.
Example:
$q = 'SELECT * FROM dbo_color';
$q=$conn->query($query);
while ($res = $q->fetch()){
$secondQ = 'SELECT * FROM relatedMaterials WHERE color = ' . $res->color;
}
And include in this query all html you need for creating table.
I hope this will help you.

Dynamic rowspan in php while loop

I need to achieve a result like this through the values in database:
------------------------------------------------------------------------
Question Marks
------------------------------------------------------------------------
Question 1 |
-------------------------------------------------|
Question 2 | 5
-------------------------------------------------|
Question 3 |
but when I do like this:
<?php
while ( $PQRr = mysql_fetch_array($PQRq) ) {
?>
<tr>
<td align="center"><?php echo $PQRr["point_title"]; ?></td>
<td align="center" rowspan="5">
<?php echo $marks_obtained; ?>
</td>
</tr>
<?php } ?>
It actually prints the the last column number of times query executes. How can I make it print just one time?
Here's what I am getting currently.
Try this. Now that part will only be executed once.
<?php
$i=0;
while ( $PQRr = mysql_fetch_array($PQRq) ) {
?>
<tr>
<td align="center"><?php echo $PQRr["point_title"]; ?></td>
<td align="center" rowspan="5">
<?php if(0==$i++) {echo $marks_obtained;} ?>
</td>
</tr>
<?php } ?>

Bootstrap3 fixed column for table

I checked thrice in SOF, for a solution regarding Bootstrap framework 3.
Even this demo is not working in my system. http://jsfiddle.net/koala_dev/4XG7T/2/
So, I have a matrix type table that will populate 'n' number of records both horizontally and vertically.
My below code is working fine if there is 5-8 columns, but if there are more than 20 columns the table is collapsed.
In other words, I need something that will keep the header part alone fixed. i.e first row with 'n' number of columns.
How can I achieve that? Could someone redefine my below code.
<div class="container">
<form action="post.php" method="post">
<div class="table responsive">
<table class="table table-striped table-hover" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<th>S.No</th>
<?php while($row = mysql_fetch_array( $myloop )) { ?>
<th><?php echo $row['book_name']; ?></th>
<?php } ?>
</tr>
<tr>
<?php while($row1 = mysql_fetch_array( $myloop1 )) { ?>
<td><?php echo $row1['author_name']; ?></td>
<td><?php echo $row1['author_rating']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
</div>
</form>
</div>
To make it simple I want the below row to be fixed so that I can scroll horizontally and check the records.
<tr>
<th>S.No</th>
<?php while($row = mysql_fetch_array( $myloop )) { ?>
<th><?php echo $row['book_name']; ?></th>
<?php } ?>
</tr>
Any help?
Thanks,
Kimz

Categories