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)
Related
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.
This question already has answers here:
Single Value Mysqli [duplicate]
(8 answers)
Closed 3 years ago.
I can't seems to display the query results on my HTML. It displays nothing.
And I really don't know how to do that.
$qr1= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=1;");
$qr2= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=2;");
$qr3= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=3;");
$qr4= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=4;");
$qr5= mysqli_query($conn,"SELECT qr FROM `count` WHERE id=5;");
$total= $qr1 + $qr2 + $qr3 + $qr4 + $qr5;
My HTML :
<tbody>
<tr>
<th scope="row">QR1</th>
<td><?php echo $qr1 ?></td>
</tr>
<tr>
<th scope="row">QR2</th>
<td><?php echo $qr2 ?></td>
</tr>
<tr>
<th scope="row">QR3</th>
<td><?php echo $qr3 ?></td>
</tr>
<tr>
<th scope="row">QR4</th>
<td><?php echo $qr4 ?></td>
</tr>
<tr>
<th scope="row">QR5</th>
<td><?php echo $qr5 ?></td>
</tr>
</tbody>
what's missing is that you need to specify the column on your $qr.
Here's the shorter version of your code.
$total = 0;
$qr= mysqli_query($conn,"select qr, concat('QR', cast(id as varchar(3))) as id FROM `count` where id in (1,2,3,4,5) order by id;");
<tbody>
while ($row = mysqli_fetch_row($qr)) {
<tr>
<th scope="row">
<?php echo $row['id'] ?>
</th>
<td><?php echo $row['qr'] ?></td>
</tr>
$total = $total + $row['qr'];
}
</tbody>
I have two tables:
student
attendance
I want to select the attendance of students by conditions of year and exam_type (midterm, final) and show in one table.
$student_attendance1 = mysqli_query($con, "SELECT *
FROM student_attendance
INNER JOIN student
ON student.student_id = student_attendance.student_id
WHERE attendance_year=$attendance_year
AND exam_type=1");
$row_studend_attendance1 = mysqli_fetch_assoc($student_attendance1);
$studend_attendance2 = mysqli_query($con, "SELECT *
FROM student_attendance
INNER JOIN student
ON student.student_id = student_attendance.student_id
WHERE attendance_year=$attendance_year
AND exam_type=1");
$row_studend_attendance2 = mysqli_fetch_assoc($student_attendance2);
How can I do this? DATA
I will suppose you always have one, and only one, row per student for exam_type = 1 or exam_type = 2(?) (By the way, your two queries, as you wrote them, are absolutely identical...)
You should add a clause like "ORDER BY student.student_id ASC" to be sure you retrieve the datas from your two queries in the same order.
Then, all you have to do is display your datas into a table :
<?php
$student_attendance1 = mysqli_query($con, "SELECT *
FROM student_attendance
INNER JOIN student
ON student.student_id = student_attendance.student_id
WHERE attendance_year=$attendance_year
AND exam_type=1 ORDER BY student.student_id ASC");
$studend_attendance2 = mysqli_query($con, "SELECT *
FROM student_attendance
INNER JOIN student
ON student.student_id = student_attendance.student_id
WHERE attendance_year=$attendance_year
AND exam_type=2 ORDER BY student.student_id ASC");
while($row_studend_attendance1 = mysqli_fetch_assoc($student_attendance1)) {
$row_studend_attendance2 = mysqli_fetch_assoc($student_attendance2);
?> // Close your PHP tag.
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>F/Name</th>
<th>Exams</th>
<th>Year days</th>
<th>Present</th>
<th>Absent</th>
<th>Sickness</th>
<th>Permission</th>
</tr>
<tr>
<td rowspan="3"><?= $row_studend_attendance1['student_id'] ?></td>
<td rowspan="3"><?= $row_studend_attendance1['surname'] ?></td>
<td rowspan="3"><?= $row_studend_attendance1['firstname'] ?></td>
<td>Midterm</td>
<td><?= $row_studend_attendance1['year_days'] ?></td>
<td><?= $row_studend_attendance1['present'] ?></td>
<td><?= $row_studend_attendance1['absent'] ?></td>
<td><?= $row_studend_attendance1['sickness'] ?></td>
<td><?= $row_studend_attendance1['permission'] ?></td>
</tr>
<tr>
<td>Final</td>
<td><?= $row_studend_attendance2['year_days'] ?></td>
<td><?= $row_studend_attendance2['present'] ?></td>
<td><?= $row_studend_attendance2['absent'] ?></td>
<td><?= $row_studend_attendance2['sickness'] ?></td>
<td><?= $row_studend_attendance2['permission'] ?></td>
</tr>
<tr>
<td>Sum</td>
<td><?= $row_studend_attendance1['year_days'] + $row_studend_attendance2['year_days'] ?></td>
<td><?= $row_studend_attendance1['present'] + $row_studend_attendance2['present'] ?></td>
<td><?= $row_studend_attendance1['absent'] + $row_studend_attendance2['absent'] ?></td>
<td><?= $row_studend_attendance1['sickness'] + $row_studend_attendance2['sickness'] ?></td>
<td><?= $row_studend_attendance1['permission'] + $row_studend_attendance2['permission'] ?></td>
</tr>
</table>
<?php } ?>
I hope I answer your question. If not, feel free to ask again.
I am working on stock project in PHP.Currently while searching according date it is showing data according searching date which is good.But those date which is not avail in database ,it is showing blank,while i need prevous date data.(Like if i enter 25.12.2015,in the result it should show previous date data like 23.12.2015 in this case.)
my html code is:
<form action="dateview.php" method="post">
<table width="60%" border="2" bordercolor="green">
<tr>
<td>DATE</td>
<td>
<input type="date" name="date">
</td>
<td colspan="2">
<center><input type="submit" value="search"/></center>
</td>
</table>
</form>
my php code is:
<?php
if($qw="select * from details where date='$date'"){
$qq = mysqli_query($con,$qw);
while($r=mysqli_fetch_array($qq,MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $r['itemname']; ?></td>
<td><?php echo $r['deposit']; ?></td><td><?php echo $r['withdraw']; ?></td>
<td><?php echo $r['total']; ?></td>
<td><?php echo $r['approvedby']; ?></td>
<td><?php echo $r['receivedby']; ?></td>
<td><?php echo $r['givenby']; ?></td>
<td><?php echo $r['receivedto']; ?></td>
</tr>
<?php } ?>
Just tweak your SELECT query:
SELECT * FROM details WHERE date <= 'your-date-here' ORDER BY date DESC LIMIT 1
You might use something like this
date('Y-m-d', strtotime("23.12.2015")); // o/p: 2015-12-23
Where 'Y-m-d' format you stored in DB.
Refer date format here
I have two tables, from that i need to fetch each row value, and print it like below.
<table id="contact" class="display">
<thead>
<tr>
<td>Name</td>
<td>Business Email</td>
<td>Mobile No</td>
<td>Company Name</td>
<td>Message</td>
</tr>
</thead>
<tr>
<td><?php echo $result['0']; ?></td>
<td><?php echo $result['1']; ?></td>
<td><?php echo $result['2']; ?></td>
<td><?php echo $result['3']; ?></td>
<td><?php echo $result['4']; ?></td>
</tr><?php
}
?>
</table>
I have tried
SELECT GROUP_CONCAT(field_value)
FROM `table2` GROUP BY `submit_time`
But it will make problem in explode step.
If comma separator came in message field, explode function increase array value, i cant fetch the correct value and print it.
Kindly share the idea to fix this.
You should have a primary key or atleast an auto increment key on the first table in order to associate it with the second table. The plugin you are using might be using it by ordering it. So every table 2 entry has a table 1 entry ordered by timestamp. What you have to do is create a new table1 column called ID and make it auto increment, it wont effect the working of the plugin. Then you can use a custom query like this.
SELECT $wpdb->table1.*
FROM $wpdb->table1
INNER JOIN $wpdb->table2 ON ($wpdb->table1.ID = $wpdb->table2.ID)
WHERE $wpdb->table2.field_name = 'text-506'