Solved

Display all years in archive query

Posted on 2011-09-13
7
186 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 110

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 110

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 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