SQL order by date inside WHILE LOOP - php

I'm having trouble sorting my events by date, as I have event id's in one table and event date in another.
The code is as follows.
$getEventIds = "SELECT * FROM rz6wq_ohanah_registrations WHERE email='".$userEmail."'";
$result = mysqli_query($conn, $getEventIds);
echo '<br />';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$getEventTitle = "SELECT * FROM rz6wq_ohanah_events WHERE ohanah_event_id=" . $row['ohanah_event_id'] . " ORDER BY date ASC";
$title = mysqli_query($conn, $getEventTitle);
$rowTitle = $title->fetch_assoc();
$originalDate = $rowTitle["date"];
$newDate = date("d-m-Y", strtotime($originalDate));
date_default_timezone_set('Europe/Copenhagen');
$currentDate = date('d/m/Y');
if(strtotime($currentDate) < strtotime($newDate)){
//echo "id: " . $row["ohanah_event_id"] . "<br>";
echo "<div class='eventsTilmeldt'>";
echo "<b>" . utf8_encode($rowTitle["title"]) . "</b><br>";
echo "<br>";
echo "Status: "; if($row["paid"] == 0){echo "<span style='color:red;'>ej betalt</span>";}else{echo "<span style='color:green;'>betalt</span>";}
echo "<br>";
echo "Dato: " . $newDate . "<br>";
echo "Tidspunkt: " . $rowTitle["start_time"] . "<br>";
echo "<br>";
echo "Adresse: " . utf8_encode($rowTitle["adress"]) . "<br>";
echo "By: " . utf8_encode($rowTitle["geolocated_city"]) . "<br>";
echo "Sted: " . utf8_encode($rowTitle["venue"]) . "<br>";
echo "</div>";
echo "<br>";
}else{
echo "";
}
}
} else {
echo "Ingen tilmeldte events.";
}
The issue that it doesen't sort my event's correctly by date. I figure it has something to do with it being inside the while loop where I order by date?
"SELECT * FROM rz6wq_ohanah_events WHERE ohanah_event_id=" . $row['ohanah_event_id'] . " ORDER BY date ASC";
I need to get the event's from another table first, called registrations - inside this table there is no date field.
How can I sort this list by date ?

You can use JOIN instead:
$getEventIds = "SELECT reg.*,event.* FROM rz6wq_ohanah_registrations as reg
JOIN rz6wq_ohanah_events as event ON reg.ohanah_event_id=event.ohanah_event_id
WHERE email='".$userEmail."'
ORDER BY event.date ASC";

Why use two query You can do it in single query
SELECT A.*,B.* FROM rz6wq_ohanah_registrations AS A JOIN
rz6wq_ohanah_events AS B ON A.ohanah_event_id=B.ohanah_event_id WHERE
A.email='".$userEmail."' ORDER BY B.date ASC

$getEventIds = "SELECT * FROM rz6wq_ohanah_registrations AS ROR, z6wq_ohanah_events AS ZOE WHERE ROR.ohanah_event_id = ZOE.ohanah_event_id AND ZOE.email = '" . $userEmail . "' ORDER BY date ROR.ASC";

Related

How do i make each movie appear on under their respective date

i cant seem to make each movie appear under their respective date and have a correct time dropdown menu for the movie time selection
$sql_date = "SELECT DISTINCT sDate, sTitle FROM movieScreenings ";
$result_date = mysqli_query($db, $sql_date);
while($row = mysqli_fetch_array($result_date)) {
echo "<h2>" . $row['sDate'] . "</h2>";
$sql_movie = "SELECT * FROM movieList, movieScreenings WHERE title = '" . $row['sTitle'] . "'";
$result_movie = mysqli_query($db, $sql_movie);
while($row2 = mysqli_fetch_array($result_movie)) {
echo "<div class='box'>
<img class='poster' src='posters/" . $row2['poster'] . "'/>
<h2>" . $row2['title'] . "</h2>
<p>" . $row2['description'] . "</p>";
$sql_time = "SELECT DISTINCT sTime FROM movieScreenings WHERE sTitle = '" . $row2['title'] . "' AND sDate = '" . $row['sDate'] . "'";
$result_time = mysqli_query($db, $sql_time);
while($row3 = mysqli_fetch_array($result_time)) {
echo "<select name='sTime'>
<option value='" . $row3['sTime'] . "'>" . $row3['sTime'] . "</option>
</select>";
echo "</div>";
}
}
}
mysqli_free_result($result_date);
mysqli_free_result($result_movie);
mysqli_free_result($result_time);
// Close connection
mysqli_close($db);
You probably want to sort your first query by sDate with ORDER BY sDate.
In your second query you are again selecting from movieScreenings which is not necessary because I imagine all the data you are accessing (poster, title, description) is stored in movieList.
Thirdly, in your last while loop you are repeating the <select> tag for every screening time. This will result in multiple dropdown inputs. Also, you are including the closing </div> tag in this loop. So only loop the <option> tag.
With these changes your code would look like this:
$sql_date = "SELECT DISTINCT sDate, sTitle FROM movieScreenings ORDER BY sDate ASC";
$result_date = mysqli_query($db, $sql_date);
while($row = mysqli_fetch_array($result_date)) {
echo "<h2>" . $row['sDate'] . "</h2>";
$sql_movie = "SELECT * FROM movieList WHERE title = '" . $row['sTitle'] . "'";
$result_movie = mysqli_query($db, $sql_movie);
while($row2 = mysqli_fetch_array($result_movie)) {
echo "<div class='box'>
<img class='poster' src='posters/" . $row2['poster'] . "'/>
<h2>" . $row2['title'] . "</h2>
<p>" . $row2['description'] . "</p>";
$sql_time = "SELECT DISTINCT sTime FROM movieScreenings WHERE sTitle = '" . $row2['title'] . "' AND sDate = '" . $row['sDate'] . "'";
$result_time = mysqli_query($db, $sql_time);
echo "<select name='sTime'>";
while($row3 = mysqli_fetch_array($result_time)) {
echo "<option value='" . $row3['sTime'] . "'>" . $row3['sTime'] . "</option>";
}
echo "</select>";
echo "</div>";
}
}
...
And lastly, I would suggest using ID's as your foreign keys to prevent duplicates in the future. There might be multiple movies with the same title.

