Hi, I have created in the db two rows for one flight, one for the arrival and another for departure. Now Ineed to order by arrival time and departure time in the order.
This is my code but it's not working.
In the date that I've selected I have to display the arrivals and departures in the rights order eg:
Flight 1 : arr 10:30
Flight 2 : dep 10:55
Flight 1 : Dep 11:00
Flight 5 : Dep 11:20
And so on....
$sqlArr = "SELECT * FROM flights WHERE arr_date = '$date' OR dep_date = '$date' ORDER BY
eta,etd ASC";
$query = mysqli_query($cnx, $sqlArr);
foreach ($query as $key => $v ){
if ($v['xid_arrival'] == 0) {?>
<tr>
<td>ARR</td>
<td><?=$v['arr_date'];?> </td>
<td><?=$v['atyp'];?> </td>
<td><?= $v['reg'];?> </td>
<td><?=$v['arr_cls'];?> </td>
<td><?=$v['adep'];?> </td>
<td><?= date('H:i', strtotime($v['eta']));?> </td>
<td onclick="openRow(this, <?= $v['id_flt'];?>)">+</td>
</tr>
<tr hidden id="<?= $v['id_flt'];?>">
<td colspan="6">hidden id <?= $v['id_flt'];?></td>
</tr>
<?php } else { ?>
<tr>
<td>DEP</td>
<td><?=$v['dep_date'];?> </td>
<td><?=$v['atyp'];?> </td>
<td><?= $v['reg'];?> </td>
<td><?=$v['dep_cls'];?> </td>
<td><?=$v['ades'];?> </td>
<td><?= date('H:i', strtotime($v['etd']));?> </td>
<td onclick="openRow(this, <?= $v['xid_arrival'];?>)">+</td>
</tr>
<tr hidden id="<?= $v['xid_arrival'];?>">
<td colspan="6">hidden xid <?= $v['xid_arrival'];?></td>
</tr>
<?php }?>
<?php } ?>
You need to create one column with departure or arrival date depending which is defined in a line and order by that column.
Something like this
SELECT
*,
CASE
WHEN eta > 0 THEN eta
WHEN etd > 0 THEN etd
END as sort_order
FROM flights
WHERE
arr_date = '$date' OR dep_date = '$date'
ORDER BY
sort_order ASC;
And remember to escape data you get as input to your SQL query to prevent SQL injection vulnerabilities.
And SELECT * is lazy coding. Select only columns you neeed.
Related
I have a query which select the last entered data into the database month wise and i have assigned it into an array and tried to display it inside a table which works fine but the data is combined togeather as shown below
Example - the first numeric data you see on the table 1245871 should be displayed as follows
Month - 12
Closing Mileage - 45871
But as you can see on the image both the data are combined togeather how can i seperate these two values and display them accordingingly. The code for this is as below,
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$querymain = "SELECT * FROM users";
$resultsmain = mysqli_query($connect,$querymain);
if ($resultsmain->num_rows>0) {
while ($userid = mysqli_fetch_assoc($resultsmain)) {
?>
<table class="table" class="mt-3 mb-3">
<thead class="bg-dark text-light">
<tr>
<th colspan="3"><?php echo $userid['id']; ?></th>
<th colspan="3"><?php echo $userid['name']; ?></th>
<th colspan="3"><?php echo $userid['approved_kmpl'];?> KM</th>
</tr>
</thead>
<thead>
<tr>
<th>Month</th>
<th>Closing Mileage</th>
<th>Usage For Month</th>
<th>Required Per Month</th>
<th>Excess Used</th>
<th>(%)</th>
<th>KM/L</th>
<th>Consumed Liters</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<?php
$closingmileage = "SELECT extract(MONTH from date) as Month,
MAX(mileage) FROM mileagesnew
WHERE user_id='".$userid['id']."'
AND extract(YEAR FROM date) ='2020'
group by Month
ORDER BY month desc";
$closingres = mysqli_query($connect,$closingmileage);
if ($closingres->num_rows>0) {
while ($closingrow = mysqli_fetch_assoc($closingres)) {
$data =array($closingrow);
foreach($data as $value){
print_r($value);
?>
<tr>
<td> <?php echo implode($value); ?> </td>
</tr>
<tr>
<td> <?php echo implode($value); ?> </td>
</tr>
<?php
}
}
}
else{
echo "No Data Found";
}
?>
</tbody>
</table>
<?php
}
}
You have 2 columns returned in the resultset of the query Month and mileagesnew. So $closingrow will be an array with 2 members, named as the columns names from your table, or the alias you give the column name in the query. So all you need to do is output $closingrow['Month'] and $closingrow['mileagesnew'] seperately in the 2 table cells
Also I assume you want the 2 values on the same row of the tabel to output both cells within the same <tr>...</tr> along with the empty cells you have not filled yet
<?php
$closingmileage = "SELECT extract(MONTH from date) as Month,
MAX(mileage) FROM mileagesnew
WHERE user_id='".$userid['id']."'
AND extract(YEAR FROM date) ='2020'
group by Month
ORDER BY month desc";
$closingres = mysqli_query($connect,$closingmileage);
if ($closingres->num_rows>0) {
while ($closingrow = mysqli_fetch_assoc($closingres)) {
echo "<tr>
<td>$closingrow[Month]}</td>
<td>$closingrow[mileagesnew ]</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>";
}
} else{
echo "No Data Found";
}
?>
I m struggling to send Each ClientID Information from Database to their specific Email Address.
My training exercise was to use this SQL (See Below) that have 3 different tables that are as follow: Client, Sites, Job_Cards.
Now I have create a SQL that gives me the output that I need. But my problem is based at the fact that 1 clientID may have one or Many SiteID. one SiteID may have one and many Job_Card#.
When I create my PHP Script the Data Displays as follow: Link on a HTML page.
My Question is in 2 Parts:
How can I format the table to Display ONLY 1 ClientID or ClientName with the list of Job#, site name and Description of this single Client?
How may I send an Email to Each of the ClientName Without Duplicate ID. And What php script will be good to use to send Emails to Each Clients?
Here is my PHP Script and My SQL Statment:
$sqlSelect = "SELECT DISTINCT(cl.client_id),
client_name,
DATE(jb.date),
jb.job_number,
jb.repair,
st.site_name
FROM
job_cards jb
INNER JOIN
clients cl ON (cl.client_id = jb.client_id)
LEFT JOIN
sites st on (st.site_id = jb.site_id)
WHERE
jb.completed = 1
AND cl.client_id = jb.client_id
AND jb.date >= DATE_ADD(DATE(NOW()), INTERVAL - 30 DAY)
ORDER BY cl.client_name ASC";
//echo $sqlSelect;
$tresult = mysql_query($sqlSelect);
//die("ss");
//$dataCount = mysql_num_rows($result);
while($userData = mysql_fetch_assoc($tresult)){
if($i%2==0)
$classname = 'evenRow';
else if($i%2==1)
//extract($userData);
?>
</table>
</td>
</tr>
<tr>
<td align='center' height="30" style="font-size:14px;">
<b><?php echo $userData['client_name'];?></b>
</td>
<td align='center'></td>
<td>
<table width='100%' cellpadding= '1' border='0'>
<thead>
<tr>
<td style="font-size:13px; text-align:Left;"><b>Date</b></td>
<td style="font-size:13px; text-align:Left;"><b>Job #</b></td>
<td style="font-size:13px; text-align:left;"><b>Site name</b></td>
<td style="font-size:13px; text-align:left;"><b>Description</b></td>
</tr>
</thead>
<tr class='<?php if(isset($classname)) echo $classname;?>'>
<tr>
<td>
<?php echo mysql_real_escape_string ($userData['DATE(jb.date)']); ?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['job_number']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['site_name']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['repair']);?>
</td>
</tr>
<?php $i++;
}
?>
</tr>
</table>
<table width='100%' cellpadding= '1' border='1'>
<tr>
</tr>
</table>
Please May Some 1 Help me. I tried What I could But Still I cannot send emails and the Table format doesn't display the way I want.
Thank.
This data is showing in MySql Database
<table>
<tr>
<td>
Team
</td>
<td>
Score
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
10
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Australia
</td>
<td>
5
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
5
</td>
</tr>
</table>
and I want to show data on webpage like this
<table>
<tr>
<td>
Team
</td>
<td>
Score
</td>
</tr>
<tr>
<td>
New Zealand
</td>
<td>
15
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Australia
</td>
<td>
5
</td>
</tr>
</table>
Note that in database "New Zealand" showing 2 time in Database but on webpage it showing 1 time and also its score in sum
please tell me how can I do this using mysqli
Thanks in Advance.
Without your query I can't use your table or field names, but it would look something like this:
SELECT `team`, SUM(`score`) AS `score_sum` FROM `teams` GROUP BY `team`
This groups the rows by team and for each group, the scores are summed for that team. You would then access the team name using team and the total score for each team using score_sum in your PHP code. You'll have to provide more details like your query and code if this doesn't help.
I'll give a suggestion for your table , considering that you have already created soccer table with three columns ID,Team,Score . regardless a way you insert a data in this table , here how to get it properly
<?php
include("connection.php");
$team = "select distinct team from soccer";
$team2 = mysqli_query($mysqli,$team);
$up = "0";
$id ="1";
echo "<table><tr><td>ID</td><td>TEAM</td><td>SCORE</td></tr><tr>";
while ($teamres = $team2->fetch_array()){
echo "<td>".$id."</td><td>".$teamres['team']."</td>";
$score = mysqli_query($mysqli,"SELECT sum(score) FROM soccer where team ='".$teamres['team']."'");
$score2 = mysqli_fetch_row($score);
$scorere = $score2[0];
echo "<td>".$scorere."</td>";
echo "</tr>";
$id++;
$up++;
}
echo "</table>";
?>
In my database I store orders as entryDate based on Unix timestamp. I would like to make a simple HTML chart that displays the number of orders of a given day. But this is thougher that it sounds (for me atleast).
How do I loop through my database based on an unix timestamp? How do I then get the total number of orders of that day? The furtest I got is:
SELECT COUNT(id)
FROM customers_orders
WHERE entryDate >= NOW() - INTERVAL 7 DAY
AND status != 99 AND totalprice > 0 AND importId = 0
For orders of TODAY I use:
SELECT COUNT(id) FROM customers_orders WHERE DATE_FORMAT(FROM_UNIXTIME(`entryDate`), '%Y-%m-%d') = CURDATE() AND status != 99
The end result should be something simple like:
<table>
<tr>
<td><?= $objStats->numMonday; ?></td>
<td>Monday</td>
</tr>
<tr>
<td><?= $objStats->numTuesday; ?></td>
<td>Tuesday</td>
</tr>
<tr>
<td><?= $objStats->numWednesday; ?></td>
<td>Wednesday</td>
</tr>
<tr>
<td><?= $objStats->numThursday; ?></td>
<td>Thursday</td>
</tr>
<tr>
<td><?= $objStats->numFriday; ?></td>
<td>Friday</td>
</tr>
<tr>
<td><?= $objStats->numSaturday; ?></td>
<td>Saturday</td>
</tr>
<tr>
<td><?= $objStats->numSunday; ?></td>
<td>Sunday</td>
</tr>
</table>
DATE(entryDate) gives you the YYYY MM DD part of your timestamp, group your query with that.
SELECT COUNT(id), DATE(entryDate)
FROM customers_orders
WHERE entryDate >= NOW() - INTERVAL 7 DAY
AND status != 99 AND totalprice > 0 AND importId = 0
GROUP BY DATE(entryDate)
I basically have this program which fetches orders from a database. My problem is I need to make a report in such a way that the script will get rows with the same column value then count them and display them.
Say Table 'orders'
salesorder family product
1111111 pi_gx af000
1111111 pi_gx af000
1111112 sfng af111
1111113 pi_gx af000
will display in my php page
sales order family qty product
1111111 pi_gx 2 af000
1111112 sfng 1 af111
1111113 pi_gx 1 af000
It counts the quantity of row of the said sales order and displays the quantity, at the same time displays only a single copy of that sales order in my page.
Here's the code:
<body class="printable"><h1 align="center">New Orders Dropped for Product Integration 1X</h1>
<table align="center" width="100%">
<tr>
<td class="labels">Prepared: </td>
<td class="boxed"><?php date_default_timezone_set("Asia/Singapore");$today = date("d/m/y H:i");echo $today; ?></td>
<td class="divider"> </td>
<td class="labels">Time Coverage: </td>
<td class="boxed">12:00 to 2:00</td>
<td class="divider"> </td>
<td class="labels">BirthStamp: </td>
<td class="boxed">5/21/2012</td>
<td class="divider"> </td>
<td class="labels">Saved: </td>
<td class="boxed"><?php echo $today; ?></td>
</tr>
<tr>
<td class="labels">Prepared by (Production): </td>
<td><input type="text" name="preparer" id="preparer" class="boxedPrepared" /></td>
<td class="divider"></td>
<td class="labels">Recorded by (Store): </td>
<td><input type="text" name="recorder" id="recorded" class="boxedPrepared" /></td>
<td class="divider"></td>
<td class="labels">Recorded: </td>
<td class="boxed" colspan="3"><?php echo $today; ?></td>
</tr>
</table>
<br />
<?php
$conn = mysql_connect("localhost", "root", "123456") or die(mysql_error());
mysql_select_db("store") or die(mysql_error());
$sql = mysql_query("SELECT * FROM report ORDER BY salesorder AND masterproduct ASC") or die(mysql_error());
if(mysql_num_rows($sql) == 0) {
echo "<center><b>No ORDER/S in Queue</b></center>";
} else {
echo "
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" class=\"data\">
<tr>
<td class=\"dataHeader\">Sales Order</td>
<td class=\"dataHeader\">Sales Order Code</td>
<td class=\"dataHeader\">Family</td>
<td class=\"dataHeader\">Product Code</td>
<td class=\"dataHeader\">Quantity</td>
<td class=\"dataHeader\">Birth Stamp</td>
<td class=\"dataHeader\">Due Date</td>
</tr>
";
while($result = mysql_fetch_array($sql)) {
echo "
<tr>
<td class=\"data\">".$result['salesorder']."</td>
<td class=\"data\"><span class=\"title\">*".$result['salesorder']."*</span><br />".$result['salesorder']."</td>
<td class=\"data\">".$result['family']."</td>
<td class=\"data\"><span class=\"title\">*".$result['masterproduct']."*</span><br />".$result['masterproduct']."</td>
<td class=\"data\">";
//need to echo the value here
echo "</td>
<td class=\"data\">".$result['birthstamp']."</td>
<td class=\"data\"><span class=\"title\">*".$result['duedate']."*</span><br />".$result['duedate']."</td>
</tr>
";
}
echo "</table>";
}
?>
SELECT salesorder, family, product, COUNT() AS qty FROM orders
GROUP BY salesorder;
EDIT: OK, try this on for size:
SELECT r.*, t.qty FROM report r LEFT JOIN
(SELECT salesorder, COUNT() AS qty FROM orders
GROUP BY salesorder) t
ON t.salesorder=r.salesorder
ORDER BY r.salesorder AND r.masterproduct ASC
Try this query:
SELECT
COUNT() AS qty, sales_order, family, products
FROM orders
GROUP BY sales_order
Finally figured it out. Thank you guys for helping. I changed my sql query to this:
$sql = mysql_query("SELECT salesorder, masterproduct, family, birthstamp, duedate, COUNT( * ) AS total FROM report WHERE family = '$family' AND birthstamp BETWEEN '$startDT' AND '$endDT' GROUP BY salesorder, masterproduct, family, duedate ");
and it worked fine!