clown[NOR] Posted April 15, 2007 Share Posted April 15, 2007 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."; } } ?> Quote Link to comment Share on other sites More sharing options...
ignace Posted April 15, 2007 Share Posted April 15, 2007 why don't u just use: SELECT * FROM `news` ORDER BY `news_publish_date` DESC LIMIT 5 Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 15, 2007 Author Share Posted April 15, 2007 oh my god... i feel really stupid right now... Quote Link to comment Share on other sites More sharing options...
ignace Posted April 15, 2007 Share Posted April 15, 2007 i always do to Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 15, 2007 Author Share Posted April 15, 2007 well... now I got this message: Could not run query from DB EDIT: nevermind... my stupidity striked again... wrote ORBER instead of ORDER..haha 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.