zombietuesday Posted September 20, 2006 Share Posted September 20, 2006 Hi .. i am currently trying to modify a script that drags just 1 random item of news from the database, I have got as far making it drag 5 items of news out of the DB!BUT .. what i really want is 5 items .. in order by date :)the original code is here : [code]<div align="justify"><? $row = mysql_fetch_array(mysql_query("SELECT * FROM `match_news` ORDER BY RAND() LIMIT 5")); echo nl2br($row["article"]); ?> </div>[/code]what ive done to get 5 is here : [code]<div align="justify"><? $newsAmount=0;while ($newsAmount<5) {$row = mysql_fetch_array(mysql_query("SELECT * FROM `match_news` ORDER BY RAND() LIMIT 5")); echo nl2br($row["article"]); ?><br /><hr><br /><? $newsAmount++; } ?> </div>[/code]Now im a complete newb at any kind of scripting .. the loop seemed easy enuff to figure out ( tho ive probably done it really really wrong!Any help would be MUCH appreciated! I know this is real begginers stuff! Quote Link to comment https://forums.phpfreaks.com/topic/21379-new-to-coding-php-so-be-gentle-help-with-getting-info-from-database/ Share on other sites More sharing options...
HuggieBear Posted September 20, 2006 Share Posted September 20, 2006 OK, to order by date, you must have the date in the database field somewhere. Assuming this is the case and the database field is called article_date, try this:[code]<div align="justify"><?php$query = "SELECT * FROM `match_news` ORDER BY article_date DESC LIMIT 5";$result = mysql_query($query);while (mysql_fetch_array($result)) { echo nl2br($row["article"]); echo '<br /><hr><br />';}?></div>[/code]There's no point in limiting the loop to 5 iterations if you know your SQL array only contains 5 rows. You've made the restriction in MySQL.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/21379-new-to-coding-php-so-be-gentle-help-with-getting-info-from-database/#findComment-95190 Share on other sites More sharing options...
zombietuesday Posted September 20, 2006 Author Share Posted September 20, 2006 [quote author=HuggieBear link=topic=108751.msg437810#msg437810 date=1158746780]OK, to order by date, you must have the date in the database field somewhere. Assuming this is the case and the database field is called article_date, try this:[code]<div align="justify"><?php$query = "SELECT * FROM `match_news` ORDER BY article_date DESC LIMIT 5";$result = mysql_query($query);while (mysql_fetch_array($result)) { echo nl2br($row["article"]); echo '<br /><hr><br />';}?></div>[/code]There's no point in limiting the loop to 5 iterations if you know your SQL array only contains 5 rows. You've made the restriction in MySQL.RegardsHuggie[/quote]THANKS!!im not to bothered about date as such .. more about the order they were put in... is there something generic for that .. an ID or something ? Thanks !the 5 limit was to just get the last 5 news items .. maybe i did that wrong ? Quote Link to comment https://forums.phpfreaks.com/topic/21379-new-to-coding-php-so-be-gentle-help-with-getting-info-from-database/#findComment-95194 Share on other sites More sharing options...
HuggieBear Posted September 20, 2006 Share Posted September 20, 2006 What columns are in your table? It's always a good idea to have an auto-incrementing ID column of some kind in there.e.g.[b]Match_News[/b]Article_ID (unique, auto-increment)Article_TitleArticle_DateArticle_ContentsThen your sql could look like this:[code]<div align="justify"><?php$query = "SELECT * FROM `Match_News` ORDER BY Article_ID DESC LIMIT 5";$result = mysql_query($query);while (mysql_fetch_array($result)) { echo nl2br($row["article"]); echo '<br /><hr><br />';}?></div>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/21379-new-to-coding-php-so-be-gentle-help-with-getting-info-from-database/#findComment-95200 Share on other sites More sharing options...
zombietuesday Posted September 20, 2006 Author Share Posted September 20, 2006 [quote author=HuggieBear link=topic=108751.msg437820#msg437820 date=1158748026]What columns are in your table? It's always a good idea to have an auto-incrementing ID column of some kind in there.e.g.[b]Match_News[/b]Article_ID (unique, auto-increment)Article_TitleArticle_DateArticle_ContentsThen your sql could look like this:[code]<div align="justify"><?php$query = "SELECT * FROM `Match_News` ORDER BY Article_ID DESC LIMIT 5";$result = mysql_query($query);while (mysql_fetch_array($result)) { echo nl2br($row["article"]); echo '<br /><hr><br />';}?></div>[/code]RegardsHuggie[/quote]Looks to be just the 2 id and articleid = auto_increment Quote Link to comment https://forums.phpfreaks.com/topic/21379-new-to-coding-php-so-be-gentle-help-with-getting-info-from-database/#findComment-95205 Share on other sites More sharing options...
HuggieBear Posted September 20, 2006 Share Posted September 20, 2006 In that case the following should work.Huggie[code]<div align="justify"><?php$query = "SELECT * FROM `Match_News` ORDER BY id DESC LIMIT 5";$result = mysql_query($query);while (mysql_fetch_array($result)) { echo nl2br($row["article"]); echo '<br /><hr><br />';}?></div>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21379-new-to-coding-php-so-be-gentle-help-with-getting-info-from-database/#findComment-95208 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.