Date navigation menu from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neocorps
Forum Newbie
Posts: 1
Joined: Mon Jul 26, 2010 3:28 pm

Date navigation menu from mysql

Post by neocorps »

Hello everyone,

I'm a little over my head here with php, I'm a web designer and (newbie)php developer and I need a Navigation menu that contains years and months depending on mysql data.

This is what I need to get as a result:

Code: Select all

<ul>
            <li>
                <a href="#">2009 (6)</a>
                <ul>
                    <li><a href="">January (1)</a></li>
                    <li><a href="">February (1)</a></li>
                    <li><a href="">April (2)</a></li>
                    <li><a href="">June (2)</a></li>
                </ul>
            </li>
            <li>
                <a class="m2" href="#">2008 (10)</a>
                <ul>
                    <li><a href="">January (2)</a></li>
                    <li><a href="">April (2)</a></li>
                    <li><a href="">July (3)</a></li>
                    <li><a href="">August(1)</a></li>
                    <li><a href="">October(1)</a></li>
                    <li><a href="">December(1)</a></li>
                </ul>
            </li>
            <li><a class="m3" href="#">2007 (0)</a></li>
            <li>
                <a class="m4" href="#">2006 (10)</a>
                <ul>
                    <li><a href="">January (2)</a></li>
                    <li><a href="">April (2)</a></li>
                    <li><a href="">July (3)</a></li>
                    <li><a href="">August(1)</a></li>
                    <li><a href="">October(1)</a></li>
                    <li><a href="">December(1)</a></li>
                </ul>
            </li>
</ul>
Now, I have a mysql table with records that contain dates in this format : Y-m-d (2010-05-21).

How could I create the result with php?

This is the code I have so far:

Code: Select all

//we get the dates from the events of a certain type
	$query = "SELECT date FROM events WHERE type = '{$event_type}' ORDER BY date ASC";
	$result_set = mysql_query($query, $connection);
	confirm_query($result_set);
	
	//Declare the var that contains the menu
	$menu = "<ul>";
	$allyears = array();
	while($data = mysql_fetch_array($result_set)){
		list($year,$month,$day) = explode('-', $data['fecha']);
		if(!in_array($year, $allyears, true)){
			$allyears[] = $year;
			$menu .= "<li>".$year."</li>";
		}
	}
	$menu .= "</ul>";
	echo $menu;
With that I get only the years.. but I'm stuck there.. how could I insert the months where I want, and I also need a way to count the events that occur per month so that I can display the numer beside the month and the year.

I think I could do this by creating an array that contains everything (years, moths and a counter per month).. but as I was saying, I'm over my head with that.

Any Ideas on how to approach this problem?

Thank you!

Fernando G.
Post Reply