Jump to content

[SOLVED] News Navigation


Ne0_Dev

Recommended Posts

Hi Guys,

 

I was just wondering if anyone can help me out on the best way (if possible) to achieve the following:

 

I have a mysql database table that holds news articles with the following fields:

 

id

Title

createdon

details

 

My question is how can I generate an unordered list using the year stored in the "createdon" field as the top level heading (h3) and the title as links to the article details in the <li> elements.  The idea being that when a new article is addded it appears under the correct year and when we move into a new year a new menu is automatically created.  An example of the list structure can be seen below:

 

<h3 class="menuheader expandable">2008</h3>

<ul class="categoryitems">

 

      <li><a href="news.php?article_id=19">Marketing to IT & Telecoms decision makers </a></li>

        <li><a href="news.php?article_id=18">Revised Guide to European Data Laws for Direct Marketing</a></li>

        <li><a href="news.php?article_id=9">Mobilising the Enterprise Report</a></li>

        <li><a href="news.php?article_id=8">New Global Corporations...</a></li>

    </ul>

 

<h3 class="menuheader expandable">2007</h3>

 

<ul class="categoryitems">

    <li><a href="news.php?article_id=15">Business Applications in the UK and EIRE</a></li>

        <li><a href="news.php?article_id=14">Trends of Mobile Devices... </a></li>

        <li><a href="news.php?article_id=13">Decision Makers...</a></li>

        <li><a href="news.php?article_id=12">Mobile & Device Management...</a></li>

        <li><a href="news.php?article_id=10">IT and Telecoms...</a></li>

 

    </ul>

 

I hope this makes sense and any suggestions would be most appreciated.

Link to comment
https://forums.phpfreaks.com/topic/109894-solved-news-navigation/
Share on other sites

This might do the trick I havent tested it but give it a shot

 

<?php

$sql = "SELECT * FROM news ORDER BY year DESC";
$sql_query = mysql_query($sql);

$cur_year = null;

if (mysql_num_rows($sql_query) > 0) {
while ($data = mysql_fetch_array($sql_query)) {
	if ($cur_year === null) {
		// Output the First year
		echo '
		<h3 class="menuheader expandable">'.$data['createdon'].'</h3>
		<ul class="categoryitems">';
	}
	if ($data['createdon'] != $cur_year && $cur_year !== null) {
		// Check to see if THIS row is in the current year..if not close previous year and start new one
		echo '
		</ul>

		<h3 class="menuheader expandable">2008</h3>
		<ul class="categoryitems">';
	}
     echo '<li><a href="news.php?article_id='.$data['id'].'">'.$data['Title'].'</a></li>'."\n";
     $cur_year = $data['createdon'];
}
//close the last year
echo '</ul>';
} else {
echo 'No Rows';
}

?>

Hi Buddski,

 

Many Thanks for your prompt reply it was most appreciated.  I have implemented your code suggestion and am now getting the following error:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\cms\news.php on line 21

No Rows

 

Line 21 is

 

if (mysql_num_rows($sql_query) > 0) {

 

I have had a look myself but can't figure it out.

Hi Buddski,

 

I have added the error code and found that the query was failing at the "ORDER BY year DESC" line.  This was due to the fact that I had the "year" column called "created", so after ammendeding the query it started to run and output the elements in a list.  There is is only one other refinement I need to make and that is to get the articles with the same year to appear under the one heading in <li> tags.

 

Here's the code I have so far:

 

<?php

$sql = "SELECT * FROM tbl_newsarticles ORDER BY created DESC";
$sql_query = mysql_query($sql) or die(mysql_error());

$cur_year = null;

if(mysql_num_rows($sql_query) > 0) {
while ($data = mysql_fetch_array($sql_query)) {
	if ($cur_year === null) {
		// Output the First year
		echo '
		<h3 class="menuheader expandable">'.date('Y', strtotime($data['created'])).'</h3>
		<ul class="categoryitems">';
	}
	if ($data['created'] != $cur_year && $cur_year !== null) {
		// Check to see if THIS row is in the current year..if not close previous year and start new one
		echo '
		</ul>

		<h3 class="menuheader expandable">'.date('Y', strtotime($data['created'])).'</h3>
		<ul class="categoryitems">';
	}
     echo '<li><a href="news.php?article_id='.$data['id'].'">'.$data['title'].'</a></li>'."\n";
     $cur_year = $data['created'];
}
//close the last year
echo '</ul>';
} else {
echo 'No Rows';
}

?>

All done and tested i missed the part were the db `created` field was a datetime not a year value  :P Ooops

<?php
$sql = "SELECT * FROM tbl_newsarticles ORDER BY created DESC";
$sql_query = mysql_query($sql) or die(mysql_error());

$cur_year = null;

if(mysql_num_rows($sql_query) > 0) {
while ($data = mysql_fetch_array($sql_query)) {
	$date_str = date('Y', strtotime($data['created']));
	if ($cur_year === null) {
		// Output the First year
		echo '
		<h3 class="menuheader expandable">'.$date_str.'</h3>
		<ul class="categoryitems">';
	}
	if ($date_str != $cur_year && $cur_year !== null) {
		// Check to see if THIS row is in the current year..if not close previous year and start new one
		echo '
		</ul>

		<h3 class="menuheader expandable">'.$date_str.'</h3>
		<ul class="categoryitems">';
	}
     echo '<li><a href="news.php?article_id='.$data['id'].'">'.$data['title'].'</a></li>'."\n";
     $cur_year = $date_str;
}
//close the last year
echo '</ul>';
} else {
echo 'No Rows';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.