Jump to content

[SOLVED] Using Arrays to sort posts


starkplastic

Recommended Posts

Hi,

 

I'm trying to sort my posts so that they are grouped and seperated for every month. For example all december posts will be grouped togther and given a header with the name december, and etc for the other months.

 

<?php
include ('connect.php');
$months = array(1 => "january", 2 => "february", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
for($i = 12; $i > 0; $i--)
{
   echo "<h2>" .$months[$i] ."</h2>";
    (row 29->)$sql = mysql_query("SELECT DATE_FORMAT(date, '%n') AS manad, date, title FROM news_posts WHERE MONTH(manad)= '$i'");
    while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
   echo $row['title'];
   echo $row['date'];
}
   }
?> 

 

All I get is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/alexofsweden/public_html/archive.php on line 29.

 

 

If there is another solution to my problem that is easier, share it please :)

 

Link to comment
Share on other sites

oh... line 29... sorry.

 

 

Well, the error basically means that your SQL isn't valid.  Echo it out before the query, or try running it directly against the database.  Even if it returns 0 rows, a valid query will return a useable mySQL resource.

Link to comment
Share on other sites

try

<?php
include ('connect.php');
   
$sql = "SELECT MONTHNAME(date) as mth, date, title 
        FROM news_posts
        ORDER BY date";
$res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");

$prevMth = '';
while (list($mth, $date, $title) = mysql_fetch_row($res)) {
    if ($prevMth != $mth) {
        echo "<h2>$mth</h2>";
        $prevMth = $mth;
    }
    echo $title . ' ' . date('jS M Y', $date) . '<br />';
}

?> 

Link to comment
Share on other sites

try

<?php
include ('connect.php');
   
$sql = "SELECT MONTHNAME(date) as mth, date, title 
        FROM news_posts
        ORDER BY date";
$res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");

$prevMth = '';
while (list($mth, $date, $title) = mysql_fetch_row($res)) {
    if ($prevMth != $mth) {
        echo "<h2>$mth</h2>";
        $prevMth = $mth;
    }
    echo $title . ' ' . date('jS M Y', $date) . '<br />';
}

?> 

 

wow thanks a mill!!!!

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.