Using this question I was able to build forward/backward functionality based on the current day of the week using a href links. I also wanted to be able to have a a href link for a specific day so you'll notice I added the $link_7 for example and built a Sunday a href. Here is where the crazyness comes in. You'll Notice $link_7 is Specified and is for Sunday but I have to make the actual href a 2 for Sunday. Today is Friday so I think my routine is doing a +2 and throwing off the value. What is the best way to circumvent this when going direct to a Day of the week?
if (array_key_exists('day', $_GET)) {
$add_day = $_GET['day'];
} else {
$add_day = 0;
}
$yesterday = $add_day - 1;
$tomorrow = $add_day + 1;
$link_yesterday = $_SERVER['PHP_SELF'] . '?day=' . $yesterday ;
$link_tomorrow = $_SERVER['PHP_SELF'] . '?day=' . $tomorrow ;
$link_7 = $_SERVER['PHP_SELF'] . '?day=7' ;
$link_1 = $_SERVER['PHP_SELF'] . '?day=1' ;
$link_2 = $_SERVER['PHP_SELF'] . '?day=2' ;
$link_3 = $_SERVER['PHP_SELF'] . '?day=3' ;
$link_4 = $_SERVER['PHP_SELF'] . '?day=4' ;
$link_5 = $_SERVER['PHP_SELF'] . '?day=5' ;
$link_6 = $_SERVER['PHP_SELF'] . '?day=6' ;
$display_day = mktime(0, 0, 0, date("m") , date("d") + $add_day, date("Y"));
$today = date('N', $display_day);
<a style="color:white" href="<?php echo $link_yesterday;?>">See Yesterday</a>
<a style="color:white" href="<?php echo $link_tomorrow;?>">See Tomorrow</a> |
<a style="color:white" href="<?php echo $link_2;?>">Sunday</a>
<a style="color:white" href="<?php echo $link_3;?>">Monday</a>
<a style="color:white" href="<?php echo $link_4;?>">Tuesday</a>
<a style="color:white" href="<?php echo $link_5;?>">Wednesday</a>
<a style="color:white" href="<?php echo $link_6;?>">Thursday</a>
<a style="color:white" href="<?php echo $link_7;?>">Friday</a>
<a style="color:white" href="<?php echo $link_1;?>">Saturday</a>
if( $today == 7) { ?>
<th class="WADAResultsTableHeader">Sunday</th>
<?php } elseif( $today == 1 ) { ?>
<th class="WADAResultsTableHeader">Monday</th>
<?php } elseif( $today == 2 ) { ?>
<th class="WADAResultsTableHeader">Tuesday</th>
<?php } elseif( $today == 3 ) { ?>
<th class="WADAResultsTableHeader">Wednesday</th>
<?php } elseif( $today == 4 ) { ?>
<th class="WADAResultsTableHeader">Thursday</th>
<?php } elseif( $today == 5 ) { ?>
<th class="WADAResultsTableHeader">Friday</th>
<?php } elseif( $today == 6 ) { ?>
<th class="WADAResultsTableHeader">Saturday</th>
<?php } ?>
Related
I want to create a calendar that displays a single month. Users can only see the current month and next month. The calendar has two buttons that allow going to the next month or when the current month. I want to prevent (by PHP) the possibility of going to the other month when you type in the URL number of the month which is too distant or previous than current. When you type the wrong month in the URL the PHP should redirect you to the current or next month.
I noticed that when the user enters the wrong month and when the redirection header should work then there is an error:
"(output started at
C:\xampp\htdocs\php\page--xampp--phpmailer\subpages\profile__dates.php:97)
in C:\xampp\htdocs\php\page--xampp--phpmailer\php\profile_dates.php on
line 15". "profile__dates.php:97"
returns from database base64 profile image. When I change the length src of the image then everything is working very well but I don't see the user's avatar.
How can I fix it without changing the method of saving a user's avatar in the database and in PHP?
$userInfo['profile_image'] - returns over 6k length string, when I short it up to 30 chars then it's working
<?php
session_start();
if(isset($_SESSION['isLoggedIn'])){
if(!$_SESSION['isLoggedIn']){
header("Location: ../index.php");
exit();
}else{
if(isset($_GET['month']) || isset($_GET['year'])){
if(!empty($_GET['month']) || !empty($_GET['year'])){
include('../php/getuserinfo.php');
include('../php/is_profile_activated.php');
include('../php/profile_dates.php');
}
}else{
$month = date('m');
$year = date('Y');
header("Location: http://localhost/php/page--xampp--phpmailer/subpages/profile__dates.php?month=$month&year=$year");
exit();
}
}
}else{
die(header("Location: ./login.php"));
}
?>
<!DOCTYPE html>
<html lang="pl-PL">
<head>
<title>Rezerwacja terminu | Bartek OSK</title>
<meta charset="UTF-8">
<meta name="description" content="Od lat profesjonalnie uczymy naszych kursantów nauki jazdy. Posiadamy bardzo wysoką zdawalność. Posiadamy łączone kursy obniżając przy tym łączną cenę. ">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="../styles/basic.css">
<link rel="stylesheet" href="../styles/profile__dates.css">
<link rel="apple-touch-icon" sizes="180x180" href="../favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../favicons/favicon-16x16.png">
<link rel="manifest" href="../favicons/site.webmanifest">
<link rel="mask-icon" href="../favicons/safari-pinned-tab.svg" color="#5bbad5">
</head>
<body>
<nav>
<div id="navbar">
<div class="nav-logo flexbox-row-center">
<img class="nav-logo-img" src="../imgs/logo.png" alt="BartekOSK logo">
</div>
<div class="nav-links flexbox-row-center">
<ul class="flexbox-row-center">
<li class="nav--elem">Strona Główna</li>
<li class="nav--elem">Cennik</li>
<li class="nav--elem">Galeria</li>
<li class="nav--elem">Kontakt</li>
<?php
if(isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn']){
echo('<li class="nav__account--elem"><a class="nav__account--link" href="../subpages/profile__home.php"><img src="'.$userInfo['profile_image'].'" alt="User profile image"></a>
<ul class="nav__account__dd--list">
<li class="nav__account__dd--elem">Informacje o użytkowniku</li>
<li class="nav__account__dd--elem">Informacje o kursie</li>
<li class="nav__account__dd--elem">Terminy</li>
<li class="nav__account__dd--elem">Wyloguj się</li>
</ul></li>');
}else{
echo '<li class="nav__signup--elem">Zapisz się</li>';
echo '<li class="nav__login--elem">Zaloguj się</li>';
}
?>
</ul>
</div>
</div>
<div class="navigation__rwd">
<button class="hamburger">
<span class="hamburger__box">
<span class="hamburger__inner"></span>
</span>
</button>
<div class="nav-list-wrapper">
<ul class="nav-list">
<li><img class="rwd-nav-logo-img" src="../imgs/logo.png" alt="BartekOSK logo"></li>
<li>Strona Główna</li>
<li>Cennik</li>
<li>Galeria</li>
<li>Kontakt</li>
<?php
if(isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn']){
echo '<li class="rwd__nav__signup--elem">Konto</li>';
echo '<li class="rwd__nav__logout--elem">Wyloguj się</li>';
}else{
echo '<li class="rwd__nav__signup--elem">Zapisz się</li>';
echo '<li class="rwd__nav__login--elem">Zaloguj się</li>';
}
?>
</ul>
</div>
</div>
</nav>
<section id="content">
<h1>REZERWACJA TERMINU</h1>
<div class="home__btn--container">
<img class="home__btn--img" src="../imgs/home_icon.png" alt="Home icon" title="Profil">
</div>
<?php echo generateCalendar(); ?>
<div class="profile--nav flexbox-row-center">
<ul class="nav--list">
<li class="nav__list--item"><img class="nav__list__item--img" src="../imgs/user_icon_small.png" alt="User icon" title="Informacje o użytkowniku"></li>
<li class="nav__list--item"><img class="nav__list__item--img" src="../imgs/student_cap_icon_small.png" alt="Student cap icon" title="Informacje o kursie"></li>
<li class="nav__list--item"><img class="nav__list__item--img active" src="../imgs/calendar_icon_small.png" alt="Calendar icon" title="Terminy"></li>
</ul>
</div>
</section>
<div class="header-shape-one">
<img src="../svgs/shape1.svg" alt="Background Shape1">
</div>
<div class="header-shape-two">
<img src="../svgs/shape2.svg" alt="Background Shape2">
</div>
<footer class="flexbox-column-center">
<div class="footer-contact flexbox-column-center">
<img src="../imgs/logo.png" alt="BartekOSK logo">
<ul>
<li><img src="../imgs/marker_white-32px.png" alt="Marker image">ul. Sokolska 34, 40-121 Katowice</li>
<li><img src="../imgs/mail_white-32px.png" alt="Mail image">bartekosk#gmail.com</li>
<li><img src="../imgs/phone_white-32px.png" alt="Phone image">+48 501-546-408</li>
</ul>
</div>
<div class="important__documents--container">
Regulamin
Polityka prywatności
RODO
</div>
<div class="footer-copyright flexbox-row-center">
<p>© Bartek OSK. Wszelkie prawa zastrzeżone. | Zaprojektowane przez Kamil Krukowski.</p>
</div>
</footer>
<?php
if(!isset($_COOKIE['cookieAgree'])){
echo('<section class="cookies__notification--container">
<p class="cookies__info">Ta strona wykorzystuje pliki <strong>cookies</strong> w celu polepszenia doświadczeń z korzystania naszej strony.</p>
Więcej informacji
<br>
<button class="cookies__agree--btn">Zrozumiałem/am</button>
</section>');
}
?>
<script
src="https://code.jquery.com/jquery-3.5.1.js"
integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
crossorigin="anonymous"></script>
<script src="../js/dates.js"></script>
<script src="../js/script.js"></script>
</body>
</html>
<?php
ob_start();
function generateCalendar(){
//Get basic info about date and calendar
$typeOfCalendar = CAL_GREGORIAN;
$month = $_GET['month'];
$year = $_GET['year'];
$monthName = date('M', mktime(0,0,0,$month,1,$year));
//If user wants to show two months forward or previous month
if(date('m') == 12){
if($month > 1 && $year == date('Y')+1){
$month = 1;
$year = date('Y')+1;
die(header("Location: ../subpages/profile__dates.php?month=$month&year=$year"));
}
}else{
if($month > date('m')+1){
$month = date('m')+1;
}else if($month < date('m')){
$month = date('m');
}
}
$daysInTheMonth = cal_days_in_month($typeOfCalendar, $month, $year);
$firstDayOfTheMonth = date("N", mktime(0,0,0,$month,1,$year));
//Days of previous month
if($month == 1){
$daysInThePrevMonth = cal_days_in_month($typeOfCalendar, 12, $year-1);
$prevMonthDay = $daysInThePrevMonth - $firstDayOfTheMonth+2;
}else{
$prevMonth = $month-1;
$daysInThePrevMonth = cal_days_in_month($typeOfCalendar, $prevMonth, $year);
$prevMonthDay = $daysInThePrevMonth - $firstDayOfTheMonth+2;
}
//Days of forward month
if($month == 12){
$fwdMonthDay = date("j", mktime(0,0,0,1,1,$year+1));
echo($fwdMonthDay);
}else{
$fwdMonth = $month+1;
$fwdMonthDay = date("j", mktime(0,0,0,$fwdMonth,1,$year+1));
echo($fwdMonthDay);
}
//Create table
echo('<table class="calendar--container">');
//Info
echo('<tr>');
echo('<td colspan="2"><button class="calendar__back--btn"><</button></td>');
echo('<td colspan="3"><p class="calendar__month--text">'.$monthName.'</p></td>');
echo('<td colspan="2"><button class="calendar__next--btn">></button></td>');
echo('</tr>');
//Days
echo('<tr>');
echo('<td><p>Pon</p></td>');
echo('<td><p>Wt</p></td>');
echo('<td><p>Śro</p></td>');
echo('<td><p>Czw</p></td>');
echo('<td><p>Pią</p></td>');
echo('<td><p>Sob</p></td>');
echo('<td><p>Nie</p></td>');
echo('</tr>');
$dateInLoop = 0;
for($i = 1; $i<=6; $i++){
echo("<tr>");
for($j = 1; $j<=7; $j++){
if($i === 1 && ($j === $firstDayOfTheMonth-1)){
echo("<td><p class='other__month__day--text'>$prevMonthDay</p></td>");
$dateInLoop = 1;
}else if($i === 1 && ($j >= $firstDayOfTheMonth)){
echo("<td data-day='$dateInLoop'><button class='calendar__day--btn'>$dateInLoop</button></td>");
$dateInLoop++;
}else if($i === 1 && ($j < $firstDayOfTheMonth)){
echo("<td><p class='other__month__day--text'>$prevMonthDay</p></td>");
$dateInLoop++;
$prevMonthDay++;
}else if($i != 1 && ($dateInLoop <= $daysInTheMonth)){
echo("<td data-day='$dateInLoop'><button class='calendar__day--btn'>$dateInLoop</button></td>");
$dateInLoop++;
}else if($i != 1 && ($dateInLoop >= $daysInTheMonth)){
echo("<td><p class='other__month__day--text'>$fwdMonthDay</p></td>");
$dateInLoop++;
$fwdMonthDay++;
}
}
echo("</tr>");
}
echo("</table>");
}
ob_end_flush();
?>
As per the comment I made, what you could try would be to get the generateCalendar method to return the HTML string rather than output it directly and then call that function earlier than you do currently.
For example:
function generateCalendar(){
if( isset( $_GET['month'], $_GET['year'] ) ){
$html=array();
$month = $_GET['month'];
$year = $_GET['year'];
if( !is_numeric( $month ) or !is_numeric( $year ) ){
return false;
}
$typeOfCalendar = CAL_GREGORIAN;
$monthName = date('M', mktime(0,0,0,$month,1,$year));
//If user wants to show two months forward or previous month
if( date('m') == 12 ){
if( $month > 1 && $year == date('Y')+1 ){
$month = 1;
$year = date('Y')+1;
die( header( "Location: ../subpages/profile__dates.php?month=$month&year=$year" ) );
}
}else{
if($month > date('m')+1){
$month = date('m')+1;
}else if($month < date('m')){
$month = date('m');
}
}
$daysInTheMonth = cal_days_in_month($typeOfCalendar, $month, $year);
$firstDayOfTheMonth = date("N", mktime(0,0,0,$month,1,$year));
//Days of previous month
if($month == 1){
$daysInThePrevMonth = cal_days_in_month($typeOfCalendar, 12, $year-1);
$prevMonthDay = $daysInThePrevMonth - $firstDayOfTheMonth+2;
}else{
$prevMonth = $month-1;
$daysInThePrevMonth = cal_days_in_month($typeOfCalendar, $prevMonth, $year);
$prevMonthDay = $daysInThePrevMonth - $firstDayOfTheMonth+2;
}
//Days of forward month
if( $month == 12 ){
$fwdMonthDay = date("j", mktime(0,0,0,1,1,$year+1));
$html[]=$fwdMonthDay;
}else{
$fwdMonth = $month+1;
$fwdMonthDay = date("j", mktime(0,0,0,$fwdMonth,1,$year+1));
$html[]=$fwdMonthDay;
}
//Create table
$html[]='
<table class="calendar--container">
<tr>
<td colspan="2"><button class="calendar__back--btn"><</button></td>
<td colspan="3"><p class="calendar__month--text">'.$monthName.'</p></td>
<td colspan="2"><button class="calendar__next--btn">></button></td>
</tr>
<tr>
<td><p>Pon</p></td>
<td><p>Wt</p></td>
<td><p>Śro</p></td>
<td><p>Czw</p></td>
<td><p>Pią</p></td>
<td><p>Sob</p></td>
<td><p>Nie</p></td>
</tr>');
$dateInLoop = 0;
for($i = 1; $i<=6; $i++){
$html[]='<tr>;
for( $j = 1; $j<=7; $j++ ){
if( $i === 1 && ($j === $firstDayOfTheMonth-1) ){
$html[]="<td><p class='other__month__day--text'>$prevMonthDay</p></td>";
$dateInLoop = 1;
}else if( $i === 1 && ($j >= $firstDayOfTheMonth) ){
$html[]="<td data-day='$dateInLoop'><button class='calendar__day--btn'>$dateInLoop</button></td>";
$dateInLoop++;
}else if($i === 1 && ($j < $firstDayOfTheMonth)){
$html[]="<td><p class='other__month__day--text'>$prevMonthDay</p></td>";
$dateInLoop++;
$prevMonthDay++;
}else if( $i != 1 && ($dateInLoop <= $daysInTheMonth) ){
$html[]="<td data-day='$dateInLoop'><button class='calendar__day--btn'>$dateInLoop</button></td>";
$dateInLoop++;
}else if( $i != 1 && ($dateInLoop >= $daysInTheMonth) ){
$html[]="<td><p class='other__month__day--text'>$fwdMonthDay</p></td>";
$dateInLoop++;
$fwdMonthDay++;
}
}
$html[]="</tr>";
}
$html[]="</table>";
return implode( PHP_EOL, $html );
}
return false;
}
And then, in the main page:
<?php
session_start();
if(isset($_SESSION['isLoggedIn'])){
if(!$_SESSION['isLoggedIn']){
exit(header("Location: ../index.php"));
}else{
if(isset($_GET['month']) || isset($_GET['year'])){
if(!empty($_GET['month']) || !empty($_GET['year'])){
include('../php/getuserinfo.php');
include('../php/is_profile_activated.php');
include('../php/profile_dates.php');
#call the calendar that now returns HTML string
$calhtml=( function_exists('generateCalendar') ) ? generateCalendar() : false;
}
}else{
$month = date('m');
$year = date('Y');
exit( header( "Location: http://localhost/php/page--xampp--phpmailer/subpages/profile__dates.php?month=$month&year=$year)" );
}
}
}else{
die(header("Location: ./login.php"));
}
?>
Later, in that same page ( where the generateCalendar method was originally called )
<?php
# echo the calendar data
if( $calhtml ) echo $calhtml;
?>
how can i change the first day in the first column of weeks into Monday.
here's the code i got from a website, and I've been trying to change the position of the days but still it displays the first day of the week as Sunday. how can i change it to Monday, thanks in advance
if(isset($_POST['func']) && !empty($_POST['func'])){
switch($_POST['func']){
case 'getCalender':
getCalender($_POST['year'],$_POST['month']);
break;
case 'getEvents':
getEvents($_POST['date']);
break;
default:
break;
}
}
/*
* Get calendar full HTML
*/
function getCalender($year = '',$month = '')
{
$dateYear = ($year != '')?$year:date("Y");
$dateMonth = ($month != '')?$month:date("m");
$date = $dateYear.'-'.$dateMonth.'-01';
$currentMonthFirstDay = date("N",strtotime($date));
$totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);
$totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);
$boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;
?>
<div id="calender_section">
<h2>
<<
<select name="month_dropdown" class="month_dropdown dropdown"><?php echo getAllMonths($dateMonth); ?></select>
<select name="year_dropdown" class="year_dropdown dropdown"><?php echo getYearList($dateYear); ?></select>
>>
</h2>
<div id="event_list" class="none"></div>
<div id="calender_section_top">
<ul>
<li>Po</li>
<li>Út</li>
<li>St</li>
<li>Čt</li>
<li>Pá</li>
<li>So</li>
<li>Ne</li>
</ul>
</div>
<div id="calender_section_bot">
<ul>
<?php
$dayCount = 1;
for($cb=1;$cb<=$boxDisplay;$cb++){
if(($cb >= $currentMonthFirstDay+0 || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay)){
//Current date
$currentDate = $dateYear.'-'.$dateMonth.'-'.$dayCount;
$eventNum = 0;
//Include db configuration file
include 'dbConfig.php';
//Get number of events based on the current date
$result = $db->query("SELECT title FROM events WHERE date = '".$currentDate."' AND status = 1");
$eventNum = $result->num_rows;
//Define date cell color
if(strtotime($currentDate) == strtotime(date("Y-m-d"))){
echo '<li date="'.$currentDate.'" class="grey date_cell">';
}elseif($eventNum > 0){
echo '<li date="'.$currentDate.'" class="light_sky date_cell">';
}else{
echo '<li date="'.$currentDate.'" class="date_cell">';
}
//Date cell
echo '<span>';
echo $dayCount;
echo '</span>';
//Hover event popup
echo '<div id="date_popup_'.$currentDate.'" class="date_popup_wrap none">';
echo '<div class="date_window">';
echo '<div class="popup_event">Events ('.$eventNum.')</div>';
echo ($eventNum > 0)?'view events':'';
echo '</div></div>';
echo '</li>';
$dayCount++;
?>
<?php }else{ ?>
<li><span> </span></li>
<?php } } ?>
Hi i have a code that displays title, date and location. I want it to put on a div, unfortunately there is some wrong in my code and it loops my div.
I just want all may data to be inside "may-container" i just don't know how to create this container.
Hope you could help me with this. Thanks
<div class="may-container">
<div class="May">
title
date
location
</div>
<div class="May">
title
date
location
</div>
</div>
Here is my code
$counter = 0;
while ( $startdate <= $enddate) {
if ( date("F",strtotime($result['date'])) == "May" && date("m, Y",strtotime($result['date'])) >= date("m, Y") )
{
if ($counter < 1)
{
echo "<div class='May-container'>";
$counter++;
}
echo "<div class= 'May'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
}
$startdate = strtotime("+120 day", $startdate);
}
if your code date("F",strtotime($result['date'])) produces a month in words then why do you have to put if and else?
why not:
while ( $startdate <= $enddate) {
echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
$startdate = strtotime("+120 day", $startdate);
}
EDIT: to answer your comment, you can try this code:
This code is only applicable if your Data is sorted out by date
$last_month = '';
$is_first = true;
while ( $startdate <= $enddate) {
if($last_month != date('F',strtotime($result['date']))){
echo '</div>';
$is_first = true;
}
if($is_first){
$last_month = date('F',strtotime($result['date']));
echo "<div class='". strtolower($last_month) . "-container'>";
$is_first = false;
}
echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
$startdate = strtotime("+120 day", $startdate);
}
if the code above code runs as what i expected(sorry i didnt run it for i dont have enough time to) it will yields something like:
<div class="may-container">
<div class="May">
title
date
location
</div>
<div class="May">
title
date
location
</div>
</div>
<div class="june-container">
<div class="June">
title
date
location
</div>
<div class="June">
title
date
location
</div>
</div>
I'm using joomla with K2 and I want to order news in The archive module first by year. When you click on The year it Needs to show all articles from that year. Bit The module also Needs to expand The months to filter deeper
How can I modify the module to act like This?
Greets
So this is what I mean
2012
- Januari
- Februari
- March
- etc etc
2013
- Januari
- Februari
- March
- etc etc.
Both year and month needs to be filterable
How can I modify the module to act like This?
Greets
So this is what I mean
List item
2012
Januari
Februari
March
etc etc
2013
Januari
Februari
March
etc etc.
Both year and month needs to be filterable
How can I modify the module to act like This?
Greets
<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2ArchivesBlock<?php if($params->get('moduleclass_sfx')) echo ' '.$params->get('moduleclass_sfx'); ?>">
<ul>
<?php foreach ($months as $month): ?>
<li>
<?php if ($params->get('archiveCategory', 0) > 0): ?>
<a href="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=date&month='.$month->m.'&year='.$month->y.'&catid='.$params->get('archiveCategory')); ?>">
<?php echo $month->name.' '.$month->y; ?>
<?php if ($params->get('archiveItemsCounter')) echo '('.$month->numOfItems.')'; ?>
</a>
<?php else: ?>
<a href="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=date&month='.$month->m.'&year='.$month->y); ?>">
<?php echo $month->name.' '.$month->y; ?>
<?php if ($params->get('archiveItemsCounter')) echo '('.$month->numOfItems.')'; ?>
</a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
The query is from another file
public static function getArchive(&$params)
{
$mainframe = JFactory::getApplication();
$user = JFactory::getUser();
$aid = (int)$user->get('aid');
$db = JFactory::getDBO();
$jnow = JFactory::getDate();
$now = K2_JVERSION == '15' ? $jnow->toMySQL() : $jnow->toSql();
$nullDate = $db->getNullDate();
$query = "SELECT DISTINCT MONTH(created) as m, YEAR(created) as y FROM #__k2_items WHERE published=1 AND ( publish_up = ".$db->Quote($nullDate)." OR publish_up <= ".$db->Quote($now)." ) AND ( publish_down = ".$db->Quote($nullDate)." OR publish_down >= ".$db->Quote($now)." ) AND trash=0";
if (K2_JVERSION != '15')
{
$query .= " AND access IN(".implode(',', $user->getAuthorisedViewLevels()).") ";
if ($mainframe->getLanguageFilter())
{
$languageTag = JFactory::getLanguage()->getTag();
$query .= " AND language IN (".$db->Quote($languageTag).", ".$db->Quote('*').") ";
}
}
else
{
$query .= " AND access<={$aid} ";
}
$catid = $params->get('archiveCategory', 0);
if ($catid > 0)
$query .= " AND catid=".(int)$catid;
$query .= " ORDER BY created DESC";
$db->setQuery($query);
$rows = $db->loadObjectList();
$months = array(JText::_('K2_JANUARY'), JText::_('K2_FEBRUARY'), JText::_('K2_MARCH'), JText::_('K2_APRIL'), JText::_('K2_MAY'), JText::_('K2_JUNE'), JText::_('K2_JULY'), JText::_('K2_AUGUST'), JText::_('K2_SEPTEMBER'), JText::_('K2_OCTOBER'), JText::_('K2_NOVEMBER'), JText::_('K2_DECEMBER'), );
if (count($rows))
{
foreach ($rows as $row)
{
if ($params->get('archiveItemsCounter'))
{
$row->numOfItems = modK2ToolsHelper::countArchiveItems($row->m, $row->y, $catid);
}
else
{
$row->numOfItems = '';
}
$row->name = $months[($row->m) - 1];
$archives[] = $row;
}
return $archives;
}
}
This solved my issue:
<div id="k2ModuleBox<?php echo $module->id; ?>" class="k2ArchivesBlock<?php if($params->get('moduleclass_sfx')) echo ' '.$params->get('moduleclass_sfx'); ?>">
<?php
foreach ($months as $month) {
$years[$month->y][] = $month;
}
?>
<?php foreach ($years as $year => $months) : ?>
<h4><?php echo $year; ?></h4>
<ul>
<?php foreach ($months as $month): ?>
<li>
<?php if ($params->get('archiveCategory', 0) > 0): ?>
<a href="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=date&month='.$month->m.'&year='.$month->y.'&catid='.$params->get('archiveCategory')); ?>">
<?php echo $month->name.' '.$month->y; ?>
<?php if ($params->get('archiveItemsCounter')) echo '('.$month->numOfItems.')'; ?>
</a>
<?php else: ?>
<a href="<?php echo JRoute::_('index.php?option=com_k2&view=itemlist&task=date&month='.$month->m.'&year='.$month->y); ?>">
<?php echo $month->name.' '.$month->y; ?>
<?php if ($params->get('archiveItemsCounter')) echo '('.$month->numOfItems.')'; ?>
</a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div>
In the code below, I'm pulling all upcoming training classes from my database. I'm checking to see if the endDate has passed and I'm also checking if the status !='2'
I want this to return the 4 most recent results. It works fine until statusdoes =2. I understand that the loop is technically running 4 times, but only displaying the results with status !='2'
How can I change this so that if status = '2' the loop will continue until it finds 4 results that meet the criteria?
<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($count <= 4 && $row = $sth->fetch()) {
if($row['endDate'] > $today && $row['status'] != '2') {?>
<li>
<img class="post_thumb" src="/images/img.jpg" alt="" />
<div class="post_description">
<small class="details">
<?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
</small>
<a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
</div>
</li>
<?php }
$count++;
}
?>
You must put the $count++ inside the if loop, otherwise it will always get incremented.
As in:
<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($count <= 4 && $row = $sth->fetch()) {
if($row['endDate'] > $today && $row['status'] != '2') {?>
<li>
<img class="post_thumb" src="/images/img.jpg" alt="" />
<div class="post_description">
<small class="details">
<?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
</small>
<a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
</div>
</li>
<?php
$count++;
}
}
?>
You can break out out of the loop, if its four
while($row = $sth->fetch()) {
....
if($row['status']=='2' && $count >="4") break;
$count++;
}