starkplastic Posted March 19, 2007 Share Posted March 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
bwochinski Posted March 19, 2007 Share Posted March 19, 2007 You have some extra text before your $sql variable... (row 29->)$sql = mysql_query("SELECT DATE_... Quote Link to comment Share on other sites More sharing options...
starkplastic Posted March 19, 2007 Author Share Posted March 19, 2007 You have some extra text before your $sql variable... (row 29->)$sql = mysql_query("SELECT DATE_... To show where row 29 is. Quote Link to comment Share on other sites More sharing options...
bwochinski Posted March 19, 2007 Share Posted March 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 19, 2007 Share Posted March 19, 2007 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 />'; } ?> Quote Link to comment Share on other sites More sharing options...
starkplastic Posted March 20, 2007 Author Share Posted March 20, 2007 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!!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.