how display every similar rows in separate table? - php

i'm creating a table that receive orders from users, the C_ID will store in (StationaryCustomersInfo) table where the C_ID is unique, and the table (StationaryOrders) will store every item with the C_ID, the C_ID in (StationaryOrders) table is not unique because C_ID will repeated with every items belongs to that C_ID.
i want to display every orders by the C_ID where every orders for each customer will displayed with separate table.
$sql=mysqli_query($link, "SELECT Item, Personal, Department, Quantity, Color FROM StationaryOrders, StationaryCustomersInfo WHERE StationaryCustomersInfo.C_ID = StationaryOrders.C_ID");
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
if($sql){
while($row=mysqli_fetch_assoc($sql)){
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
}
}
?>
</table>
I expect to display each customers orders with separate table and button belongs to that table.

This can be done by adding in your query the C_ID and sort the result by C_ID. Then, in php, you can check if the previous fetch C_ID is the same than the current one.
In example :
// Let's factorise the building of the <table> header there to avoid duplication in code
function ShowTableHeader($cid)
{
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<caption>Orders of user <?php echo $cid; ?> : </caption>
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
}
$query = "SELECT sci.C_ID, "
. "Item, "
. "Personal, "
. "Department, "
. "Quantity, "
. "Color "
."FROM StationaryOrders so "
."LEFT JOIN StationaryCustomersInfo sci "
."ON sci.C_ID = so.C_ID "
."ORDER BY sci.C_ID";
$sql = mysqli_query($link, $query);
if ($sql)
{
if ($row=mysqli_fetch_assoc($sql))
{
// initialize the first C_ID for the first result
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
do
{
// different C_ID than before ? Let's build a new <table>
if ($previousCID != $row['C_ID'])
{
echo "</table>";
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
}
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
} while ($row=mysqli_fetch_assoc($sql));
echo "</table>";
}
}

Related

PHP SQL grouping all columns with same starttime

So i am having trouble coding the SQL queries correctly to get the desired output.
<body class="hi">
<div class="container dt[-head|-body]-center">
<div class="row ">
<div class="table">
<hr>
<table style="width:100%" class="table">
<thead>
<tr>
<th style="width:20%">ID</th>
<th style="width:15%">Date / Time</th>
<th style="width:15%">Location </th>
<th style="width:15%">Team A</th>
<th style="width:15%">Team B</th>
<th style="width:10%">Score A</th>
<th style="width:10%">Score B</th>
</tr>
</thead>
<tbody>
<?php
$sql =
"SELECT * FROM activitytable WHERE starttime IN
(
SELECT starttime FROM activitytable
GROUP BY starttime HAVING count(*) > 1
)";
$res_data = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($res_data)){
?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['starttime'] ?></td>
<td><?= $row['location'] ?></td>
<?php
if ($row['team'] = 1)
{
?>
<td>
<?php
echo $row["person"];
echo "<br>";
echo $row["person"];
echo "<br>";
echo $row["person"];
echo "<br>";
echo $row["person"];
echo "<br>";
}
?>
</td>
if ($row['team'] = 2)
{
?>
<td>
<?php
echo $row["person"];
echo "<br>";
echo $row["person"];
echo "<br>";
echo $row["person"];
echo "<br>";
echo $row["person"];
echo "<br>";
}
?>
</td>
<td><?= $row['team'] ?></td>
<td><?= $row['scoreA'] ?></td>
<td><?= $row['scoreB'] ?></td>
<?php } ?>
</tr>
</tbody>
</table>
</div>
</div>
</div>
It needs to be grouped by starttime. when I exec the code above it doesn't group the startime, it creates a row for every person and it is just printing every persons name 4 teams in each row.
Team A and Team B columns need to get the players from either team 1 (Team A) or 2 (Team B)
So in the database will be 8 records for every game played.
id - AUTO INCREMENT
player - players name
starttime - time of activity
scoreA - score for team 1
team - either 1 or 2
scoreB - score for team 2
location - location activity was held.
I want the output to look like this. Can't seem to figure it out, new to PHP SQL.
thanks.
u must use two separate query for achieving this :
$sql = "SELECT distinct starttime FROM activitytable" ;
$res_data = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($res_data)){
$sql = "SELECT player FROM activitytable WHERE starttime = '" . $row['starttime '] . "'";
$players = mysqli_query($conn,$sql);
// rest of your html code
}
and then :
<td><?= $row['id'] ?></td>
<td><?= $row['starttime'] ?></td>
<td><?= $row['location'] ?></td>
<?php
if ($row['team'] = 1)
{ ?>
<td>
<?php foreach($players as $player) {
echo $player["player"];
echo "<br>";
}
}
?>

