PHP calendar with dates --> distinct weeks and merging rows - php

I am currently creating a calendar where you can see all users that are saved in the database for that day and the possibility to add an user per date.
I want to work with dates because I think it is easier to work with in the future.
So what I am doing: I save a date into the database combined to an userid.
While I am fetching the data from database into a table I am converting the dates to days and weeks and years. Just to see which day is at 2015/10/05.
So I get $day = Monday, $week = 41, $year = 2015
In the table I want to put the users to the matching day.
The calendar looks like this:
I am calling an if statement:
if ($day == 'Mon') {
// SELECT * FROM users WHERE datum=$date
// Fetch and echo the users name into that day
}
The database looks like this:
The weeks showed here in the database, maybe they can be used for a proper display? But I don't know I tried several things.
I am using this code for putting the database rows into a table:
$sql = "SELECT distinct(week),datum, van , tot FROM roosternew ORDER BY datum ASC";
$run_sql = mysqli_query($conn , $sql);
?>
<table class="table">
<thead>
<tr class="primary">
<th width="5%"><center>Week</center></th>
<th width="10%">Maandag</th>
<th width="10%">Dinsdag</th>
<th width="10%">Woensdag</th>
<th width="10%">Donderdag</th>
<th width="10%">Vrijdag</th>
<th width="10%">Zaterdag</th>
<th width="10%">Zondag</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($run_sql)) {
echo $row['datum'].'test';
$datum = $row['datum'];
$van = $row['van'];
$tot = $row['tot'];
$tijd = $van.' - '.$tot;
$date = new DateTime($datum);
$week = $date->format("W");
$dag = $date->format("D");
$jaar = $date->format("Y");
$newdate_ = new DateTime($datum);
$newdate = $newdate_->format('Y-m-d');
$week_start = new DateTime();
$week_start->setISODate($jaar,$week);
$thisdate_ = $week_start->format('d-m');
//Create plus button
$plus_button = ' <i class="fa fa-plus-square"></i><br/>';
//Plus button
$thisdate = '<div class="datetable"><div class="datetable">'.$thisdate_.'</div></div><br/>';
?>
<tr>
<td class="primary"><center><strong><?= $week; ?></strong><br/><?php echo $thisdate.'<h4><small>'.$jaar.'</small></h4>'; ?></center></td>
<td>
<?php
createPlusButton(0, $jaar,$week);
if ($dag == 'Mon') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(1, $jaar,$week);
if ($dag == 'Tue') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(2, $jaar,$week);
if ($dag == 'Wed') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(3, $jaar,$week);
if ($dag == 'Thu') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(4, $jaar,$week);
if ($dag == 'Fri') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(5, $jaar,$week);
if ($dag == 'Sat') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(6, $jaar,$week);
if ($dag == 'Sun') {
getScheduleAdmin($newdate);
}
?>
</td>
</tr>
<? } ?>
</tbody>
</table>
The function getScheduleAdmin($newdate):
function getScheduleAdmin($newdate) {
require('connect.php');
$sql2 = "SELECT * FROM roosternew WHERE datum='$newdate'";
$run_sql2 = mysqli_query($conn , $sql2);
while ($row2 = mysqli_fetch_assoc($run_sql2)) {
$id_rooster_users = $row2['idusers'];
$sql= mysqli_query($conn,"SELECT * FROM users WHERE idusers='$id_rooster_users'");
$row = mysqli_fetch_array($sql);
$name = $row['name'];
$color = $row['color'];
$sql3 = "SELECT van,tot FROM roosternew WHERE idusers= '$id_rooster_users'";
$sql3_run = mysqli_query($conn,$sql3);
$sql3_array = mysqli_fetch_array($sql3_run);
$van_user = $sql3_array['van'];
$tot_user = $sql3_array['tot'];
$tijd_user = $van_user.' - '.$tot_user;
echo ''.$name.'<br/>'.'('.$tijd_user.')';
echo '<br/>';
}
}
The problem is described in the first image (the calendar), it will show me more than once the week and they won't merge.
I dont know how to merge the weeks so that it will be :
Something like that.
And not like this (situation right now):
if anyone knows a better way of doing this, please tell me.

Related

How to compare days with current days in php?

