Jump to content

Archived

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

zombietuesday

New to coding PHP .. so be gentle! Help with getting info from database.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Regards
Huggie

Share this post


Link to post
Share on other sites
[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.

Regards
Huggie
[/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 ?

Share this post


Link to post
Share on other sites
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_Title
Article_Date
Article_Contents

Then 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]

Regards
Huggie

Share this post


Link to post
Share on other sites
[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_Title
Article_Date
Article_Contents



Then 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]

Regards
Huggie
[/quote]


Looks to be just the 2

id and article

id = auto_increment

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.