sql query not working in my webpage

I wrote a MySQL query in my PHP code to join two tables, but I don't want all the columns in the result
My place details table:
My registration table:
My Code
<?php
$con= mysqli_connect("localhost", "root", "", "project");
if(!$con)
{
die('not connected');
}
$result= mysqli_query($con, "SELECT placedetails.name,placedetails.doj,placedetails.total,placedetails.stay,placedetails.food,placedetails.travel,registration.username
FROM placedetails inner join registration on placedetails.id=registration.id ;" );
?>
<div class="container">
<CENTER><h2>view Booking Details</h2>
<table class="table table-bordered">
<th>place</th>
<th>Travelling Date</th>
<th>Total Cost</th>
<th>Stay cost</th>
<th>Food cost</th>
<th>Travelling cost</th>
<th>User ID</th>
<?php
while($row =mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['doj']; ?></td>
<td><?php echo $row['total'] ;?></td>
<td><?php echo $row['stay'] ;?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['username'] ;?></td>
<td><img src='".$row['choose']."'/></td>";
</tr>
<?php
}
?>
</table>
</div>
i wrote my code,its executing but the problem is the data is not executing,can someone please help me out with my problem.
MysqlAdmin
In your table place details create a new entry call ID_regist.
After that change or give a number that correspond to your id in tabel Regist.
code php
For the connection create a connection.php.
<?php
$con= mysqli_connect("localhost", "root", "", "project");
if(!$con)
{
die('not connected');
}
?>
Makes your database more difficult to attack
Main code
<?php
include("connection.php");
?>
<div class="container">
<CENTER><h2>view Booking Details</h2>
<table class="table table-bordered">
<th>place</th>
<th>Travelling Date</th>
<th>Total Cost</th>
<th>Stay cost</th>
<th>Food cost</th>
<th>Travelling cost</th>
<th>User ID</th>
<?php
$sql=
"SELECT ".
"pld.name, ".
"pld.doj, ".
"pld.total, ".
"pld.stay, ".
"pld.food, ".
"pld.travel, ".
"pld.username ".
"FROM placedetails pld ".
"inner join registration reg ".
"on pld.ID_regist=reg.id;";
$result= mysqli_query($con, $sql);
if($result){
while($row =mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['doj']; ?></td>
<td><?php echo $row['total'] ;?></td>
<td><?php echo $row['stay'] ;?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['username'] ;?></td>
<td><img src='".$row['choose']."'/></td>"; // first there is no row, call chose
</tr>
<?php
}
}
else{
die("Error"); //Something of the type mysqli_error(connection);
}
?>
</table>
</div>
Attention to the row call chose, that you don't use in your sql query.
If that does not solve your problems, tell me what happens when I try to help you.
You have to alter the place table and add registration_id. Then join with this field.
SELECT placedetails.name,placedetails.doj,placedetails.total,placedetails.stay,placedetails.food,placedetails.travel,registration.username
FROM placedetails inner join registration on registration.id=place.registration_id

how to stop loop from table which select from query?

image
Hello guys. I wanna ask how to minimize the second table?
because i wanna it just printed once no loop but i want all the value to compare with the first table
here is my code
<?php
$query1 = mysql_query("SELECT * FROM training_detail AS s JOIN subject AS t JOIN training AS u JOIN employee AS v WHERE v.id_employee = $id1 AND s.nik LIKE v.nik AND u.id_subject LIKE t.id_subject AND s.id_training LIKE u.id_training ");
$i=1;
while($row1 = mysql_fetch_array($query1))
{
$date = $row1['date'];
$subject1 = $row1['subject_name'];
?>
<table class="table table-bordered">
<tr>
A
<td class="table-bordered">No</td>
<td class="table-bordered">date</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject no</td>
<td class="table-bordered">revision no</td>
<td class="table-bordered">Trainer</td>
<td class="table-bordered">Institution</td>
</tr>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo date("j/F/Y", strtotime($date)); ?></td>
<td class="table-bordered"><?php echo $subject1; ?></td>
<td class="table-bordered"><?php echo $row1['subject_no']; ?></td>
<td class="table-bordered"><?php echo $row1['revision_no']; ?></td>
<td class="table-bordered"><?php echo $row1['trainer']; ?></td>
<td class="table-bordered"><?php echo $row1['institution']; ?></td>
</tr>
</br>
<table class="table table-bordered">
<tr>
B
<td class="table-bordered">No</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject name</td>
<td class="table-bordered">subject no</td>
</tr>
<?php
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC ");
$x=1;
while($row2 = mysql_fetch_array($query2))
{
$subject2 = $row2['subject_name'];
if (strcasecmp($subject1, $subject2) != 0)
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo $row2['subject'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
$x++;
}
$i++;
}
?>
</table>
</table>
sorry about my pic's words is my own language
if you want to minimize the output
try to use limit
like so:
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC limit 1");

How to output the name of their foreign key id

I have a problem, I want that each id in the foreign key can output the name instead of their id. Here is the image.
Here's my code :
<table class="altrowstable" data-responsive="table" >
<thead >
<tr>
<th> IDno</th>
<th> Lastname </th>
<th> Firstname </th>
<th> Department </th>
<th> Program </th>
<th> Action</th>
</tr>
</thead>
<tbody>
<div style="text-align:center; line-height:50px;">
<?php
include('../connection/connect.php');
$YearNow=Date('Y');
$result = $db->prepare("SELECT * FROM student,school_year where user_type =3 AND student.syearid = school_year.syearid AND school_year.from_year like $YearNow ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['idno']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['firstname']; ?></td>
//name belong's to their id's
<td><?php echo $row['dept_id']; ?></td>
<td><?php echo $row['progid']; ?></td>
<td><a style="border:1px solid grey; background:grey; border-radius:10%; padding:7px 12px; color:white; text-decoration:none; " href="addcandidates.php?idno=<?php echo $row['idno']; ?>" > Running</a></div></td>
</tr>
<?php
}
?>
</tbody>
</table>
Thanks guys need a help
Just replace this line
<td><?php echo $row['progid']; ?></td>
by this one
<td><?php echo $row['prog_name']; ?></td>
To use the field you want
You also need to adapt your query to select program infos if not already in the table school_year or student (with a join) :
$result = $db->prepare("SELECT * FROM student
INNER JOIN school_year ON student.syearid = school_year.syearid
INNER JOIN program ON student.progid = program.progid
WHERE user_type =3
AND school_year.from_year like $YearNow ");
Assuming the program table is called "program"
Change
<td><?php echo $row['progid']; ?></td>
to
<td><?php echo $row['prog_name']; ?></td>
And I would recommend to study both php and sql
Change
<td><?php echo $row['progid']; ?></td>
by
<td><?php echo $row['prog_name']; ?></td>
inside the loop:
for($i=0; $row = $result->fetch(); $i++){
$program = $db->prepare("SELECT * FROM program where progid = :progid");
$program->execute(array('progid' => $row['progid']));
$p = $program->fetch();
now you can use it as follow: $p['prog_name']

How remove rows in loop or any other method to get diplay as below

Needed Display
Here my code and display
<table width="100%" cellspacing="0" cellpadding="0" summary="" id="box-table-a">
<thead>
<tr>
<th width="" scope="col"><strong>Item Description</strong></th>
<th scope="col" style="text-align:center;"><strong>Quantity</strong></th>
<th width="350" scope="col"><strong>Supplier Name</strong></th>
<th scope="col" style="text-align:center;"><strong>Unit Price Rs.</strong></th>
<th scope="col" style="text-align:center;"><strong>VAT Price Rs.</strong></th>
<th width="100" scope="col"><strong>Total Price Rs.</strong></th>
</tr>
</thead>
<tbody>
<?php
$get_data =mysql_query("SELECT supplier_add_quotaion_form.quotaion_request_id,supplier_add_quotaion_form.supplier_id,supplier_add_quotaion_form.supplier_add_quotaion_id,supplier_add_quotaion_request_item.* FROM supplier_add_quotaion_request_item,supplier_add_quotaion_form
WHERE supplier_add_quotaion_form.supplier_add_quotaion_id=supplier_add_quotaion_request_item.supplier_add_quotaion_id AND supplier_add_quotaion_form.quotaion_request_id='$id' ORDER BY quotation_item_id");
while($row = mysql_fetch_array($get_data)){
$quotaion_request_id = $row['quotaion_request_id'];
$supplier_add_quotaion_id = $row['supplier_add_quotaion_id'];
$supplier_id = $row['supplier_id'];
$net_item_value = $row['net_item_value'];
$vat_item_value = $row['vat_item_value'];
$total_value = $row['total_value'];
$quotation_item_id = $row['quotation_item_id'];
$get_count = mysql_query("SELECT quotation_item_id FROM supplier_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
$count = mysql_num_rows($get_count);
$get_quantity = mysql_query("SELECT quantity_required,item_description FROM clerk_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
while($rowB = mysql_fetch_array($get_quantity)){
$quantity_required = $rowB['quantity_required'];
$item_description = $rowB['item_description'];
}
?>
<tr>
<td><?php echo $item_description; ?></td>
<td align="center"><?php echo $quantity_required; ?></td>
<td><?php echo $supplier_id; ?></td>
<td align="center"><?php echo $net_item_value; ?></td>
<td align="center"><?php echo $vat_item_value; ?></td>
<td align="center"><?php echo $total_value; ?></td>
</tr>
<?php
}
?>
</tbody>
When Duplicate description coming need to rowspan them or what ever method need to show display as first image..i try it get count and then rowspan according to it.but unable to get need display,don't know how to delete extra cell
Duplicate row count not fixed,only example show first image,it can be range 1-7 duplicate for one description(for one item 7 suppliers able to bids)
supplier_add_quotaion_form table structure
supplier_add_quotaion_request_item table structure
clerk_add_quotaion_request_item structure
You can do this in one pass,try this
<?php
$get_data =mysql_query("...");
$last_quotaion_request_id = -1;
while($row = mysql_fetch_array($get_data)){
$quotaion_request_id = $row['quotaion_request_id'];
$supplier_add_quotaion_id = $row['supplier_add_quotaion_id'];
$supplier_id = $row['supplier_id'];
$net_item_value = $row['net_item_value'];
$vat_item_value = $row['vat_item_value'];
$total_value = $row['total_value'];
$quotation_item_id = $row['quotation_item_id'];
$get_count = mysql_query("SELECT quotation_item_id FROM supplier_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
$count = mysql_num_rows($get_count);
$get_quantity = mysql_query("SELECT quantity_required,item_description FROM clerk_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
while($rowB = mysql_fetch_array($get_quantity)){
$quantity_required = $rowB['quantity_required'];
$item_description = $rowB['item_description'];
}
?>
<tr>
<?php if($last_quotaion_request_id != $quotaion_request_id){ ?>
<td rowspan="<?php echo $count; ?>"><?php echo $item_description; ?></td>
<td rowspan="<?php echo $count; ?>" align="center"><?php echo $quantity_required; ?></td>
<?php } ?>
<td><?php echo $supplier_id; ?></td>
<td align="center"><?php echo $net_item_value; ?></td>
<td align="center"><?php echo $vat_item_value; ?></td>
<td align="center"><?php echo $total_value; ?></td>
</tr>
<?php
$last_quotaion_request_id = $quotaion_request_id;
}
?>
It'll probably be FAR easier to slurp your query results into a structured array, from which you can then easily retrieve the necessary counts to do your rowspans:
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[$row['item_description']][] = $row;
}
echo "start your table here";
foreach($data as $description => $items) {
echo "<tr><td rowspan=" . count($items) . ">";
foreach($items as $item) {
output item data here
}
}
This won't work as is, but should give you an idea of how to go about it.
you need to change the structure of table you are using. as per my guessing I'm giving a suggestion. keep the item description in a table and the quotation details in another table. and while fetching the data you can fetch using group by sql command.
your item table may be like this:
1. id
2. desc
3. clicks
and quotation table may be like this
1. id
2. item_id
3. supplier
4. quantity
5. vat
6. price
etc.

Categories