Jump to content

PHP/SQL Display Help


teki

Recommended Posts

Hey guys

Recently i have been coding a php code which auto submits to a database.

Can anyone supply me with a code of

Showing the lastest article with a read more option

Showing the latest ten with read more

Showing all with read more
Link to comment
Share on other sites

Asuming each record has a unique auto_incrementing id column, then

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

[!--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, then

for 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?
Link to comment
Share on other sites

[!--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
Link to comment
Share on other sites

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

[!--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 question

With showing the differnt vairables on the pageh which shows the full article do you need to put lots of echos?
Link to comment
Share on other sites

One or many - depends on your coding style, for example

You can

[code]<?php
echo "<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]
Link to comment
Share on other sites

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 url
showmore.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
Link to comment
Share on other sites

[!--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 example

You can

[code]<?php
echo "<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 url
showmore.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?
Link to comment
Share on other sites

[!--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?
Link to comment
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.