Solved

Display all years in archive query

Posted on 2011-09-13
7
183 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 108

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 108

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now