How to show records between Dates in search?

It only shows 'No record found' but I'm trying to show the records between the dates using search
<?php
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary ";
// Date filter
if (isset($_POST['search'])) {
$fromDate = $_POST['fromDate'];
$endDate = $_POST['endDate'];
if (!empty($fromDate) && !empty($endDate)) {
$query = "SELECT title, count(title) as totalnotary ,
notary_date, book_no
FROM tbl_notary
Where 1
and notary_date between '" . $fromDate . "' and '" . $endDate . "'";
}
}
// Sort
$query .= " group by book_no,title,Year(notary_date),
month(notary_date),day(notary_date)
ORDER BY notary_date DESC";
$Records = mysqli_query($conn, $query);
// Check records found or not
if (mysqli_num_rows($Records) > 0) {
while ($Record = mysqli_fetch_assoc($Records)) {
$book_no = $Record['book_no'];
$title = $Record['title'];
$totalnotary = $Record['totalnotary'];
$notary_date = date("F j,Y", strtotime($Record['notary_date']));
echo "<tr>";
echo "<td>" . $book_no . "</td>";
echo "<td>" . $title . "</td>";
echo "<td>" . $totalnotary . "</td>";
echo "<td>" . $notary_date . "</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='4'>No record found.</td>";
echo "</tr>";
}
?>
OK, so you are saying that here is your problem.
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary
Where 1 //!!!!!What is this 1???? You are missing a column name probably
and notary_date between '" . $fromDate . "' and '" . $endDate . "'";
Try this query. You have WHERE 1 which can't do anything and probably is failing your query. Try with some error reporting and you will probably get an error there.
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary
WHERE notary_date BETWEEN '" . $fromDate . "' AND '" . $endDate . "'";
Then put this below to see if query is failing or not.
$Records = mysqli_query($conn, $query) or die (mysqli_error($conn));

changing time format from mysqli default to h:i in a php fetch

I am trying to display a list of times on a calendar that are reserved.
I want the times to display in h:i format or "00:00" but they are displaying as the format in the Mysqli database or "00:00:00". Also I need it in 12 hour not 24.
Here is my last attempt with only the start time changed:
<?php
$air = $airc;
include('connect.php');
if ($result = $con->query("SELECT * FROM cal WHERE cal_air = '$air' AND cal_date = '$calday1' ORDER BY DATE_FORMAT(cal_time, '%h:%i') ASC"))
{
if ($result->num_rows > 5)
{
echo "<div class='new'><a href='day.php?pass_id=",urlencode($calday1),"'>Multiple<BR>Reservations</a><BR>(Click for details)</div>";
}
elseif ($result->num_rows > 0)
{
while ($row = $result->fetch_object())
{
echo "<a href='entry-check.php?cal_id=" . $row->cal_id . "&cal_user=" .$row->cal_user . "'>" . date('h:i', strtotime($row->cal_time)) . "</a>&nbsp to &nbsp";
echo "<a href='entry-check.php?cal_id=" . $row->cal_id . "&cal_user=" .$row->cal_user . "'>" . $row->cal_end . "</a>&nbsp";
echo "<BR>";
}
echo '<div class="new2">Add</div>';
}
else
{
echo '<div class="new">No Reservations</div>';
}
}
else
{
echo "Error: " . $mysqli->error;
}
?>
One option is to format the time in sql. Try bellow code! Hope it helps.
<?php
$air = $airc;
include('connect.php');
if ($result = $con->query("SELECT cal_id,cal_user, DATE_FORMAT(cal_time, '%h:%i') as start_time, DATE_FORMAT(cal_end, '%h:%i') as end_time, cal_date, cal_air FROM cal WHERE cal_air = '$air' AND cal_date = '$calday1' ORDER BY DATE_FORMAT(cal_time, '%h:%i') ASC"))
{
if ($result->num_rows > 5)
{
echo "<div class='new'><a href='day.php?pass_id=",urlencode($calday1),"'>Multiple<BR>Reservations</a><BR>(Click for details)</div>";
}
elseif ($result->num_rows > 0)
{
while ($row = $result->fetch_object())
{
echo "<a href='entry-check.php?cal_id=" . $row->cal_id . "&cal_user=" .$row->cal_user . "'>" . $row->start_time . "</a>&nbsp to &nbsp";
echo "<a href='entry-check.php?cal_id=" . $row->cal_id . "&cal_user=" .$row->cal_user . "'>" . $row->end_time . "</a>&nbsp";
echo "<BR>";
}
echo '<div class="new2">Add</div>';
}
else
{
echo '<div class="new">No Reservations</div>';
}
}
else
{
echo "Error: " . $mysqli->error;
}
?>

Output single result of a MySQL Query with PHP not working

My table 'viewlevels' has the following data (among other):
id |title
10 |Cenas
I'm running the SQL query:
SELECT title FROM viewlevels WHERE id=10
Which is returning "Cenas" as expected.
But using the following PHP script, I just get "texto= " , why?
$res = $db->query("SELECT title FROM viewlevels WHERE id=10");
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
echo " texto= " . $row['title'] . "\n";
};
To see both fields you have to echo those columns:
while ($row = $res->fetch_assoc()) {
echo " id= " . $row['id'] . "\n";
echo " texto= " . $row['title'] . "\n";
};
You don't need to use data_seek in this instance.
$res = $db->query("SELECT title FROM viewlevels WHERE id=10");
while ($row = $res->fetch_assoc()) {
echo " texto= " . $row['title'] . "\n";
}
Will work.

