Order By Year with Date field Mysql/PHP - 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
...
}

Related

How to calculate data from database in current month from first date of month until current date in month

The problem for this code is it calculate all data from all month that has in the database.. It not from this current month from first date of this month until current date of this month...Can someone help me...
<tbody>
<?php
$FullTotal = 0;
while($rowF = $resultX -> fetch_assoc()){// registerfighter
$totalPlus = 0;
//table start
?>
<tr>
<td><?php echo $rowF["UserName"] ?></td>
<?php
$resultY-> data_seek(0);
while($rowY = $resultY -> fetch_assoc()){ // by addplan
$resultR -> data_seek(0);
while($rowR = $resultR -> fetch_assoc()){ // by record
if ($first_date <= $rowR["SubmitDate"] && $last_date <= $rowR["SubmitDate"]){
// this the problems...it not work
if( ($rowF["UserID"]===$rowR["UserIDD"]) and
(($rowR["No_Plan"]) === '1' and
($rowY["No_Plan"] === $rowR['No_Plan']))) {
$total = ($rowR["UserInputNEW"] + $rowR["UserInputNMP"]);
$totalPlus = $totalPlus + $total;
$FullTotal = $FullTotal + $total;
}
}
}
}
?>
<td><?php echo $totalPlus; ?></td>
</tr>
<?php
//table end
} ?>
<td colspan="1"> Total </td>
<td colspan="1"> <?php echo $FullTotal?></td>
</tbody>

Display Rows with Month and Year PHP

I got all my archived entries to display like so,
2015 Entries
March [2]
April [10]
November [6]
2004 Entries
January [5]
February [7]
December [19]
Now I'm stuck and I've been trying to figure out how to display archived entries by selecting a specific month and year. Let's say I wanted to view on my all entries during November 2015, then my link on the address bar would look something like...
display.php?year=2015&monthname=November
When I executed the code, the entries shows up as blank. So far, my code looks like this. I also used the type datetime as my date field. Help is appreciated.
$year = strip_tags(trim($_GET['year']));
$monthname = strip_tags(trim($_GET['monthname']));
$q = "select * from blog where year(date)='$year' and monthname(date) ='$monthname' order by date desc ";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
// dynamic navigation variables
$rows_per_page=1; // adjust the number here to display number of entries per page
$total_records=mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);
$screen = $_GET["screen"];
if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
$q .= "LIMIT $start, $rows_per_page";
$result= mysql_query($q, $connection) or die
("Could not execute query : $q." . mysql_error());
while ($row=mysql_fetch_array($result))
{
$id=$row["id"];
$name=$row["name"];
$email=$row["email"];
$entry=$row["entry"];
$date=$row["date"];
$icon=$row["icon"];
$title=$row["title"];
?>
<table width="80%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><?php echo "$title"; ?></td>
</tr>
<tr>
<td>
<p><img src="<?php echo "$icon"; ?>" alt="icon" align="left"><?php echo "$entry"; ?></p>
<p>Posted by <a href="mailto:<?php echo "$email"; ?>"><?php echo "$name"; ?> on <?php echo "$date"; ?>.</p>
</td>
</tr>
</table>
<div align="center">
<?php
} #end of while
// Display dynamic navigation here
// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "display.php?year=$year&monthname=$monthname&screen=$j";
echo "Prev";
}
// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "display.php?year=$year&monthname=$monthname&screen=" . $i;
$j = $i + 1;
echo " | $j | ";
}
if ($screen < $pages-1) {
$j = $screen + 1;
$url = "display.php?year=$year&monthname=$monthname&screen=$j";
echo "Next";
}
?>
</div>
I forgot to put the word monthname on my address bar and accidentally left it as display.php?year=2015&month=November No code needed to be altered.

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

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.

How to display show the records of datename & year using this approach

