News/Blog Archive list

I’m currently trying to make a news archive list and feel I almost have it right, but could use some help in finishing it off.

Currently my archive looks like this: (See Attachment Current_Archive.jpg) Notice I’m only getting one result per month.

I want it to look like this: (See Attachment Archive_Needed.jpg) Notice here I’m getting more than one result per month. This is just a Photoshop example. I dont want the same file names just more results under each month.

Here is the code I’m working with.

[php]<?php
include($_SERVER[‘DOCUMENT_ROOT’] . “/includes/database.php”);

$stmt = $db->prepare(“SELECT id,date,title FROM htp_news ORDER BY date DESC”);
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
$current_year = date(“Y”, strtotime($row[1]));
$current_month = date(“F”, strtotime($row[1]));

                      if ($current_year > $last_year) {
                       echo "<h1>" . $current_year . "</h1>";
                       echo "<h2>" . $current_month . "</h2>";
                       $last_year = $current_year;
                       $last_month = $current_month;

echo “

”;
}
elseif ($current_month > $last_month) {
echo “

” . $current_month . “

”;
$last_month = $current_month;
echo “”;
}
elseif ($current_year < $last_year) {
echo “

” . $current_year . “

”;
echo “

” . $current_month . “

”;
$last_year = $current_year;
$last_month = $current_month;

echo “

”;
}
elseif ($current_month < $last_month) {
echo “

” . $current_month . “

”;
$last_month = $current_month;

echo “

”;
}
}
}
?>[/php]

Any help would be greatly appreciated!


Current_Archive.jpg

I would do this

after the query loop through the results (as you do now), but instead of outputting add them to an array

