Jump to content

[SOLVED] Archiving News


stublackett

Recommended Posts

Hi,

 

I'm working on http://www.berwickrangers.net/news.php As you can see the news list is rather huuuuuuuuuge. What I'm looking to do is archive the news, Possibly week by week. How could I do this? Any tutorials that you may be aware of out there.

 

Probably looking to do it as a drop down too.

 

Any help appreciated. Cheers

Link to comment
Share on other sites

Hi stublackett,

 

You could have a drop-down box and match by month, for example:

 

<select name="newsmonth">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
</select>

 

Then, get the selected month by using:

 

$newsmonth = $_POST['newsmonth'];

 

And then you could just perform a MySQL query matching the $newsmonth to the required month in the database, obviously you would need to change the option values to match the method you used to store the month in the database.

 

Something like this should work.

 

Hope this helps.

Link to comment
Share on other sites

  • 1 month later...

Just started to look at this.. Been damn busy.

 

I've got a select menu set up in HTML Like so :

 

<form action="news.php" method="POST">
                    <select name="newsmonth">
                      <option value="" selected="selected"> </option>
                      <option value="01">January</option>
                      <option value="02">February</option>
                      <option value="03">March</option>
                      <option value="04">April</option>
                      <option value="05">May</option>
                      <option value="06">June</option>
                      <option value="07">July</option>
                      <option value="08">August</option>
                      <option value="09">September</option>
                      <option value="10">October</option>
                      <option value="11">November</option>
                      <option value="12">December</option>
                    </select>
                    <input type="submit" name="go" value="Go" />
                    </form>  

 

I just need to sort the SQL selection out. Its finding the month to match which is proving difficult.

 

The date string is currently echo'ed out as DD/MM/YY

 

So my PHP Code (As I think it should look is as follows :

 

<?php
$newsmonth = $_POST['newsmonth'];
$result = mysql_query("SELECT *, DATE_FORMAT( date, '%m' )AS uk_date FROM $db_table WHERE uk_date = $newsmonth ORDER BY nid DESC");
?>

 

But its not showing anything... I suspect its something to do with the %m can anyone help?

 

Cheers

Link to comment
Share on other sites

SELECT * FROM $db_table WHERE MONTH(date) = $newsmonth ORDER BY nid DESC;

 

You'll get a problem once your site has been online for a year though. When you select January you would get news items from January 2009 and January 2010 (and January 2011 later on).

 

What I would do is to list them in reverse chronological order, but introduce pagination so you can do like "Earlier entires". We have a pagination tutorial on PHP Freaks.

Link to comment
Share on other sites

I'll have a look at the pagination stuff, Cheers. Was thinking it could be very tricky once the year comes round. Ideally I'd like the news from season to season, Which may be difficult.

 

Still having difficulty tho......

 

That seems to work in PHP My Admin, Just not in the actual page.

 

What I need is an IF Statement that shows ALL news items, Unless the Archive Form has been submitted..

 

My script is as follows

 

          <?php 
	  	if (isset($_POST['posted'])) {
			$newsmonth = $_POST['newsmonth'];
			$result = mysql_query("SELECT * FROM $db_table WHERE MONTH(date) = $newsmonth ORDER BY nid DESC");
			while($myrow = mysql_fetch_assoc($result))
        {//begin of loop
		$nid = $myrow['nid'];
		$headline = $myrow['headline'];
		$date = $myrow['uk_date'];
// Show Pulled Through News Items
	echo "<ul>
    	    	<li>
				<h2><span class='newsdate'>($date)</span> <a href='newsitem.php?nid=$nid' title='$headline'>$headline</a> </h2>
			</li>
     	</ul>";  
	} 
	} else {
	include("includes/news-select.php"); 
	}//End While loop		 	 									
?>

Link to comment
Share on other sites

Hi Stuart,

 

Your script is relying on $_POST['posted'] to be set before performing the query.  Is that set?  Post your HTML to confirm.

 

Also, your else statement is just including "news-select.php" and doing nothing else.

 

Change your code to read:

 

          <?php 
	  	if ($_SERVER['REQUEST_METHOD']=='POST') 
		{
			$newsmonth = $_POST['newsmonth'];
			$result = mysql_query("SELECT * FROM $db_table WHERE MONTH(date) = $newsmonth ORDER BY nid DESC");
		}
		else
		{
			$result = mysql_query("SELECT * FROM $db_table ORDER BY nid DESC");
			include("includes/news-select.php"); 
		}		
		while($myrow = mysql_fetch_assoc($result))
        	{//begin of loop
		$nid = $myrow['nid'];
		$headline = $myrow['headline'];
		$date = $myrow['uk_date'];
		// Show Pulled Through News Items
		echo "<ul>
    	    		<li>
				<h2><span class='newsdate'>($date)</span> <a href='newsitem.php?nid=$nid' title='$headline'>$headline</a> </h2>
				</li>
     		</ul>";  
		} 							
?>

 

Hope this helps.

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.