Solved

Display all years in archive query

Posted on 2011-09-13
7
185 Views
Last Modified: 2012-05-12
I am trying to display a list of events by year (from the database image attached) as a dropdown menu.

Here's the test page..http://www.anjoman.co.uk/previous-events-test2.php

It's pretty much there using this query code...

<?php
$curyear = date('Y');
$dbhandle = new mysqli('localhost', 'anjomanc_bright', 'letitbe', 'london');
$result = "SELECT * FROM events WHERE eventdate like '$curyear%' ORDER BY eventdate DESC";

while (mysqli_num_rows($result) > 0 || $curyear == date('Y')) {
  echo "<b>".$curyear."</b></br>";
  while ($row = $result->fetch_object()) {
    echo "&nbsp;&nbsp;".$row->eventdate."&nbsp;".$row->title."</br>";
  }

  $curyear = $curyear - 1;
  $result = "SELECT * FROM events WHERE eventdate like '$curyear%' ORDER BY eventdate DESC";
}
$dbhandle->close();

?>

The problem is that is displays years when there are no events, is there a way to only show the year if there is an event to show?

 database
0
Comment
Question by:BrighteyesDesign
  • 3
  • 2
  • 2
7 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36529603
Yes, I think so.  After the second query that says this:

$result = "SELECT * FROM events WHERE eventdate like '$curyear%' ORDER BY eventdate DESC";

Get the number of rows in $result and if that number is zero, use continue;
0
 

Author Comment

by:BrighteyesDesign
ID: 36529680
Thanks Ray, I'm not sure how to write that code, could you help?

The code in the question was put together by another expert not me!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36529738
Maybe something like this... I do not have your data base so I cannot test the code, sorry.
<?php
$curyear = date('Y');
$dbhandle = new mysqli('localhost', 'anjomanc_bright', 'letitbe', 'london');
$result = mysqli_query("SELECT * FROM events WHERE eventdate like '$curyear%' ORDER BY eventdate DESC");

while (mysqli_num_rows($result) > 0 || $curyear == date('Y')) {
  echo "<b>".$curyear."</b></br>";
  while ($row = $result->fetch_object()) {
    echo "&nbsp;&nbsp;".$row->eventdate."&nbsp;".$row->title."</br>";
  }

  $curyear = $curyear - 1;
  $result = mysqli_query("SELECT * FROM events WHERE eventdate like '$curyear%' ORDER BY eventdate DESC");
  if (mysqli_num_rows($result) == 0) continue;
}
$dbhandle->close();

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 82

Expert Comment

by:hielo
ID: 36530159
Try the attached code:
<?php
$dbhandle = new mysqli('localhost', 'anjomanc_bright', 'letitbe', 'london');
$result = "SELECT e.*, DATE_FORMAT(`eventdate`,'%Y') as y FROM `events` e WHERE (`title` IS NOT NULL) AND Trim(`title`)!='' ORDER BY `eventdate` DESC";

$curyear = null;
while ($row = $result->fetch_object())
{
	if( $curyear!=$row->y )
	{
		$curyear=$row->y;
		echo '<b>', $curyear, '</b><br />';
	}
        echo '&nbsp;&nbsp;', $row->eventdate, '&nbsp;', $row->title, '<br />';
}
$dbhandle->close();
?>

Open in new window

0
 

Author Comment

by:BrighteyesDesign
ID: 36530427
Thanks for that, both queries return blank pages though.

Hielo's code is live here  http://www.anjoman.co.uk/previous-events-test2.php

The original results are here...http://www.anjoman.co.uk/previous-events-test3.php
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36530518
on my post (AND on YOUR original post cannot possible be working because it too has the same problem on that line) the third line should be:
$result = $dbhandle->query( ... );
<?php
$dbhandle = new mysqli('localhost', 'anjomanc_bright', 'letitbe', 'london');
$result = $dbhandle->query("SELECT e.*, DATE_FORMAT(`eventdate`,'%Y') as y FROM `events` e WHERE (`title` IS NOT NULL) AND Trim(`title`)!='' ORDER BY `eventdate` DESC");

$curyear = null;
while ($row = $result->fetch_object())
{
	if( $curyear!=$row->y )
	{
		$curyear=$row->y;
		echo '<b>', $curyear, '</b><br />';
	}
        echo '&nbsp;&nbsp;', $row->eventdate, '&nbsp;', $row->title, '<br />';
}
$dbhandle->close();
?>

Open in new window

0
 

Author Closing Comment

by:BrighteyesDesign
ID: 36530878
Thanks a lot for that!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question