[php]<?php
$result = array();
$stmt = $db->prepare(“SELECT id,date,title FROM htp_news ORDER BY date DESC”);
if ($stmt->execute()) {
while ($article = $stmt->fetch(PDO::FETCH_OBJ)) {
$year = date(“Y”, strtotime($article->date));
$month = date(“F”, strtotime($article->date));
$result[$year][$month][] = $article;
}

foreach ($result as $year => $data) {
  echo "<h1>" . $year . "</h1>";
  foreach ($data as $month => $articles) {
    echo "<h2>" . $month . "</h2>";
    echo "<ul>";
    foreach ($articles as $article) {
       echo "<li><a href='htp-news.php?id=". $article->id ."'>" . date("M d, Y", strtotime($article->date)) . " - " . $article->title . "</a></li>";
    }
    echo "</ul>";
  } 
}[/php]

Note, this was just written in notepad++/here on the forum, not tested. But it shows how I would do it, and I think its way more readable

You’re the man, Thank you so much. :smiley:
Worked great!

Yay, kinda figured there would be some errors, but sounds great. Please note the usage of variable names that reflect what they contain. If you just read through our codes I find it a lot easier to visualize what I am doing in mine.

Yea I guess that makes sense and would make things easier to read /understand. Good tip. Thanks again for the help.

I hate to come back to you again after you’ve helped me out already, but was wondering if you could show me how to convert this code so I can break out of php and style the code a little easier.

I’ve been trying to figure it out for a few hours but can’t seem to get the same results as the raw php.

Here is the original php code again:
[php]<?php
include_once($_SERVER[‘DOCUMENT_ROOT’]."/includes/database.php");

$result = array();
$stmt = $db->prepare(“SELECT id,date,title FROM htp_news ORDER BY date DESC”);
if ($stmt->execute()) {
while ($article = $stmt->fetch(PDO::FETCH_OBJ)) {
$year = date(“Y”, strtotime($article->date));
$month = date(“F”, strtotime($article->date));
$result[$year][$month][] = $article;
}

 foreach ($result as $year => $data) {
   echo "<h1>" . $year . "</h1>";
   foreach ($data as $month => $articles) {
     echo "<h2>" . $month . "</h2>";
     echo "<ul>";
     foreach ($articles as $article) {
        echo "<li><a href='htp-news.php?id=". $article->id ."'>" . date("M d, Y", strtotime($article->date)) . " - " . $article->title . "</a></li>";
     }
     echo "</ul>";
   } 
 }
}

?>[/php]

And here is my butchered attempt:
[php]<?php
include_once($_SERVER[‘DOCUMENT_ROOT’]."/includes/database.php");

$result = array();
$stmt = $db->prepare(“SELECT id,date,title FROM htp_news ORDER BY date DESC”);
if ($stmt->execute()) {
while ($article = $stmt->fetch(PDO::FETCH_OBJ)) {
$year = date(“Y”, strtotime($article->date));
$month = date(“F”, strtotime($article->date));
$result[$year][$month][] = $article;
}
?>

<?php foreach ($result as $year => $data): foreach($data as $month => $articles);?>
<h1><?php echo $year ?></h1>
<h2><?php echo $month ?></h2>
	<?php endforeach ?>
        
<ul>           
<?php foreach ($articles as $article):?>
<li>
<?php echo "" . date("M d, Y", strtotime($article->date)) . " - " . $article->title . "" ?>
</li>
<?php endforeach ?>
</ul>
<?php } ?>[/php]

With my attempt I get the following results

2014

February

2013

December

•Dec 27, 2013 - Foyer Divine School
•Dec 19, 2013 - Foyer Divine School
•Dec 19, 2013 - Mireille Senecharle


It looks like this when everything is correct:

2014

April

•Apr 10, 2014 - Test Post Images
•Apr 10, 2014 - Test Post 2
•Apr 10, 2014 - Test New Post 1
•Apr 05, 2014 - test 5
•Apr 05, 2014 - test 6
•Apr 02, 2014 - Test post 1

March

•Mar 13, 2014 - New Post Update test!
•Mar 11, 2014 - TEST post 2
•Mar 11, 2014 - TEST post 3

February

•Feb 07, 2014 - Mireille Senecharle

2013

December

•Dec 27, 2013 - Foyer Divine School
•Dec 19, 2013 - Foyer Divine School
•Dec 19, 2013 - Mireille Senecharle

Again sorry to have to come crawling back to you. :-[

Which IDE do you use to write code? Netbeans and others would light up as a christmas tree and point out the errors for you.

Basically you have moved around the foreaches so they are positioned wrong

[ul][li]I echo year between the first and second foreach[/li]
[li]You start the second foreach with a semi colon and not a colon[/li]
[li]You are making it uneccessary confusing when not indenting the code properly[/li]
[li]You start 3 foreach loops but only end 2[/li][/ul]

[php]<?php
include_once($_SERVER[‘DOCUMENT_ROOT’] . “/includes/database.php”);

$result = array();
$stmt = $db->prepare(“SELECT id,date,title FROM htp_news ORDER BY date DESC”);

if ($stmt->execute()) {
while ($article = $stmt->fetch(PDO::FETCH_OBJ)) {
$year = date(“Y”, strtotime($article->date));
$month = date(“F”, strtotime($article->date));
$article->date = date(“M d, Y”, strtotime($article->date));
$result[$year][$month][] = $article;
}

foreach ($result as $year => $data): ?>

<?= $year ?>


<?php foreach ($data as $month => $articles): ?>

<?= $month ?>



<?php endforeach ?> <?php endforeach ?> <?php } ?>[/php]

I currently use dreamweaver. I wasn’t getting any errors.

What IDE are you using I will switch to anything that will help point out my errors better.
I’ll check out the ones you mentioned.

Thank you again. That worked flawlessly.

Netbeans is a good free open source alternative, but you can read up on it in my “tutorial”/introduction here
http://www.phphelp.com/forum/the-occasional-tutorial/make-your-life-simpler-use-an-ide!/

There are definitly better alternatives out there than Dreamweaver…

Sweet, Thanks for the suggestion and link. I will switch ASAP.

Sponsor our Newsletter | Privacy Policy | Terms of Service