teki Posted May 5, 2006 Share Posted May 5, 2006 Hey guysRecently i have been coding a php code which auto submits to a database. Can anyone supply me with a code ofShowing the lastest article with a read more optionShowing the latest ten with read moreShowing all with read more Quote Link to comment Share on other sites More sharing options...
Barand Posted May 6, 2006 Share Posted May 6, 2006 Asuming each record has a unique auto_incrementing id column, thenfor all articles (latest first)[code]$sql = "SELECT id, title FROM articles ORDER BY id DESC";[/code]for latest x articles to be displayed[code]$x = 10;$sql = "SELECT id, title FROM articles ORDER BY id DESC LIMIT $x";[/code]For the read more bit, output a link containing the id of the record in the querystring[code]echo "a href='showmore.php?id=$id>Read more...</a>";[/code] Quote Link to comment Share on other sites More sharing options...
teki Posted May 6, 2006 Author Share Posted May 6, 2006 [!--quoteo(post=371789:date=May 6 2006, 06:42 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 6 2006, 06:42 PM) [snapback]371789[/snapback][/div][div class=\'quotemain\'][!--quotec--]Asuming each record has a unique auto_incrementing id column, thenfor all articles (latest first)[code]$sql = "SELECT id, title FROM articles ORDER BY id DESC";[/code]for latest x articles to be displayed[code]$x = 10;$sql = "SELECT id, title FROM articles ORDER BY id DESC LIMIT $x";[/code]For the read more bit, output a link containing the id of the record in the querystring[code]echo "a href='showmore.php?id=$id>Read more...</a>";[/code][/quote]Thanks and is that code auto going to connect to the right database? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 6, 2006 Share Posted May 6, 2006 You still need to connect to your server and select the database, but as you are already writing to it I assume you know how to do that. Quote Link to comment Share on other sites More sharing options...
teki Posted May 6, 2006 Author Share Posted May 6, 2006 [!--quoteo(post=371802:date=May 6 2006, 09:16 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 6 2006, 09:16 PM) [snapback]371802[/snapback][/div][div class=\'quotemain\'][!--quotec--]You still need to connect to your server and select the database, but as you are already writing to it I assume you know how to do that.[/quote]Ok Cheers mate[code]echo "a href='showmore.php?id=$id>Read more...</a>";[/code]With taht code u gave me does there need to be more information/ coding in a file named showmore.php Quote Link to comment Share on other sites More sharing options...
corillo181 Posted May 7, 2006 Share Posted May 7, 2006 YOU NEED A PAGE NAMED SHOWMORE.PHP..THAT WHERE IS ASSUMING YOU GOT THE REST OF THE ARTICLE.. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 7, 2006 Share Posted May 7, 2006 [!--quoteo(post=371897:date=May 6 2006, 11:17 PM:name=teki)--][div class=\'quotetop\']QUOTE(teki @ May 6 2006, 11:17 PM) [snapback]371897[/snapback][/div][div class=\'quotemain\'][!--quotec--]Ok Cheers mate[code]echo "a href='showmore.php?id=$id>Read more...</a>";[/code]With taht code u gave me does there need to be more information/ coding in a file named showmore.php[/quote]When the user clicks the "Read more..." link the page "showmore.php" (or whatever you want to call it) will be loaded and the id of the article is passed to it in the querystring. That page then has to query the database table, using the id value, to display the article contents in full. Quote Link to comment Share on other sites More sharing options...
teki Posted May 7, 2006 Author Share Posted May 7, 2006 [!--quoteo(post=371970:date=May 7 2006, 07:27 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 7 2006, 07:27 PM) [snapback]371970[/snapback][/div][div class=\'quotemain\'][!--quotec--]When the user clicks the "Read more..." link the page "showmore.php" (or whatever you want to call it) will be loaded and the id of the article is passed to it in the querystring. That page then has to query the database table, using the id value, to display the article contents in full.[/quote]ok thanks and one last questionWith showing the differnt vairables on the pageh which shows the full article do you need to put lots of echos? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 7, 2006 Share Posted May 7, 2006 One or many - depends on your coding style, for exampleYou can[code]<?phpecho "<H1>$title<H1> <H3>Author : $author</H3> <p>$content</p> <p>Date : $article_date</p>";?> [/code]or[code]<H1><?php echo $title ?><H1><H3>Author : <?php echo $author ?></H3><p><?php echo $content ?></p><p>Date : <?php echo $article_date ?></p>[/code] Quote Link to comment Share on other sites More sharing options...
emehrkay Posted May 7, 2006 Share Posted May 7, 2006 understanding this concept is what really opened php up for me.on the show more page you have to run another query based on the id passed in the urlshowmore.php?id=15$id = $_GET['id']; //get the id from the querystring$query = "SELECT * FROM table WHERE id = ". $id ."";that query will return article number 15 Quote Link to comment Share on other sites More sharing options...
teki Posted May 8, 2006 Author Share Posted May 8, 2006 [!--quoteo(post=371991:date=May 7 2006, 10:45 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 7 2006, 10:45 PM) [snapback]371991[/snapback][/div][div class=\'quotemain\'][!--quotec--]One or many - depends on your coding style, for exampleYou can[code]<?phpecho "<H1>$title<H1> <H3>Author : $author</H3> <p>$content</p> <p>Date : $article_date</p>";?> [/code]or[code]<H1><?php echo $title ?><H1><H3>Author : <?php echo $author ?></H3><p><?php echo $content ?></p><p>Date : <?php echo $article_date ?></p>[/code][/quote]Ok thanks and to put them on different parts of a page u could put em in tables right? [!--quoteo(post=371999:date=May 8 2006, 12:00 AM:name=emehrkay)--][div class=\'quotetop\']QUOTE(emehrkay @ May 8 2006, 12:00 AM) [snapback]371999[/snapback][/div][div class=\'quotemain\'][!--quotec--]understanding this concept is what really opened php up for me.on the show more page you have to run another query based on the id passed in the urlshowmore.php?id=15$id = $_GET['id']; //get the id from the querystring$query = "SELECT * FROM table WHERE id = ". $id ."";that query will return article number 15[/quote]So would i mix both of your codes together?And would i need to put a connecting to database on this page? Quote Link to comment Share on other sites More sharing options...
teki Posted May 9, 2006 Author Share Posted May 9, 2006 Anyone know? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 9, 2006 Share Posted May 9, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]And would i need to put a connecting to database on this page?[/quote]Yes, you need to connect on each page that accesses the db Quote Link to comment Share on other sites More sharing options...
teki Posted May 9, 2006 Author Share Posted May 9, 2006 [!--quoteo(post=372521:date=May 9 2006, 04:56 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 9 2006, 04:56 PM) [snapback]372521[/snapback][/div][div class=\'quotemain\'][!--quotec--]Yes, you need to connect on each page that accesses the db[/quote]also barand do i need to use the code emehrkay gave me? The one that goes$id = $_GET['id']; //get the id from the querystring$query = "SELECT * FROM table WHERE id = ". $id ."";Also would the code for showing latest article be[code]$x = 1;$sql = "SELECT id, title FROM articles ORDER BY id DESC LIMIT $x";[/code]and if you had five variables in the article and you only wanted to show 2 of them with that latest one article code what would you add to that code? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 9, 2006 Share Posted May 9, 2006 You'll need code like emehrkay posted for getting that specific article from the "read more..." link.In the SELECT clause try to avoid "SELECT * " as this will get all the columns and just specify the ones you actually need. The less data you pull from the database the quicker the query. Listing the actual columns also better documents what the query is doing if you come back to it later or if someone else needs to change something.A series of articles by Kevin Yank got me started with PHP/MySQL - worth a read.[a href=\"http://www.sitepoint.com/article/publishing-mysql-data-web\" target=\"_blank\"]http://www.sitepoint.com/article/publishing-mysql-data-web[/a] 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.