Here's what I need to have happen:
I have created variables that include the dates for the current week.
I have also created an array that has the sport types in it as $k => $v.
I created a javascript accordion that will open and close if clicked on. The dates of the week are used as the title of the accordion. I want to then return all the records, in their respective accordion, based on the date in the database but I also need to list those records according to sport type. So for example:
Accordion * October 2, 2013 * Accordion (when user clicks it will open and show all the records that have a game on that date listed under the sport type).
I need to echo the value of the sport type array I created if the record matches the key for the sport type. For example: if the database returns three rows of data, one having the sport type of "baseball" and two having the sport type of "MSoccer" then I need to return the records like this in a table:
BASEBALL (sport type)
VI vs. UVA close td at 6:00 PM EST
MEN'S SOCCER
VI # Montreat at 3:30 PM EST
VI vs. St. Andrews at 4:00 PM EST
if there are no records that have a game on the date then I would just echo "no games today"
Then I need to create the next accordion item and return the records for that one where the game date matches the date for the accordion.
Here's what I have so far...just not sure where to go with it:
<?php
date_default_timezone_set('US/Eastern');
$today = time();
$weekMonDate = date('Y-m-d',strtotime("last Monday", $today));
$weekTuesDate = date('Y-m-d', strtotime('+1 days', strtotime($weekStartDate)));
$weekWedDate = date('Y-m-d', strtotime('+2 days', strtotime($weekStartDate)));
$weekThursDate = date('Y-m-d', strtotime('+3 days', strtotime($weekStartDate)));
$weekFriDate = date('Y-m-d', strtotime('+4 days', strtotime($weekStartDate)));
$weekSatDate = date('Y-m-d', strtotime('+5 days', strtotime($weekStartDate)));
$weekSunDate = date('Y-m-d', strtotime('+6 days', strtotime($weekStartDate)));
$sport = array(
"Baseball" => 'BASEBALL',
"MSoccer" => 'MEN\'S SOCCER',
"MBasketball" => 'MEN\'S BASKETBALL',
);
foreach ($sport as $k => $v) {
$sql = 'SELECT gametime, conference, scrimmage, exhibition, gamedate, homeschool, visitorschool, homelivestatsurl, notes, gamestatus, homescore, visitorscore, score, record, sporttype FROM schedule WHERE WEEKOFYEAR(gamedate)=WEEKOFYEAR(NOW()) ORDER BY gamedate';
$result = mysql_query($sql, $link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
?>
<?php
mysql_data_seek($result, 0);
$day = ' ';
$sport = ' ';
while ($row = mysql_fetch_assoc($result)) {
if (date("d", strtotime($row['gamedate'])) !== $day) {
$day=date("d", strtotime($row['gamedate'])); ?>
?>
Related
How can someone print fullcalendar or export fullcalendar in PDF or Excel format?
For example:
Input:
Start Date: 01 Jan 2020
End Date: 31 Dec 2020
On click of button it will get all of the events between given date ranges and populate it to the fullcalendar
There will be something like...
26th January 2020 which will have an event - Australia Day
14th February 2020 which will have an event - Valentines day
...
...
25th December 2020 which will have an event - X-Mas Day
As you can see there will be multiple events in different months and what I am looking for is printing or exporting this fullcalendar in a single page with all 12 number of months.
Your response will help many.
You can use the below sample code by editing , hope it helps
<?php
header("Content-Type: text/html; charset=iso-8859-9");
header("Content-Type: text/html; charset=windows-1254");
header("Content-Type: text/html; charset=x-mac-turkish");
$today = date("d-m-Y");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=$today-BOOK.xls");
$date = '2020-05-01';
//get end date of month
$end = '2020-05-' . date('t', strtotime($date));
include("../../DATABASE.php");
?>
<table>
<tr>
<td>PERSONAL</td>
<?php
while(strtotime($date) <= strtotime($end)){
$day_num = date('d', strtotime($date));
$day_name = date('l', strtotime($date));
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
echo "<td>$day_num <br/> $day_name</td>";
}
?>
</tr>
<?php
//get end date of month
$hsql=mysql_query("select * from XXXUSER");
while($hyaz=mysql_fetch_array($hsql)){
$firstname=$hyaz["firstname"];
$kullanicidi=$hyaz["id"];
echo "<tr>";
echo "<td>$firstname</td>";
$fromdate = '2020-06-01';
$todate = '2020-06-' . date('t', strtotime($fromdate));
while(strtotime($fromdate) <= strtotime($todate)) {
$geneltarih = date('Y-m-d', strtotime($fromdate));
$fromdate = date("Y-m-d", strtotime("+1 day",strtotime($fromdate)));
$xxxsql = mysql_fetch_array(mysql_query("select * from XXXSCHEDULE where kaynakcodu='$kullanicidi' and start_event='$geneltarih'"));
$title = $xxxsql["title"];
echo " <td>$title</td>";
}
echo "</tr>";
}
?>
</table>
I have a table message, in which fields are: id,username,message,lastdate. I want to compare from these dates (which are in my table) to current date. If difference is 30 days then respected username should be show in admin page.
I tried for date difference, but its not working.
<?php
include("connection.php");
$queryd=mysql_query("select * from message") or die(mysql_error());
while($resultd=mysql_fetch_array($queryd))
{
extract($resultd);
$date1=date('Y-m-d');
$date2=$lastdate;
$diff=date_diff($date2,$date1);
echo $diff->format("%R%a days");
}
?>
Also I tried from this code. But nothing happend
<?php
include("connection.php");
$queryd=mysql_query("select * from message") or die(mysql_error());
while($resultd=mysql_fetch_array($queryd))
{
extract($resultd);
$date1=date('Y-m-d');
$date2=$lastdate;
$query12=mysql_query("SELECT username,DATEDIFF($date1,$date2) FROM message WHERE DATEDIFF($date1,$date2)<30") or die(mysql_error());
while($result12=mysql_fetch_array($query12))
{
if($result12)
echo $username;
else
echo"record not";
}
}
?
My third solution is here. I think it is working. But It is repeating values.
<?php
include("connection.php");
$queryd=mysql_query("select * from message") or die(mysql_error());
while($resultd=mysql_fetch_array($queryd))
{
extract($resultd);
$date1=date('Y-m-d');
$date2=$lastdate;
$days = (strtotime($date2) - strtotime($date1)) / (60 * 60 * 24);
$result123=mysql_query("select username from message where '$days'<30");
while($query123=mysql_fetch_array($result123))
{
if($query123)
echo $username." ".$days."<br>";
else
echo mysql_error();
}
}
?>
You can do this date comparison inside your MySQL DBMS.
Try this query:
SELECT username, message FROM message WHERE lastdate <= CURDATE() - INTERVAL 30 DAY
It should return just the rows of interest.
I want to select MySQLi records between a startdate and enddate so I have a JS script that allows me to enter dates (using HTML datebox type inputs) which I pass to a PHP script using JQuery's $.post method. My JS code is:
startDate = new Date(document.getElementById("fromDate").value);
startDate = new Date(startDate.getUTCFullYear(),startDate.getUTCMonth(), startDate.getUTCDate(), startDate.getUTCHours(), startDate.getUTCMinutes(), startDate.getUTCSeconds());
endDate = new Date(document.getElementById("toDate").value);
endDate = new Date(endDate.getUTCFullYear(), endDate.getUTCMonth(), endDate.getUTCDate(), endDate.getUTCHours(), endDate.getUTCMinutes(), endDate.getUTCSeconds());
$.post("DBQuery.php", {
startdate: startDate,
enddate: endDate,
},
function (output) {
var result = JSON.parse(output);
$('#PHPStartDate').html(result.startdate).show();
$('#PHPEndDate').html(result.enddate).show();
});
My PHP code is:
$startdate = date('Y-m-d h:i:s',strtotime($_POST["startdate"]));
$enddate = date('Y-m-d h:i:s',strtotime($_POST["enddate"]));
$con = mysqli_connect("localhost","username","password","dbase");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM Hits WHERE timestamp BETWEEN '$startdate' AND '$enddate'";
if ($result = mysqli_query($con,$sql)) {
$visitors = mysqli_num_rows($result);
}
$sql="SELECT DISTINCT session FROM Hits WHERE timestamp BETWEEN '$startdate' AND '$enddate'";
if ($result = mysqli_query($con,$sql)) {
$sessions = mysqli_num_rows($result);
}
$sql="SELECT DISTINCT country FROM Hits WHERE timestamp BETWEEN '$startdate' AND '$enddate'";
if ($result = mysqli_query($con,$sql)) {
$countries = mysqli_num_rows($result);
}
$sql="SELECT DISTINCT city FROM Hits WHERE timestamp BETWEEN '$startdate' AND '$enddate'";
if ($result = mysqli_query($con,$sql)) {
$cities = mysqli_num_rows($result);
}
$output = json_encode(array("visitors"=>$visitors,"sessions"=>$sessions,"countries"=>$countries,"cities"=>$cities,"startdate"=>$startdate,"enddate"=>$enddate));
echo $output;
mysqli_free_result($result);
mysqli_close($con);
However the dates returned by PHP are 18 hours behind the dates which JS passed to PHP - very weird! My browser and MAMP server are on the same machine (my iMac) and I live in Thailand (UTC + 7 hours).
Here's a screen shot of what is displayed in HTML using the following code:
<div id="reportTitle">
Report of site activity from
<span id="startTime"></span>on<span id="startDate"></span>to
<span id="endTime"></span>on<span id="endDate"></span><br>
for<span id="showVisitors"></span>
<span id="showCities"></span>
<span id="showCountries"></span>
</div>
<div id="reportBody">
<div>
<span><h3>From</h3></span><span id="PHPStartDate"></span>
<span><h3>to</h3></span><span id="PHPEndDate"></span>
The first set of dates displayed at the top of the screenshot are simply the JS startdate and enddatevariables and the lower set of dates are those returned by the PHP variables $startdate and $enddate which ought not to have changed (but in fact are 18 hours earlier!).
Screenshot of JS and PHP dates
I suspect the problem is to do with timezones but given that Thailand is UTC+7 it's hard to work out where the 18 hour time difference is coming from.
I have a calendar on my site that uses javascript/jquery to place events in the calendar. All the events are stored in a MySQL database with the title and Unix timestamp.
My problem is that this method (shown in the code below) only allows one event per day, and creating two rows with the same date, only uses the latter of the two. There are several instances where I need two or more events in one day.
My code:
var CalendarEvent = function (time, title) {
this.time = time;
this.title = title;
}
var allEvents = {
<?php require_once 'ndx/mysql.php';
$events = mysqli_query($con, "SELECT * FROM events");
while($lse = mysqli_fetch_array($events)):
$date = date('Y-m-d', $lse['timestamp']);
$notime = $lse['notime'];
if($notime != 'true'){
$time = date('g:i a', $lse['timestamp']);
}else{
$time = '';
}
$event = addslashes($lse['title']);
?>
'<?php echo $date; ?>': [
new CalendarEvent('<?php echo $time; ?>', '<?php echo $event; ?>')
],
<?php endwhile; ?>
I have a main select list of courses which drives various things on a page. When a course is selected another select list will be repopulated with the start date of that course up to 6 months in advance. Also, I have a table on the page with the students name and phone number, when a course is selected, the table will be repopulated with all the students enrolled onto that course. My problem is I will be getting various different things from PHP via JSON i.e. the students and the starting date. How can I therefore pass more than one thing to jQuery? What if the course select list affected not just 2 things but 3, 5 or even 10? How would we handle that with PHP and jQuery?
foreach($m as $meta)
{
$metaCourse = $this->getCourseInfo($meta['parent_course']);
//populate the select list with the name of each course
$metaSelectList .= '<option id="select'.$count.'" value="'.$metaCourse['id'].'">'.$metaCourse['fullname'].'</option>';
$count++;
//get only the first course's dates
if($count3 == 1)
{
$startDate = intval( $course->getStartDate(50) );
$endDate = strtotime('+6 month', $startDate);
//populates the select list with the starting date of the course up to the next six months
for($date = $startDate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$count2.'" value="'.$date.'">'.date('D d F Y', $date).'</option>';
$count2++;
}
$count3++;
$students = $s->getStudents($metaCourse['id']);
$content = $this->createStudentTable($students);
}
}
This is my handler for the AJAX...FOR NOW (I haven't implemented the students table yet as I'm still trying to figure out how to pass multiple pieces of data to jQuery). Basically each time a course is selected, PHP creates a new select list with the appropriate dates and then passes it to jQuery. I'm not sure if I should do this in JavaScript or in PHP.
if (isset($_GET['pid']) && (isset($_GET['ajax']) && $_GET['ajax'] == "true"))//this is for lesson select list
{
$pid = intval( $_GET['pid'] );
$c = new CourseCreator();
$startDate = intval( $c->getStartDate($pid) );
$endDate = strtotime('+6 month', $startDate);
$dateSelectList = '<select name="dateSelect" id="dateSelect">';
//populates the select list with the starting date of the course up to the next six months
for($date = $startDate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$count2.'" value="'.$date.'">'.date('D d F Y', $date).'</option>';
$count2++;
}
$dateSelectList .= '</select>';
echo json_encode($dateSelectList);
exit;
}
My jQuery handler:
$('#metaSelect').live('change', function()
{
$.getJSON('?ajax=true&pid='+$('#metaSelect').val(), function(data)
{
alert(data);
$('#dateSelectDiv').html(data);
});
});
You can easily pass ALOT of data from PHP to your HTML via JSON (which you seem to of put in basic already)
However to expand on what you have - heres a quick example
<?php
$arrayOfStuff = array("theKey" => "theEntry", 123 => "Bob Dow", 56 => "Charlie Bronw", 20 => 'Monkey!', "theMyID" => $_POST['myID']);
echo json_encode($arrayOfStuff);
?>
On your HTML side.
<script>
$.post("/theurl/", {type: "fetchArrayOfStuff", myID: 24}, function(success) {
//your success object will look like this
/*
{
theKey: 'theEntry',
123: 'Bob Dow',
56: 'Charlie Bronw',
20: 'Monkey!',
theMyID: 24
}
so you can easily access any of the data.
alert(success.theKey);
alert(success[123]);
alert(success[56]);
alert(success[20]);
alert(success.theMyID);
*/
//we can iterate through the success JSON!
for(var x in success) {
alert(x + "::" + success[x]);
};
}, "json");
</script>
In the long run - your MUCH better of letting the backend do the backend stuff, and the front end doing the front-end stuff.
What this means is, try keep the HTML generation as far away as possible from the back-end, so instead of constantly passing
for($date = $startDate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$count2.'" value="'.$date.'">'.date('D d F Y', $date).'</option>';
$count2++;
}
You could perhaps
$date = $startDate;
$myJson = array()
while($date <= $endDate) {
$myJson[] = array("date" => $date, "formattedDate" => date('D d F Y', $date));
$date += 86400; //86400 is the value of 1 day.
}
echo json_encode($myJson);
And you can just do a simple iteration on your HTML code.
<script>
$.get("/", {ajax: true, pid: $('#metaSelect').val()}, function(success) {
//we can iterate through the success JSON!
var _dom = $('#dateSelectDiv').html(''); //just to clear it out.
for(var x in success) {
_dom.append("<option value='"+success[x].date+"'>"+success[x].formattedDate+"</option>");
};
}, "json");
</script>
So as you can see - you can pass alot of data using JSON
Maybe look at some of the documentation to - http://api.jquery.com/jQuery.get/ , http://api.jquery.com/jQuery.post/ - might give you more ideas.
Best of luck to you