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>";
}
}
?>
Related
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>";
}
}
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']
I have a table for total report and I want to get a value from a database and format the numbers and have an output like this PHP 10,000.00 not like this 10000
Here is my trial PHP code. I have a table for print of Total reports and got problem in formatting the total amount.
function printpage()
{
window.print()
}
</script></head>
<body>
<br>
<br>
<div class="container">
<center>
<div class="alert alert-success" align="center"><h1>TOTAL REPORT</h1></div>
<br />
<table class="table table-striped table-bordered" border="1" width="900">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Start of Event</th>
<th>End of Event</th>
<th>Quantity</th>
<th>Amount</th>
<th>Status</th>
<th>Confirmation</th>
</tr>
</thead>
<tbody>
<?php
$con=mysql_connect("localhost","root","")or die(mysql_error());
$db=mysql_select_db('pm')or die(mysql_error());
$query=mysql_query("select * from reservation ")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['id'];
?>
<tr>
<?php $row['id'] ?>
<td><?php echo $row['firstname'] ?></td>
<td><?php echo $row['lastname'] ?></td>
<td><?php echo $row['address'] ?></td>
<td><?php echo $row['arrival']?></td>
<td><?php echo $row['departure'] ?></td>
<td><?php echo $row['result'] ?></td>
<td><?php
$number = 'payable';
$payable = 0;
setlocale(LC_MONETARY,"en_PHP");
echo $row("The price is %i", $payable);
?> </td>
<td><?php echo $row['status'] ?></td>
<td><?php echo $row['confirmation'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
$result = mysql_query("SELECT sum(payable) FROM reservation") or die(mysql_error());
while ($rows = mysql_fetch_array($result)) {
?>
<?php }
$result1 = mysql_query("SELECT result(qty) FROM rooinventory") or die(mysql_error());
while ($rows1 = mysql_fetch_array($result1)) {
?>
<div class="pull-right">
<div class="span">
<div class="alert alert-info"><i class="icon-credit-card icon-large"> </i> Total Amount: <?php echo $rows1['sum(p)']; ?></div>
</div>
</div>
</center>
<?php } ?>
Your locale is wrong and $number should actually be a $row value.
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $row['payable']);
For Windows machines
echo '$' . number_format(floatval($row['payable']));
Your code need to be fixed. Also en_US might not be enough depending on your system (ie: Debian needs en_US.utf-8):
setlocale(LC_MONETARY, "en_US.utf-8");
echo money_format("The price is %i", $row['payable']);
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.
I've got an inventory table that needs to have certain in each row, and then in the last column in each row, i want to loop out each item that was used per a specific inventory update i.e.
one row would have one column for the customername, one column for the date of the inventory transaction, one for the type of transaction, the specific technician and the last column for the certain products that were used in the update. what i've got so far loops out the first 4 columns fine, but the last column only gets generated for the first row. code:
<table style="width:92%;">
<tr style="font-size:14px;">
<th style="width:150px;">Customer</th>
<th style="width:110px;">Date</th>
<th style="width:140px;">Tech</th>
<th style="width:50px;text-align:center;">Type</th>
<th>Equipment</th>
</tr>
<?php
$pd_name = array();
$compArray = array();
$prevCompany = '';
$count = 0;
$select = mysql_query("SELECT pd_name, pd_col_name, company FROM item_inventory ORDER BY company ");
while($row = mysql_fetch_array($select)){
$pd_name[$row['pd_col_name']] = $row['pd_name'];
$compArray[$row['pd_col_name']] = $row['company'];
}
$select2 = mysql_query("SELECT * FROM tech_inventory WHERE date >= '$date%' ORDER BY date DESC ");
while($row2 = mysql_fetch_array($select2)){
$count = 0;
$prevCompany = '';
?>
<tr>
<td><?php echo htmlspecialchars($row2['customerName']); ?></td>
<td><?php echo $row2['date']; ?></td>
<td><?php echo $row2['tech']; ?></td>
<td style="text-align:center;"><?php echo $row2['type']; ?></td>
<td>
<table width="200">
<?php
while (list($key, $val) = each($pd_name)){
if($row2[$key] != 0){
if($prevCompany != $compArray[$key]){
?>
<tr>
<td style="text-align:center;"><?php echo $compArray[$key]; ?></td>
</tr>
<?php
$prevCompany = $compArray[$key];
}
?>
<tr>
<td><?php echo $val ?></td>
<td><?php echo $row2[$key]; ?></td>
</tr>
<?php
$count++;
}
}
?>
</table>
</td>
</tr>
<tr><td><?php echo $count; ?></td></tr>
<?php } ?>
</table>
This is what you have to do:
reset($pd_name);
while (list($key, $val) = each($pd_name))
The problem was that each steps through the array and once it reaches the end, it won't go any further. Therefore, you have to reset the array pointer to the beginning every time.