Jump to content

[SOLVED] MySQL: read from the bottom to the top of the table?


clown[NOR]

Recommended Posts

is there an easy way to do this or do I have to just loop it like i do now?

 

this is what i'm using now...

<?php
## Get the last 5 news...
function freshNews() {
	global $dbHost, $dbUser, $dbPass, $dbName;
	if (!mysql_connect($dbHost, $dbUser, $dbPass)) { die("Unable to connect to DB"); }
	if (!mysql_select_db($dbName)) { die("Unable to select DB"); }

	$query = "SELECT * FROM news";
	$result = mysql_query($query);

	if (!$result) { die("Could not run query from DB"); }

	$dbNumRows = mysql_num_rows($result);

	if ($dbNumRows > 0) {
		$i = 1;
		while ($dbNumRows > 0) {
			if ($i <= 5) {
				$query = "SELECT * FROM news WHERE nid = '".$dbNumRows."'";
				$result = mysql_query($query);
				$newsItem = mysql_fetch_assoc($result);
				if (strlen($newsItem['title']) > 30) {
					echo '<a href="?view=news&id='.$newsItem['nid'].'">'.substr($newsItem['title'], 0, 30)."...</a><br>";
				} else { echo '<a href="?view=news&id='.$newsItem['nid'].'">'.$newsItem['title']."</a><br>"; }
				$dbNumRows = $dbNumRows - 1;
				$i++;
			} else { break; }
		}
	} else { echo "No news found."; }

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.