Solved

Display all years in archive query

Posted on 2011-09-13
7
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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