PDA

View Full Version : Displaying article


tony09uk
04-27-2012, 07:53 PM
I need to display each month an article was posted e.g

APRIL

MARCH

FEBRUARY

DECEMBER

OCTOBER

etc....

I want to display the twelve previous months that articles have been posted but only want each to be displayed once, no matter how many times articles have been posted that month (the user will click on the month to view the articles from that month).

I have created this:

$months = mysql_query("SELECT article_id, content, DATE_FORMAT(date, '%M')
AS date
FROM article
ORDER BY article_id
DESC
LIMIT 12");


while($row = mysql_fetch_array($months)){
echo $row['date'] . "<br/>";
}

trouble is with the above code, if 12 articles were posted in April then April would show 12 times, how do I stop this from happening?

edbr
04-28-2012, 02:53 AM
read this http://www.dreamincode.net/forums/topic/9493-use-include-to-display-xls-spreadsheet/

tony09uk
04-29-2012, 04:41 PM
I didn't really get much from that post, but thanks for the link.

I have now got it sorted, for anyone else suffering a similar issue use

SELECT DISTINCT MONTH(date) FROM article ...

edbr
04-30-2012, 02:37 AM
that was meant for another post about xls sheets, i have no idea why i put it here. In my defenc im old and i dribble when bored :)

tony09uk
05-06-2012, 10:44 PM
I am currently trying to select all the articles from a specific month.

I.e. select January and all six posts from that month will be displayed etc...

I have tried a few ways out but all with seemingly no success. How is it done?

edbr
05-07-2012, 02:50 AM
depends but in some layouts you could use

$date="january"; ///or $date=$_post['month'];
where date =$month

tony09uk
05-07-2012, 12:50 PM
Ok, in the address bar it says pagename?id=january. So does php january febuary etc as months or does it see it as just another string? I think i have tried WHERE id=month

edbr
05-08-2012, 01:57 AM
so its a $_GET['pagename'] value.
echo out your values to check what you have. id is unlikely to be the month is it.

tony09uk
05-08-2012, 09:05 AM
in the address bar it comes up as id=january when i click on the january option.

However i have been wondering can i break up the date. It is set as day-month-year. Is this saved as an array? and if so can i break it up and use them that way?

edbr
05-08-2012, 09:24 AM
yes sorry rushing again, $_GET['id] you could explode using "-"as separator, or download the timestamp and format then.
read this http://www.tizag.com/phpT/phpdate.php

tony09uk
05-09-2012, 01:09 AM
right, I give up :(

I have used explode with no success. Could you give me an example of how you do it please.....im tearing my hair out.

below is the code to display the months and the link I created for the user to click on

$months = mysql_query("SELECT DISTINCT(MONTHNAME(date)) as post_month
FROM article
LIMIT 12");


while($row = mysql_fetch_array($months)){
echo "<a href=\"montharticles.php?id=" . $row['0'] . "\">" . $row['0'] . "</a><br/>";
}

below is the code for the second page

$article = mysql_query("SELECT article_id, content, DATE_FORMAT(date, '%d %b %y')
FROM article
WHERE article_id = $_GET[id]");


while($row = mysql_fetch_array($article)){
echo $row . "</br>";
}

I know the WHERE statement is wrong, ive tried so many things that haven't worked, so I have just left my first try in :(

tony09uk
05-09-2012, 10:57 PM
Ok so I had one last go at this and managed to get it to work....in a fashion anyway.

I have wrote this:

mysql_query("SELECT *
FROM article
WHERE monthname(date) = 'February'")

However my issue is I want the month to be dependent on what the user clicks on the previous page and not have the month set in stone. How do I do this???

edbr
05-10-2012, 02:02 AM
you could send it as a $_GET value (or $_Post)
$where = $_GET[id]:
then use WHERE monthname(date) = '$where'")

tony09uk
05-10-2012, 11:20 AM
Ok, so just to save me a little playing about time....i have spent far to long on this :(

Do I set the previous page (i.e the page it is being sent from) up as a form with an method of GET? or is that not necessary? and if i am would the GET statement need to get month rather than id?

you could send it as a $_GET value (or $_Post)
$where = $_GET[id]:
then use WHERE monthname(date) = '$where'")

edbr
05-11-2012, 02:06 AM
always echo the results of either $_Post or $_GETto see what you are dealing with (for testing i mean).
as for pasising $_GEt it can be dona as a link without a form, ie <a href='page.php?id=value"> then the $_GET recived on the page.php will be $_GET['id'], which will read as value