need help with loops, date, mySQL for archive display

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
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

need help with loops, date, mySQL for archive display

Post by vincenzobar »

Hi,

I have a page that queries MySQL and gets all news articles and then will auto generate a page that will show recent articles and then pr the previous month back i need it to place relevant articles in the respective months, like an archive.

ex:
recent articles
article 50
article 49

may 2006
article 48
article 47

etc..

I use another function to get recent articles so this function i am about to show starts at -1 for months. My for loop is not doing what i want it to. basically it only displays the last month and will not output more than one month back.

Code: Select all

function article_toc(){
$cur_year  = date('Y');
$cur_month = date('m');
$cur_day  = date('d');
$pattern = '/^(\d{1,4})-(\d{1,2})-(\d{1,2})\s(\d*):(\d{2}):(\d{2})$/';

$content = '<div class="catalog_page_content_row"><p class="accessory_details">';

//get information from database
$sql = "SELECT title, link, date_created FROM blog";
$rs= mysql_query($sql) or die("Error on Line 198: ".mysql_error());	
// date("F")-1 does not work so we create an array and use date("m")-1 to get value from array to display current month
		$months = array(01=>'January', 02=>'February', 03=>'March', 04=>'April', 05=>'May', 06=>'June', 07=>'July', 08=>'August', 09=>'September', 10=>'October', 11=>'November', 12=>'December');
	
	for($m=1;$m<=12; $m++){
		$past = date("m") - $m;
			
		while($article_rs = mysql_fetch_assoc($rs)){
	// get the month from database timestamp
	$article_month = preg_replace($pattern,'$2' , $article_rs['date_created']);
			
			if($article_month == $past){
				$past = $months[$past];
				$content .= '<span class="cart_item_ul2"><b>'.$past.' '.date('Y').'</b></span><br />';
				//$content .= '<span class="cart_item_ul2"><b>Current Articles</b></span><br />';
				
				$content .= '<a href ="'.$article_rs['link'].'">'.$article_rs['title'].'</a><br />'. $m;

			} // if
		
		} // while

	} // for

$content .= '</p>
			<p>&nbsp;</p>
			<p>&nbsp;</p>
			<p>&nbsp;</p>
			<p>&nbsp;</p>
			<p>&nbsp;</p>
			<p>&nbsp;</p>
			<p>&nbsp;</p>
			<p>&nbsp;</p>
	</div>';
echo $content;
		
}//function
I can't for the life of me figure out why the iteration of $m will not work and only iterates once.

Any help would be great!!!!
Last edited by vincenzobar on Thu May 04, 2006 9:13 am, edited 1 time in total.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

ack...put

Code: Select all

tags around that please: -D
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

I did!!! maybe i disabled something rechecking it now!


edit...
i disabled BBCode... oops !
Post Reply