Jump to content


Photo

Pulling Data


  • Please log in to reply
11 replies to this topic

#1 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 20 June 2006 - 10:26 AM

Hiya

I need a bit of advice ... or maybe if some code if your feeling nice :)

I am writing a content management system, and in it, I have built a News System. But, the problem is, on the homepage I am adding a "Recent 3 News Posts", which will hopefully if I can get the concept together, pull the last 3 Added news stories.

I havent a clue how to do it though :). Can someone maybe point me in the right direction, or even if your feeling nice, write me a bit of code.

I would be greatful! :)

Thanks for help in advance,

Richard

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 20 June 2006 - 10:29 AM

How do you exactly do all of this? Using Databases?
And how does the script "decide" how many new posts are new?
It's hard to answer your questions without seeing some script.

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 20 June 2006 - 01:25 PM

Sorry , should of been more clear.

We are using MYSQL.

Script wise - thats what I want to find out. I can pull the data from the database, but not thhe 3 most recent "ids". I dont know how to write the code to do it, as I am self taught, and hence never coming across this kind of situation...

The script is simple text boxes submitting to a Database (mysql).

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 20 June 2006 - 02:51 PM

You can use this query which should select most recent 3 news posts:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM news_table ORDER BY news_id DESC LIMIT 3 [!--sql2--][/div][!--sql3--]
Obviously you'll need to change news_table to the table that stores your news entries and news_id to the news_id column name.

#5 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 20 June 2006 - 07:35 PM

[!--quoteo(post=386035:date=Jun 20 2006, 09:51 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 20 2006, 09:51 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You can use this query which should select most recent 3 news posts:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM news_table ORDER BY news_id DESC LIMIT 3 [!--sql2--][/div][!--sql3--]
Obviously you'll need to change news_table to the table that stores your news entries and news_id to the news_id column name.
[/quote]

Thank you wildteen88 :)

#6 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 21 June 2006 - 06:04 PM

[!--quoteo(post=386035:date=Jun 20 2006, 09:51 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 20 2006, 09:51 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You can use this query which should select most recent 3 news posts:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM news_table ORDER BY news_id DESC LIMIT 3 [!--sql2--][/div][!--sql3--]
Obviously you'll need to change news_table to the table that stores your news entries and news_id to the news_id column name.
[/quote]

Got another question

a) How do I convert the above statement in PHP, would:

<?php
$query = "SELECT * FROM news_table ORDER BY news_id DESC LIMIT 3";
mysql_query($query, $mysql_link);
?>

work?

b) How would I convert that query to display the Three Recent Titles, then linking it to the news story

c) How could I implement that in PHP

Sorry for my n00bness

Ric

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 June 2006 - 06:24 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]a) How do I convert the above statement in PHP, would:

CODE
<?php
$query = "SELECT * FROM news_table ORDER BY news_id DESC LIMIT 3";
mysql_query($query, $mysql_link);
?>

work?[/quote]
Yes that'll work fine!

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]b) How would I convert that query to display the Three Recent Titles, then linking it to the news story?[/quote]
A bit like this

<?php

//connect to do db here

$query = "SELECT news_id, news_title FROM news_table ORDER BY news_id DESC LIMIT 3";
$result = mysql_query($query, $mysql_link);

while($row = mysql_fetch_assoc($result))
{
    echo "<a href=\"news.php?id={$row['news_id']}\">{$row['news_title']}</a><br />";
}

?>
Again in this code, change any instance of news_id, news_table and news_title to your field/table names.

#8 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 21 June 2006 - 07:01 PM

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/richard/public_html/noba/index.php on line 167

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/richard/public_html/noba/index.php on line 169

Any ideas? All DB connects are in the headers.....

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 June 2006 - 08:04 PM

Wheres $mysql_link being set to?

#10 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 21 June 2006 - 08:21 PM

Explain...

*embarrased*

#11 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 22 June 2006 - 10:25 AM

as in mysql_link, do you mean the headers? Because the Database is all set up correctly ...



#12 rh91uk

rh91uk
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 22 June 2006 - 10:41 AM

Ah its ok now, ive sorted it.

Thanks for all of your help :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users