Jump to content

Sort by Date Jan-Feb, March-Apr,May-Jun etc


jarvis

Recommended Posts

Hi all,

 

I've got the following script which lists all the articles by month order for a given year. This works fine. However, it simply lists by month order Aug, Jul, Jun, may etc for a given year. What I need to do is display 2 monthly ie:

jan - feb

mar - apr

may - jun

jul - aug

etc etc

 

My script is:

<?php 

// This page displays all articles within a specified category

$page_title = 'View Featured News Articles';
require('includes/header.html');

require_once('../mysql_connect.php'); // Connect to the db

// Set the category id
$id="18";

// Set the sorting order by months
if (isset($_GET['year'])) {
// $month will be appended to the links
$year = $_GET['year'];
} else { // Set a default sorting month to current month
$year= date('Y'); 
}

$query="
SELECT articles.article_id, articles.title, LEFT(articles.description, 50) AS abbrev, articles.status, article_associations.article_id, article_categories.category, DATE_FORMAT(articles.date_entered,'%M') as month,DATE_FORMAT(articles.date_entered,'%Y') as year, DATE_FORMAT(articles.date_entered,'%d %M %Y') as date 
FROM `articles` 
INNER JOIN (article_categories 
		INNER JOIN article_associations ON article_categories.article_category_id = article_associations.article_category_id) ON articles.article_id = article_associations.article_id
WHERE article_associations.article_category_id=$id
	 AND DATE_FORMAT(articles.date_entered, '%Y') = '$year' 
	 AND status='1' 
";

$result = @mysql_query ($query); // Run the query.
$num = mysql_num_rows ($result); // How many users are there?

if ($num > 0) { // If it ran OK, display the records.

echo "<h1>There is currently $num new articles for the month of $order_by</h1>";
echo '<table cellpadding="3" cellspacing="3" border="0" align="center" width="100%">';
echo '<tr>
<td><p><b>Title:</b></p></td>
<td><p><b>Description:</b></p></td>
<td> </td>
<td> </td>
</tr>';
// Fetch and print all the records.
$bg = '#CCCCCC'; // Set the background color.

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
	$category = '' .$row['5']. '';
	$bg = ($bg=='#CCCCCC' ? '#FFFFFF' : '#CCCCCC'); // Switch the background color.

		echo '<tr bgcolor="' . $bg . '">
			<td>' .$row['1']. '</td>
		  	<td>';
			   $extract = $row['2'];
			   // find position of last space in extract
			   $lastSpace = strrpos($extract, ' ');
			   // use $lastSpace to set length of new extract and add ...
			   echo substr($extract, 0, $lastSpace).'... ';		
		echo '</td>';
		echo '<td>'.$row['6'].'</td>';
		echo "<td><a href=\"articles.php?aid={$row['0']}\">Read More...</a></td>";
		echo '</tr>';	
}
echo '</table>';

echo '<h1 style="text-align: right; color: #AE0026;">'.$category.'</h1>';
echo '<p> </p>';


} else { // Not records in related to that category ID.
echo '<p class="error">There are currently no news articles!</p>';
}

$startYear = "1 january 2006";	//get the starting year
function printYears($var)
{
$id="18";	//include the category id for the links	
$start = strtotime($var);	//timestamp of the entered date
$now = strtotime("Now");	//timestamp of now so it does not write anything in the future	
	while ($now > $start)	//while the start is less than now
	{
		echo '<a href="news_archive_annually.php?s=&id=' . $id . '&year=' . date("Y", $now) .'">'.date(" Y", $now).'</a>';
		echo " | ";
		$now = strtotime("-1 year", $now);	// subtract a month from the start timestamp
	}			
}	
printYears($startYear);	//execute the function	


mysql_close(); // Close the database connection.

require('includes/footer.html');
?>

I have no idea how to do this so any help is very much appreciated!

 

Thanks in advanced

Hi  suresh64633,

 

Currently I've a database with articles. Each article can be assigned to a particular category. When you click a category, it shows the articles relevant to it. This works fine.

 

At present, the articles list in reverse month order, starting with the current month. So:

August

July

June

etc etc

 

What I'd like to do, as the articles are for every 2 months, I'd like to list them like so:

July - August

May - June

March - April

January - February

 

So the articles which currently display under August and under July will display under the one heading of July - August

and so on.

 

Does this help?

 

Thanks in advanced

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.