<table class="table row-border order-column">
<thead>
<tr>
<th></th>
<?php
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($i=1;$i<=$days;$i++)
{
?>
<th><?php echo $i; ?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($student as $key => $value)
{
?>
<tr>
<td class="cap"><?php echo $value->name; ?></td>
<?php
for($i=1;$i<=$days;$i++)
{
$result = $this->db->select('*')->from('attendance')->where('studentID',$value->studentID)->where('day(date)', date($i))->where('month(date)', date($month))->where('Year(date)', date($year))->get()->row();
if(empty($result))
{
$date = date('Y-m-d');
$current_date = date('d',strtotime($date));
if($i < $current_date)
{
?>
<td class="absent">A</td>
<?php
}
else
{
?>
<td></td>
<?php
}
}
else
{
$dateday = date('d',strtotime($result->date));
if($i == $dateday)
{
?>
<td class="present">P</td>
<?php
}
}
}
?>
</tr>
<?php
}
?>
</tbody>
In the above code I want to show absent while compare $current_date and $days but now what happen here only current month date show absent and previous month date show blank. Now, What am I going to do I don't want to show absent in future date. So, How can I do this? Please help me.
Thank You
Use Unix timestamp for your calculation and after that change Unix to gmt or utc time using ready functions to show.

PHP Mysql query optimization for report

I have this report which filters data from 5 tables but it is very slow takes around 10 seconds. I tried using index on some columns but it did not help.
Basically the first query is the master and the others are for filtering it if the other queries condition met then it will skip it.
this is the script:
<div class="col-md-12">
<h3>List of Outstandings</h3>
<table class="table table-condensed table-bordered table-hover small">
<thead>
<tr>
<th >#</th>
<th>PR #</th>
<th>PR Type</th>
<th>Description</th>
<th>Dep.</th>
<th>Date</th>
<th>Requester</th>
<th>Assigned to</th>
</tr>
</thead>
<tbody>
<?php
$chkk = 0;
$sql = "SELECT * FROM msr WHERE Status ='Approved' ";
$result6 = $connn->query($sql);
if ($result6->num_rows > 0) {
$vo = 1;
while ($row0 = $result6->fetch_assoc()) {
$chkk = 0;
$MSRID = $row0["MSRID"];
$MSRType = $row0["MSRType"];
$result4 = "SELECT owner FROM tracking WHERE MSRID='$MSRID' ";
$MyRow4 = $connn->query($result4);
$row4 = $MyRow4->fetch_assoc();
$actionBy = $row4["owner"];
$resultusr = "SELECT RFQID FROM rfq WHERE MSRID='$MSRID' AND NOPO='No' ";
$MyRowusr = $connn->query($resultusr);
$rowusr = $MyRowusr->fetch_assoc();
$rfqcount = mysqli_num_rows($MyRowusr);
if ($rfqcount > 0) {
$chkk = 1;
}
$resultusr4 = "SELECT POID FROM po WHERE MSRID='$MSRID' ";
$MyRowusr4 = $connn->query($resultusr4);
$rowusr4 = $MyRowusr4->fetch_assoc();
$rfqcount4 = mysqli_num_rows($MyRowusr4);
if ($rfqcount4 > 0) {
$chkk = 1;
}
$resultusr1 = "SELECT MSRID FROM contract WHERE MSRID='$MSRID' ";
$MyRowusr1 = $connn->query($resultusr1);
$rowusr1 = $MyRowusr1->fetch_assoc();
$rfqcount1 = mysqli_num_rows($MyRowusr1);
if ($rfqcount1 > 0) {
$chkk = 1;
}
if ($chkk == 1) {
continue;
}
?>
<tr>
<td>
<?php echo $vo; ?>
</td>
<td>
<?php echo $row0["MSRID"]; ?>
</td>
<td>
<?php echo $row0["MSRType"]; ?>
</td>
<td>
<?php echo $row0["purposeofbuying"]; ?>
</td>
<td>
<?php echo depName($row0["DepRequester"]); ?>
</td>
<td>
<?php echo $row0["RequestDate"]; ?>
</td>
<td>
<?php echo reqName($row0["RequestPer"]); ?>
</td>
<td>
<?php echo reqName($actionBy); ?>
</td>
</tr>
<?php
$vo++;
}
}
?>
</tbody>
</table>
</div>
</div>
You can use subquery method instead of looping.
Example:
$sql = "SELECT *,
( SELECT owner
FROM tracking
WHERE tracking.MSRID= msr.MSRID
) AS _owner,
( SELECT RFQID
FROM rfq
WHERE rfq.MSRID= msr.MSRID
AND rfq.NOPO='No'
) AS _RFQID
FROM msr
WHERE rfq.Status ='Approved'";

Show today's birthday

I want to use the script i placed underneath, but it should show me who's having birthday today. i have added a birthday in my sql table ion this format: 1985-06-03
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$results = mysql_query("SELECT * FROM demo LIMIT 10");
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
DB Table structure:
ID INT11
FirstName Varchar
LastName Varchar
Department Varchar
Birthday Date (yyyy-mm-dd)
Since, you will need to exclude the year, you can use the MONTH and DAY SQL functions like so:
SELECT * FROM table WHERE DAY(birthday) = DAY(CURDATE()) AND MONTH(birthday) = MONTH(CURDATE());
In your query, format each date to be MM-DD.
SELECT *
FROM demo
WHERE DATE_FORMAT(birthday, "%c-%d") = DATE_FORMAT(NOW(), "%c-%d")
LIMIT 10
This will bring back results where the MM-DD of both NOW() and the birthday value are equal.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$today_date = date('d');
$today_month = date('m');
$results = mysql_query("SELECT * FROM `table_name` where DATE('dob_column') = $today_date && MONTH(`dob_column`) = $today_month");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
}else{
echo "No one birthday on today enter code here";
}
?>
Can you try something like this:
$results = mysql_query("SELECT * FROM demo WHERE MONTH(`table_column`) = '".date('m')."' AND YEAR(`table_column`) = '".date('Y')."'");

