Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why isn't this displaying the month and year???

Status
Not open for further replies.

cturner01

Technical User
Aug 1, 2006
29
AU
When I test the following code it doesn't display the month and year above the article titles. It displays the article titles all right. Why is this happening? Can someone please help me solve this problem? Thanks in advance.

Code:
require "config2.php";

if(!isset($_GET['page'])) {
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 5;

// Figure out the limit for the query based
// on the current page number
$from = (($page * $max_results) - $max_results);

$result = mysql_query ("SELECT DATE_FORMAT(selectMonth, '%M') AS month, DATE_FORMAT(selectYear, '%Y') AS year, articleid, articletitle FROM items LIMIT $from, $max_results");

if ( mysql_num_rows ( $result ) > 0 )
{
	$c_month = '';
	$c_year  = '';

	echo "<br />";

	while ( $row = mysql_fetch_assoc ( $result ) )
	{

		if ( $row['year'] != $c_year )
		{
			echo "<h3>" . $row['year'] . "</h3><br />";

			$c_year = $row['year'];
		}


		if ( $row['month'] != $c_month )
		{
			echo "\t<h4>" . $row['month'] . "</h4><br />";

			$c_month = $row['month'];
		}

		echo '<a href=article.php?articleid='. $row['articleid'] .'>' . $row['articletitle'] . '</a><br />';
	}
}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) AS Num FROM `items`"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>";

if ($page == 1) {
	echo "Previous ";
} else {
	echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\">Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

if ($page == $total_pages) {
	echo " Next";
} else {
	echo " <a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\">Next</a>";
}

echo "</center>";

mysql_close();
 
are month and year not mysql reserved words? try changing the sql call to this:

Code:
$result = mysql_query ("SELECT DATE_FORMAT(selectMonth, '%M') AS `month`, DATE_FORMAT(selectYear, '%Y') AS `year`, articleid, articletitle FROM items LIMIT $from, $max_results");
 
You can also temporarily comment out your if statements so you see the month and year on each article, just to look at it and see if your query is right.
 
I have solved the problem. I took DATE_FORMAT out of the query and now it is displaying the months and years.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top