I am getting time from my database, when I echo it out it works, but it doesn't work in the while loop.
However, the other variables work...
Code:
$resultevent = mysql_query('SELECT venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');
$rowevent = mysql_fetch_assoc($resultevent);
$date = new DateTime($originaldate);
$newdate = $date->format('m/d/Y');
$startTime2 = $rowevent['startTime'];
$endTime2 = $rowevent['endTime'];
$venue = $rowevent['venue'];
echo $startTime2; <============== WORKS HERE
while ($row = mysql_fetch_assoc($result)) { //the code for these queries is not shown
echo 'getting here'; <======= WORKS
echo $venue; <======= WORKS
echo $startTime2; <======= DOESNT WORK
}
Your query is returning your time columns with an alias. To read them from your row you should use the alias as an index:
$startTime2 = $rowevent['startTime2'];
$endTime2 = $rowevent['endTime2'];
1.In your loop where does $results came from??
2.You set startTime as startTime2 in your query same as endTime to endTime2.
and obviously it will not work even your echo $venue.. because you fetch nothing.
it should be like this code:
$date = new DateTime($originaldate);
$resultevent = mysql_query('SELECT venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');
while ($rowevent = mysql_fetch_assoc($resultevent)) { //now you fetch your query in $resultevent
$newdate = $date->format('m/d/Y');
$startTime2 = $rowevent['startTime2'];
$endTime2 = $rowevent['endTime2'];
$venue = $rowevent['venue'];
echo 'getting here';
echo $venue;
echo $startTime2;
}
Related
I have a PHP function which inserts a date value from my SQL database into my PHP page here
$DateSql = "SELECT * FROM `ins_schedule` WHERE `active` = 1";
$DateResult = mysqli_query($connect, $DateSql);
$DateResultCheck = mysqli_num_rows($DateResult);
if ($DateResultCheck > 0){
while($row = mysqli_fetch_assoc($DateResult)){
echo "<p style=\"margin-left: 15px;\">{$row[('insider_date')]}</p>";
}
}
This method works fine for printing out the date like this
2020-01-31
But I want the date data to print out as "January 31, 2020". Would anyone know how to output the data like this?
There's a couple ways to do date formatting in PHP. Here I use the DateTime::format() method:
$DateSql = "SELECT * FROM `ins_schedule` WHERE `active` = 1";
$DateResult = mysqli_query($connect, $DateSql);
$DateResultCheck = mysqli_num_rows($DateResult);
if ( $DateResultCheck > 0 ) {
while ( $row = mysqli_fetch_assoc($DateResult) ) {
$date = new DateTime( $row['insider_date'] );
echo '<p style="margin-left: 15px;">' . $date->format('F j, Y') . '</p>';
}
}
I have a small PHP page which takes data from MySQL and displays it via PHP in a monthly calendar. I'm having trouble arranging the data properly within an array to get the desired output.
First, I will describe what I would like to happen:
students come to classes on regular days of the week
they can also make or cancel reservations
the calendar also displays days when the school is not open
In order to display this data on the calendar, I use MySQL to output data from a variety of sources, and then input that into an array with PHP, which I sort by date and output.
My issue is, I would like to be able to handle more than one row of data per day, but because I am using the date as the key, I am limited on only displaying one result per day. If I use a loop to append the date with a counter in the key, I get overlapping results in situations where someone made a reservation and then cancelled that reservation on the same day.
As for my code...
First, I check to see if the student is registered in a weekly class, then input that class into the array.
$sql = "SELECT StudentDB.studentid, ClassDB.classID, ClassDB.class_level, ClassDB.class_title, ClassDB.time, ClassDB.teacher, StudentDB.first_name, StudentDB.last_name, StudentDB.payment_amount, ClassDB.day
FROM ClassDB
INNER JOIN RegDB ON ClassDB.classID = RegDB.classid
INNER JOIN StudentDB ON StudentDB.studentID = RegDB.studentid
WHERE StudentDB.studentid = '$studentid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) { // DISPLAY REGULAR CLASS DATA
$dayofclass = $row['day'];
$class_level = $row['class_level'];
$class_title = $row["class_title"];
$day = $row["day"];
$class_time = $row["class_time"];
$time = $row["time"];
// check which dates match the days of the week and store in an array
for ($i=1;$i<=$n;$i++){
if ($i<10) {
$i = "0" . $i;
}
$day=date("l",strtotime($yearmonth.$i)); //find weekdays
if($day==$dayofclass){
$time = date("H:i",strtotime($row['time']));
$dates[]=$yearmonth.$i;
$datesdata[$yearmonth.$i] = "0";
$timedata[$yearmonth.$i] = $time;
$classiddate[$yearmonth.$i] = $row['classID'];
}
}
}
echo "</table>";
$conn->close();
}
After that, I check for specific reservations (cancelations, irregular reservations, waitlists) and input them into the array:
$lowerlimit = $yearmonth . "01";
$upperlimit = $yearmonth . "31";
$sql = "SELECT AttendanceDB.*, ClassDB.*
FROM StudentDB
INNER JOIN AttendanceDB ON StudentDB.studentid = AttendanceDB.studentid
INNER JOIN ClassDB ON AttendanceDB.classid = ClassDB.classID
WHERE StudentDB.studentid = '$studentid'
AND AttendanceDB.class_time >= '$lowerlimit'
AND AttendanceDB.class_time <= '$upperlimit'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$loopcount = 0;
// store furikae data in the array
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["class_time"] );
$time = date("H:i",strtotime($row['time']));
$mysqldate = date( 'Y-m-d', $phpdate );
$loopcount++;
$mysqldate = $mysqldate . "+" . $loopcount;
// $loopcount++;
// $mysqldate = $mysqldate . "+" . $loopcount;
$previousdate = $mysqldate;
$previousfurikae = $row['furikae'];
if ($row["furikae"] == 3){
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "1";
$timedata[$mysqldate] = $time;
$classiddate[$mysqldate] = $row['classID'];
} elseif ($row["furikae"] == 8 OR $row["furikae"] == 7) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "3";
$timedata[$mysqldate] = $time;
} elseif ($row["furikae"] == 2) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "2";
$timedata[$mysqldate] = $time;
}
}
}
$conn->close();
Then finally I check the school calendar and input the days off into the array:
$sql = "SELECT *
FROM SchoolScheduleDB
WHERE date >= '$lowerlimit'
AND date <= '$upperlimit'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// store furikae data in the array
while($row = $result->fetch_assoc()) {
$phpdate = strtotime( $row["date"] );
// $time = date("H:i",strtotime($row['time']));
// $mysqldate = date( 'Y-m-d', $phpdate ) . " " . $time;
$mysqldate = date( 'Y-m-d', $phpdate );
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "666";
}
}
$conn->close();
The way I intended it to work was that:
First the regular classes would be input
Then any reservations would overwrite the original plans
And finally the school calendar would overwrite everything
Currently, this functions as it should, but it is limited to displaying 1 result per day, but I would like to be able to display more than 1 result per day for students who come to multiple classes.
Thank you for your help. If I made any mistakes in my question or my question is unclear I will do my best to revise it.
You can make a Sub-Array for each date by using edged brackets:
$data[20180528][] = 'aa';
$data[20180528][] = 'bb';
$data[20180529][] = 'cc';
$data[20180529][] = 'dd';
$data[20180529][] = 'ee';
will give you an Array like this:
20180528 => aa
=> bb
20180529 => cc
=> dd
=> ee
I can't figure it out! I've created an array of months in "Slovenian language" and now, I would want to display my month's name instead of the number. Instead of working, it writes out - 32014vEurope/Berlin11bEurope/BerlinWed and some more weird stuff, it should obviously print out November in my case. I would like to solve this problem with arrays, but It just wouldn0t convert the number of 'n' to the requested month.
function kliknjena($link, $mojster)
{
$meseci[1] = "Januar";
$meseci[2] = "Februar";
$meseci[3] = "Marec";
$meseci[4] = "April";
$meseci[5] = "Maj";
$meseci[6] = "Junij";
$meseci[7] = "Julij";
$meseci[8] = "Avgust";
$meseci[9] = "September";
$meseci[10] = "Oktober";
$meseci[11] = "November";
$meseci[12] = "December";
$sql = "SELECT naslov, podnaslov, vsebina, ustvarjeno, slug FROM novica
where slug = '$mojster'
limit 1";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<h1>".$row["naslov"]."</h1>";
$timestamp = strtotime($row["ustvarjeno"]);
$m = date("n", $timestamp);
echo date("d. $meseci[$m]; Y, H:i:s", $timestamp);
echo "<p>".$row["podnaslov"]."</p>"."<br>";
echo "<p>".$row["vsebina"]."</p>"."<br>";
}
}
else
{
echo "0 results";
}
}
Use:
echo date('d. ', $timestamp) . $meseci[$m] . date('; Y, H:i:s', $timestamp);
What's happening is that the month name is being substituted into the date() argument, and then all the letters in the month are being treated as formatting characters, so they get replaced with the corresponding fields from the date and time.
This is for my OJT project. I need to search birthday from MySQL database in DATE type. I already got the query from January to December. The December to January is the problem.
This is what i've done code so far:
<?php
require 'connect.php';
?>
<?php
if(isset($_POST['birthday_from']) && isset($_POST['birthday_to'])){
$start_date=$_POST['birthday_from'];
$end_date=$_POST['birthday_to'];
//$s = strtolower($end_date);
$start_year = date('m-d', strtotime($start_date));
$end_year = date('m-d', strtotime($end_date));
$search_year = date('Y', strtotime($start_date));
$search_year2 = date('Y', strtotime($end_date));
$sql=" select * from members where (DATE_FORMAT(`birthday`, '%m-%d') BETWEEN '{$start_year}' AND '{$end_year}') order by DATE_FORMAT(`birthday`, '%m-%d') asc ";
$result = mysql_query($sql);
$num_row = mysql_num_rows($result);
if($num_row >= 1){
echo '<div class="alert alert-info" style="margin:auto;width:900px;"><b><center>BIRTHDAY RESULT</center></b></div>';
while($rows = mysql_fetch_array($result)){
$date = date('Y-m-d')-$rows['birthday'];
echo '<b><span style="margin-left:20px;">Name:</span></b> '.strtoupper($rows['firstname'].' '.$rows['middlename'].' '.$rows['lastname'].' '.$rows['suffix']).'<br/>';
echo '<b><span>Birthday:</span></b> '.$rows['birthday'].'<br/>';
echo '<b><span style="margin-left:30px;">Age: </span></b>'.$date.'<br/><br />';
}
}
}
?>
You do not check the case when start date > end date. It is happens.
You named var $start_year = date('m-d', strtotime($start_date)), but there is no any Year in value. It is bad practice.
You do not use vars search_year, search_year2 after defining and assigning.
I do not understand what You want, can You give the example with several rows in table and result what You want to get but You cannot?
Looks like you're using the wrong variables for your BETWEEN statement.
BETWEEN '{$start_year}' AND '{$end_year}'
should be:
BETWEEN '{$seach_year}' AND '{$search_year2}'
<?php
$con=mysqli_connect("localhost","*********","***********","loadtracker");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Date FROM pallettracker GROUP BY Date");
$DateList = mysqli_fetch_array($result);
$ListOfMonths=array('Dec','Nov','Oct','Sep','Aug','Jul','Jun','May','Apr','Mar','Feb','Jan');$ListOfDays = array('31','30','29','28','27','26','25','24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6','5','4','3','2','1');
foreach ($ListOfDays as $value) {
foreach ($ListOfMonths as $value2) {
$Yaer = array("'$value2' + '$value' + ',2013'");
foreach ($Yaer as $yaerdate) {
foreach ($DateList as $Dates) {
if ($yaerdate == $Dates)
{
echo $yaerdate;
echo '/';
}
}
}
}
}
?>
trying to order a array of dates foratted as Jan28,2013 and Decending. And then display then with a / in-between them. For an Android App
You can have your SQL query order them for you. This assuming that the column is of the Date format. If it is not check out this: SQL ORDER BY date problem.
After ordering in the query, you just need to convert it to a time then format the date as desired. This might give you what you are looking for:
<?
$datetime = strtotime($row->createdate);
$mysqldate = date("m/d/y g:i A", $datetime);
$result = mysqli_query($con,"SELECT Date FROM pallettracker GROUP BY Date");
foreach($result as $date){
$datetime = strtotime($result);
$date = date("M d, y", $datetime);
echo $date . '/';
}
?>
I don't get it, why you are using GROUP BY when you don't use a grouping function.
The better way here is DISTINCT
I would update your code in this way:
$result = mysqli_query($con,"SELECT DISTINCT `Date` FROM `pallettracker` ORDER BY `Date` DESC");
while($date = mysqli_fetch_assoc($result)
{
echo date('',strtotime($date['Date'])).'<br />';
}
It is important that you "Date" col in mysql is in the format Date or Datetime