Calculating total of rows

I fetching and displaying records from database like this
SELECT purchorders.orderno,
suppliers.suppname,
purchorders.orddate,
purchorders.deliverydate,
purchorders.initiator,
purchorders.requisitionno,
purchorders.allowprint,
purchorders.status,
suppliers.currcode,
currencies.decimalplaces AS currdecimalplaces,
SUM(purchorderdetails.unitprice*purchorderdetails.quantityord) AS ordervalue
FROM purchorders
INNER JOIN purchorderdetails
ON purchorders.orderno = purchorderdetails.orderno
INNER JOIN suppliers
ON purchorders.supplierno = suppliers.supplierid
INNER JOIN currencies
ON suppliers.currcode=currencies.currabrev
WHERE purchorders.orderno=purchorderdetails.orderno
GROUP BY purchorders.orderno,
suppliers.suppname,
purchorders.orddate,
purchorders.initiator,
purchorders.requisitionno,
purchorders.allowprint,
purchorders.status,
suppliers.currcode,
currencies.decimalplaces LIMIT 5
I am getting the result properly. But i want to calculate and display the result of ordervalues which i have displayed (total of all 5 as i limit it to 5)
I tried doing like this
$SalesOrdersResult2 = DB_query($SQL,$db);
while ($row = DB_fetch_array($SalesOrdersResult2))
{
$FormatedOrderValue2 = locale_number_format($row['ordervalue'],$row['currdecimalplaces']);
$Total = $array_sum($row['ordervalue']);
$FormatedOrderDate1 = ConvertSQLDate($row['orddate']);
$FormatedDelDate1 = ConvertSQLDate($row['deliverydate']);
echo " <tr><td> " . $row['suppname'] . " </td>";
echo " <td>$FormatedOrderDate1</td><td>$FormatedDelDate1</td><td> " . $row['initiator'] . " </td><td>$FormatedOrderValue2</td><td> " . $row['status'] . " </td></tr> ";
}
echo "<tr><td colspan='3'>Total---</td><td colspan='2'>$Total</td></tr></tbody>";
But it says "Fatal error: Function name must be a string in ..."
Somebody please help me in doing this
Thanks
I suspect you're trying to do something like this?:
$SalesOrdersResult2 = DB_query($SQL, $db);
$Total = 0;
while ($row = DB_fetch_array($SalesOrdersResult2))
{
$FormatedOrderValue2 = locale_number_format($row['ordervalue'],$row['currdecimalplaces']);
$Total += $row['ordervalue'];
$FormatedOrderDate1 = ConvertSQLDate($row['orddate']);
$FormatedDelDate1 = ConvertSQLDate($row['deliverydate']);
echo " <tr><td> " . $row['suppname'] . " </td>";
echo " <td>$FormatedOrderDate1</td><td>$FormatedDelDate1</td><td> " . $row['initiator'] . " </td><td>$FormatedOrderValue2</td><td> " . $row['status'] . " </td></tr> ";
}
echo "<tr><td colspan='3'>Total---</td><td colspan='2'>$Total</td></tr></tbody>";
Remove the $ before array_sum.

Categories