This code display the record by month and year
example:
July 2015
August 2015
September 2015
here's my code its working.
<?php
$qry = "SELECT count(*), monthname(curdate), year(curdate) FROM cswd_records GROUP BY month(curdate), year(curdate)";
$run = mysql_query($qry) or die(mysql_error());
?>
</div>
<div class="col-md-3"></div>
<div class="col-md-6">
<Center><span class="impactsoc">Total Applicants by Month</span></center><br>
<table class="table table-striped table-bordered table-hover table-condensed" >
<thead>
<tr>
<th># of Applicants</th>
<th>Month</th>
</tr>
</thead>
<tbody>
</tr>
<?php while ($row = mysql_fetch_array($run)) {?>
<tr>
<td width="250"><h5><?php echo $row['count(*)']; ?></h5></td>
<td width="250"><h5><?php echo $row['monthname(curdate)'];?> <?php echo $row['year(curdate)'];?></h5> </td>
</tr>
<tr>
<td colspan="2"><center><h4><i>Total Applicants:</i><font color="red"> <?php echo $row['count(*)']; ?></font></h4></center></td>
</tr><?php }?>
</table>
this is my problem. how to display all the list of july 2015
(it works if I just want to show the month or year. I dont know if its 2 value).
url value:
month.php?monthyear=August-2015
this one is working if only 1 value. How to display both month and year?
$myr = $_GET['monthyear']; //getting the details by link(a href)
$result = $db->prepare("SELECT * FROM cswd_records WHERE monthname(curdate)='". mysql_real_escape_string($myr) ."'");
Solution:
$result = $db->prepare("SELECT * FROM cswd_records WHERE monthname(curdate) ='". mysql_real_escape_string($myr) ."'
AND year(curdate)='". mysql_real_escape_string($myr1) ."'");
Thanks to #Harshit Shrivastava
Why dont you put year also
$result = $db->prepare("SELECT * FROM cswd_records WHERE
monthname(curdate)='". mysql_real_escape_string($myr) ."'
AND year(curdate)='". mysql_real_escape_string($myr) ."'");
EDIT
$myr = explode("-", $_GET['monthyear']);
Use
$result = $db->prepare("SELECT * FROM cswd_records WHERE
monthname(curdate)='". mysql_real_escape_string($myr[0]) ."'
AND year(curdate)='". mysql_real_escape_string($myr[1]) ."'");

Is it possible for PHP to specify a particular future date and time, then echo HTML?

Basically, I want PHP to refer to a specific date and then echo the HTML below into the appropriate table cell on the calendar that corresponds to that date, i.e., specify August 16, 2014 and then echo the event information. You guys are so fast, I wasn't ready for you, ha! :)
I have managed to make a calendar that only uses PHP and CSS and it seems to be working very well. Phew! I also would like to avoid Javascript and bypass the database, altogether, which I have managed to do up to this point.
I can use CSS tooltips to show events and I have them working with IOS. This is my last hurdle: is it possible for PHP to specify a particular date, say August 16, 2014 and then echo the following HTML, having it appear in the date-appropriate table cell?
<div class="has-tooltip">
Event
<span class="tooltip">Concert In The Park<br />
123 City Park Drive<br />
AnyTown, STATE 00000<br />
More Info<br />
or call 555.123.4567<br /></span>
</div>
No other users will be entering information; it's only for my personal use and I can always double-check before publishing.
The only actual hard-coded HTML that appears on my calendar page is <body></body>. Everything else is echoed by PHP, so I can't very well just add the above to an existing table cell.
Thank you for any help! :)
PHP CODE for calendar (without Tags):
$currMonth = isset($_GET['month']) ? $_GET['month'] : date('n');
$currYear = isset($_GET['year']) ? $_GET['year'] : date('Y');
$today = (($currYear == date('Y')) && ($currMonth == date('n'))) ? date('j') : 0;
$prevMonth = $currMonth==1 ? 12 : $currMonth-1;
$nextMonth = $currMonth==12? 1 : $currMonth+1;
$prevYear = $currMonth==1 ? $currYear-1 : $currYear;
$nextYear = $currMonth==12? $currYear+1 : $currYear;
$day1 = mktime(0,0,0,$currMonth,1,$currYear);
$dim = date('t', $day1);
$dayN = mktime(0,0,0,$currMonth,$dim,$currYear);
$dow1 = (date('w',$day1)+0) % 7;
$dowN = (date('w',$dayN)+0) % 7;
$calHead = date('F Y',$day1);
echo <<<EOT
<div class="calwrapper">
<div class="caltitle"><h1>Calendar</h1></div>
<div class="container">
<div class="fnl first"></div>
<div class="adjust"></div>
<div class="fnl last"></div>
</div>
<div class="caldisplay">
<table cellspacing="0">
<tr>
<td class="hd"><a class="cal_button" href="$_SERVER[PHP_SELF]?year=$prevYear&month=$prevMonth"> Prev </a></td>
<td colspan="5" class="adjust">$calHead</td>
<td class="hd"><a class="cal_button" href="$_SERVER[PHP_SELF]?year=$nextYear&month=$nextMonth"> Next </a></td>
</tr>
<tr>
<th class="we">Sun</th>
<th class="wd">Mon</th>
<th class="wd">Tue</th>
<th class="wd">Wed</th>
<th class="wd">Thu</th>
<th class="wd">Fri</th>
<th class="we">Sat</th>
</tr>
<tr>
EOT;
for ($d=0;$d<$dow1;$d++) echo "<td class=\"hd\"> </td>";
$c = $dow1;
for ($d=1; $d<=$dim; $d++, $c++) {
if ($c%7==0) echo "</tr><tr>";
$cl = ($c%7==5) || ($c%7==6) ? 'we' : 'wd';
$st = ($d == $today) ? "style='padding: 0px;'" : '';
echo "<td class=\"$cl\" $st>\n";
echo "$d" ;
echo "</td>\n";
}
while ($c++ % 7 != 0) echo '<td class=\"hd\"> </td>';
echo "</tr></table>\n";
echo '</div></div>';
You can do this in your for...while loop:
if (date('d/m/y') == '16/08/14') {
echo 'Your table cell here';
}
you can find more information about the date function here, http://php.net/manual/en/function.date.php

Categories