how to give style to the SQL table when checkbox selected?

SO i have form which consist of "Event Name" "Event Description" "Event Date" and checkbox "is important". When i check checkbox value "yes" its important, it sends to the sql value = "1" to table "is_important". Everything is all right, but i give the bootstrap style "bg-danger" for that "is_important" = 1 table and it doesnt show up. What's the problem?
You can see in the code:
<?php
if (isset($_POST['important'])) {
$error = array();
$success = array();
$eventTime = time();
$important = $_POST['important'];
$eventName = trim(mysql_real_escape_string($_POST['EventName']));
$eventDesc = htmlentities(trim(mysql_real_escape_string($_POST['EventDesc'])), ENT_QUOTES);
if (!isset($eventName) || empty($eventName)) {
$error['eventName'] = "Prasome ivesti ivykio varda";
} else if (strlen($eventName) > 32 || strlen($eventName) < 3) {
$error['eventName'] = "Ivykio pavadinimas turi buti tarp 3 ir 32 simboliu";
}
if (!isset($eventDesc) || empty($eventDesc)) {
$error['eventDesc'] = "Prasome ivesti ivykio aprasyma";
}
if (empty($error)) {
$sql = "INSERT INTO notes_list (title, description, timestamp,is_important) VALUES ('$eventName', '$eventDesc','$eventTime','$important')";
$result = mysqli_query($con, $sql);
$success[] = "SEKME !";
} else {
}
}
?>
<table class="table table-striped">
<thead>
<tr>
<th>Event name</th>
<th>Event description</th>
<th>Event date</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM notes_list ORDER BY id DESC LIMIT 10";
$result2 = mysqli_query($con, $query);
print_r($_POST);
if ($result2) {
while ($note = mysqli_fetch_assoc($result2)) {
?>
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
<td><?php echo $note['title']; ?></td>
<td><?php echo $note['description'] ?></td>
<td><?php echo date('l M jS', $note['timestamp']); ?></td>
</tr>
<?php
}
mysqli_free_result($result2);
}
/* close connection */
mysqli_close($con);
?>
</tbody>
</table>
Full Example in this picture:
https://www.dropbox.com/s/h650h2spy2487dm/chechbox.jpg?dl=0
This:
<tr<?php echo (($note['is_important'] == 1) ? "class='bg-danger'" : ""); ?>>
would render this:
<trclass='bg-danger'>
in case is_important is 1. You need a space there, before the class.

Order By Year with Date field Mysql/PHP

I am having trouble with getting my year to echo properly.
Example: I have 2 news articles one with a year of 2011 and the other 2012. They are splitting into the correctly "year" div. But when it comes to rendering out the year, it works. But 2011 and 2012 are stacked above each other. Something like this.
2012
2011
5.1.12 My news article
Where 2011 year is suppose to go
5.1.11 My news article
Here is my code.
<?
$startYear = 2010;
$endYear = 2012;
for($y = $endYear; $y > $startYear -1; $y--) { ?>
<?
$news = query("SELECT * FROM news_entries
WHERE title > ''
AND published = 1
AND date >= '" . mktime(0,0,0,0,0,$y) . "'
AND date < '" . mktime(0,0,0,0,0,$y+1) . "'
ORDER BY date DESC");
?>
The rest of the query.
<? if($news['total']>0) { ?>
<h3><? echo $y; ?></h3>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<th class="first" width="125">Date</th>
<th>News Headline</th>
<th width="140"> </th>
</tr>
<? do { ?>
<tr>
<td><span><? echo date("F d, Y",$news['data']['date']); ?></span></td>
<td><? echo $news['data']['headline']; ?></td>
<td>Learn More</td>
</tr>
<? } while($news['data'] = mysql_fetch_assoc($news['object'])); ?>
<? }} ?>
<tr>
<td colspan="2" class="last"></td>
<td class="last"> Back to Top</td>
</tr>
</table>
I agree with Kato's comment. Typically, you get all of the results and iterate over them, printing a new header when the data changes.
Here's how this type of thing is usually handled (pseudocode):
$sql = "SELECT * FROM news_enteries ORDER BY date DESC";
// Prepare statement
$stmt = $pdo->prepare($sql);
// Bind variables
...
// Execute query
$stmt->execute() or die();
$year_header = "";
while ($row = $stmt->fetch()) {
$year = date("Y", strtotime($row['date']));
// Do we need a new year header?
if ($year_header <> $year) {
$year_header = $year;
// Print year header
echo $year_header;
}
// Process rest of the row
...
}

Categories