Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 zombietuesday

zombietuesday
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 20 September 2006 - 08:59 AM

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 :
<div align="justify"><? $row = mysql_fetch_array(mysql_query("SELECT * FROM `match_news` ORDER BY RAND() LIMIT 5"));
		echo nl2br($row["article"]);
	?>
    </div>

what ive done to get 5 is here :
<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>


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!

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 10:06 AM

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:

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

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 zombietuesday

zombietuesday
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 20 September 2006 - 10:11 AM

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:

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

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


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 ?

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 10:27 AM

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.

Match_News
Article_ID (unique, auto-increment)
Article_Title
Article_Date
Article_Contents

Then your sql could look like this:

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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 zombietuesday

zombietuesday
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 20 September 2006 - 10:32 AM

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.

Match_News
Article_ID (unique, auto-increment)
Article_Title
Article_Date
Article_Contents



Then your sql could look like this:

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

Regards
Huggie



Looks to be just the 2

id and article

id = auto_increment

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 20 September 2006 - 10:37 AM

In that case the following should work.

Huggie

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

Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users